Doctrine 1.2.4
Doctrine_Cache_Memcache Class Reference

Inherits Doctrine_Cache_Driver.

Public Member Functions

 __construct ($options=array())
 
 contains ($id)
 
 delete ($id)
 
 deleteAll ()
 
 deleteByPrefix ($prefix)
 
 deleteByRegex ($regex)
 
 deleteBySuffix ($suffix)
 
 fetch ($id, $testCacheValidity=true)
 
 getOption ($option)
 
 save ($id, $data, $lifeTime=false)
 
 setOption ($option, $value)
 

Protected Member Functions

 _doContains ($id)
 
 _doDelete ($id)
 
 _doFetch ($id, $testCacheValidity=true)
 
 _doSave ($id, $data, $lifeTime=false)
 
 _getCacheKeys ()
 
 _getKey ($id)
 

Detailed Description

Definition at line 34 of file Memcache.php.

Constructor & Destructor Documentation

Doctrine_Cache_Memcache::__construct (   $options = array())

constructor

Parameters
array$optionsassociative array of cache driver options

Definition at line 46 of file Memcache.php.

{
if ( ! extension_loaded('memcache')) {
throw new Doctrine_Cache_Exception('In order to use Memcache driver, the memcache extension must be loaded.');
}
parent::__construct($options);
if (isset($options['servers'])) {
$value= $options['servers'];
if (isset($value['host'])) {
// in this case, $value seems to be a simple associative array (one server only)
$value = array(0 => $value); // let's transform it into a classical array of associative arrays
}
$this->setOption('servers', $value);
}
$this->_memcache = new Memcache;
foreach ($this->_options['servers'] as $server) {
if ( ! array_key_exists('persistent', $server)) {
$server['persistent'] = true;
}
if ( ! array_key_exists('port', $server)) {
$server['port'] = 11211;
}
$this->_memcache->addServer($server['host'], $server['port'], $server['persistent']);
}
}

Member Function Documentation

Doctrine_Cache_Memcache::_doContains (   $id)
protected

Test if a cache is available or not (for the given id)

Parameters
string$idcache id
Returns
mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record

Definition at line 92 of file Memcache.php.

{
return (bool) $this->_memcache->get($id);
}
Doctrine_Cache_Memcache::_doDelete (   $id)
protected

Remove a cache record directly. This method is implemented by the cache drivers and used in Doctrine_Cache_Driver::delete()

Parameters
string$idcache id
Returns
boolean true if no problem

Definition at line 124 of file Memcache.php.

{
return $this->_memcache->delete($id);
}
Doctrine_Cache_Memcache::_doFetch (   $id,
  $testCacheValidity = true 
)
protected

Test if a cache record exists for the passed id

Parameters
string$idcache id
Returns
mixed Returns either the cached data or false

Definition at line 81 of file Memcache.php.

{
return $this->_memcache->get($id);
}
Doctrine_Cache_Memcache::_doSave (   $id,
  $data,
  $lifeTime = false 
)
protected

Save a cache record directly. This method is implemented by the cache drivers and used in Doctrine_Cache_Driver::save()

Parameters
string$idcache id
string$datadata to cache
int$lifeTimeif != false, set a specific lifetime for this cache record (null => infinite lifeTime)
Returns
boolean true if no problem

Definition at line 106 of file Memcache.php.

{
if ($this->_options['compression']) {
$flag = MEMCACHE_COMPRESSED;
} else {
$flag = 0;
}
return $this->_memcache->set($id, $data, $flag, $lifeTime);
}
Doctrine_Cache_Memcache::_getCacheKeys ( )
protected

Fetch an array of all keys stored in cache

Returns
array Returns the array of cache keys

Definition at line 134 of file Memcache.php.

{
$keys = array();
$allSlabs = $this->_memcache->getExtendedStats('slabs');
foreach ($allSlabs as $server => $slabs) {
foreach (array_keys($slabs) as $slabId) {
$dump = $this->_memcache->getExtendedStats('cachedump', (int) $slabId);
foreach ($dump as $entries) {
if ($entries) {
$keys = array_merge($keys, array_keys($entries));
}
}
}
}
return $keys;
}
Doctrine_Cache_Driver::_getKey (   $id)
protectedinherited

Get the hash key passing its suffix

Parameters
string$idThe hash key suffix
Returns
string Hash key to be used by drivers

Definition at line 226 of file Driver.php.

