Apache Zeta Components Manual :: File Source for memcache_backend.php
Source for file memcache_backend.php
Documentation is available at memcache_backend.php
* File containing the ezcCacheMemcacheBackend class.
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* @version //autogentag//
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* This backend stores data in a Memcache.
* @apichange This class will be deprecated in the next major version of the
* Cache component. Please do not use it directly, but use {@link }
* ezcCacheStorageMemcache} instead.
* @version //autogentag//
* The compress threshold.
* Nearly 1MB (48,576B less).
* Maximum length of a cache key for Memcached.
* Holds an instance to a Memcache object.
* Holds the options for this class.
* @var ezcCacheStorageMemcacheOptions
* Stores the connections to Memcached.
* @var array(string=>Memcache)
protected static $connections =
array();
* Keeps track of the number of backends using the same connection.
* This is to avoid that the dtor of a backend accedentally closes a
* connection that is still in used by another backend.
* @var array(string=>int)
protected static $connectionCounter =
array();
* Stores the connection identifier.
* This is generated in the ctor and used in the dtor.
* Constructs a new ezcCacheMemcacheBackend object.
* For options for this backend see {@link ezcCacheStorageMemcacheOptions}.
* @throws ezcBaseExtensionNotFoundException
* If the PHP memcache and zlib extensions are not installed.
* @throws ezcCacheMemcacheException
* If the connection to the Memcache host did not succeed.
* @param array(string=>mixed) $options
public function __construct( array $options =
array() )
// Currently 0 backends use the connection
if ( $this->options->persistent ===
true )
$this->memcache->setCompressThreshold( self::COMPRESS_THRESHOLD );
* Destructor for the Memcache backend.
// Save to ignore persistent connections, since close() does not affect them
* Adds the $var data to the cache under the key $key. Returns true or
* false depending on the success of the operation.
public function store( $key, $var, $expire =
0 )
if ( strlen( $key ) >
self::MAX_KEY_LENGTH )
// protect our data by wrapping it in an object
$compressed =
( $this->options->compressed ===
true ) ?
MEMCACHE_COMPRESSED :
false;
return $this->memcache->set( $key, $data, $compressed, $expire );
* Returns the data from the cache associated with key $key.
public function fetch( $key )
if ( strlen( $key ) >
self::MAX_KEY_LENGTH )
return ( is_object( $data ) ) ?
$data->var :
false;
* Deletes the data from the cache associated with key $key. Returns true or
* false depending on the success of the operation.
* The value $timeout specifies the timeout period in seconds for the delete
public function delete( $key, $timeout =
0 )
if ( strlen( $key ) >
self::MAX_KEY_LENGTH )
return $this->memcache->delete( $key, $timeout );
* Resets the complete backend.
* Marked private to not expose more of this interface to the user, since
* this will be removed in future versions.
// Kills whole memcache content
* Acquires a lock on the given $key.
* @param int $waitTime usleep()
* @param int $maxTime seconds
public function acquireLock( $key, $waitTime, $maxTime )
if ( strlen( $key ) >
self::MAX_KEY_LENGTH )
// add() does not replace and returns true on success. $maxTime is
// obeyed by Memcache expiry.
while ( $this->memcache->add( $key, $key, false, $maxTime ) ===
false )
* Releases a lock on the given $key.
if ( strlen( $key ) >
self::MAX_KEY_LENGTH )