{
$prefix = isset($this->_options['prefix']) ? $this->_options['prefix'] : '';
if ( ! $prefix || strpos($id, $prefix) === 0) {
return $id;
} else {
return $prefix . $id;
}
}
Doctrine_Cache_Driver::contains (   $id)
inherited

Test if a cache record exists for the passed id

Parameters
string$idcache id
Returns
mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record

Definition at line 101 of file Driver.php.

{
$key = $this->_getKey($id);
return $this->_doContains($key);
}
Doctrine_Cache_Driver::delete (   $id)
inherited

Remove a cache record

Note: This method accepts wildcards with the * character

Parameters
string$idcache id
Returns
boolean true if no problem

Definition at line 129 of file Driver.php.

{
$key = $this->_getKey($id);
if (strpos($key, '*') !== false) {
return $this->deleteByRegex('/' . str_replace('*', '.*', $key) . '/');
}
return $this->_doDelete($key);
}
Doctrine_Cache_Driver::deleteAll ( )
inherited

Delete all cache entries from the cache driver

Returns
integer $count The number of deleted cache entries

Definition at line 208 of file Driver.php.

{
$count = 0;
if (is_array($keys = $this->_getCacheKeys())) {
foreach ($keys as $key) {
$count++;
$this->delete($key);
}
}
return $count;
}
Doctrine_Cache_Driver::deleteByPrefix (   $prefix)
inherited

Delete cache entries where the key has the passed prefix

Parameters
string$prefix
Returns
integer $count The number of deleted cache entries

Definition at line 167 of file Driver.php.

{
$count = 0;
$keys = $this->_getCacheKeys();
if (is_array($keys)) {
foreach ($keys as $key) {
if (strpos($key, $prefix) === 0) {
$count++;
$this->delete($key);
}
}
}
return $count;
}
Doctrine_Cache_Driver::deleteByRegex (   $regex)
inherited

Delete cache entries where the key matches a PHP regular expressions

Parameters
string$regex
Returns
integer $count The number of deleted cache entries

Definition at line 146 of file Driver.php.

{
$count = 0;
$keys = $this->_getCacheKeys();
if (is_array($keys)) {
foreach ($keys as $key) {
if (preg_match($regex, $key)) {
$count++;
$this->delete($key);
}
}
}
return $count;
}
Doctrine_Cache_Driver::deleteBySuffix (   $suffix)
inherited

Delete cache entries where the key has the passed suffix

Parameters
string$suffix
Returns
integer $count The number of deleted cache entries

Definition at line 188 of file Driver.php.

{
$count = 0;
$keys = $this->_getCacheKeys();
if (is_array($keys)) {
foreach ($keys as $key) {
if (substr($key, -1 * strlen($suffix)) == $suffix) {
$count++;
$this->delete($key);
}
}
}
return $count;
}
Doctrine_Cache_Driver::fetch (   $id,
  $testCacheValidity = true 
)
inherited

Fetch a cache record from this cache driver instance

Parameters
string$idcache id
boolean$testCacheValidityif set to false, the cache validity won't be tested
Returns
mixed Returns either the cached data or false

Definition at line 89 of file Driver.php.

{
$key = $this->_getKey($id);
return $this->_doFetch($key, $testCacheValidity);
}
Doctrine_Cache_Driver::getOption (   $option)
inherited

Get value of option

Parameters
mixed$optionthe option name
Returns
mixed option value

Definition at line 73 of file Driver.php.

{
if ( ! isset($this->_options[$option])) {
return null;
}
return $this->_options[$option];
}
Doctrine_Cache_Driver::save (   $id,
  $data,
  $lifeTime = false 
)
inherited

Save some string datas into a cache record

Parameters
string$idcache id
string$datadata to cache
int$lifeTimeif != false, set a specific lifetime for this cache record (null => infinite lifeTime)
Returns
boolean true if no problem

Definition at line 115 of file Driver.php.

{
$key = $this->_getKey($id);
return $this->_doSave($key, $data, $lifeTime);
}
Doctrine_Cache_Driver::setOption (   $option,
  $value 
)
inherited

Set option name and value

Parameters
mixed$optionthe option name
mixed$valueoption value
Returns
boolean TRUE on success, FALSE on failure

Definition at line 58 of file Driver.php.

{
if (isset($this->_options[$option])) {
$this->_options[$option] = $value;
return true;
}
return false;
}

The documentation for this class was generated from the following file: