Skip to content

Instantly share code, notes, and snippets.

@LeeSaferite
Created May 17, 2012 07:58
Show Gist options
  • Save LeeSaferite/3f952d1a002438c53903 to your computer and use it in GitHub Desktop.
Save LeeSaferite/3f952d1a002438c53903 to your computer and use it in GitHub Desktop.
<?php
final class LocaleMemoryCacheWrapper extends Zend_Cache_Core
{
/**
* Real cache object we are proxying
*
* @var Zend_Cache_Core
*/
protected $_parent;
/**
* Internal cache, prevent repeated cache requests
*
* @var array
*/
protected $_localCache = array();
public function __construct(Zend_Cache_Core $parent)
{
$this->_parent = $parent;
}
/**
* Set options using an instance of type Zend_Config
*
* @param Zend_Config $config
*
* @return Zend_Cache_Core
*/
public function setConfig(Zend_Config $config)
{
return $this->_parent->setConfig($config);
}
/**
* Set the backend
*
* @param Zend_Cache_Backend $backendObject
*
* @throws Zend_Cache_Exception
* @return void
*/
public function setBackend(Zend_Cache_Backend $backendObject)
{
$this->_parent->setBackend($backendObject);
}
/**
* Returns the backend
*
* @return Zend_Cache_Backend backend object
*/
public function getBackend()
{
return $this->_parent->getBackend();
}
/**
* Public frontend to set an option
*
* There is an additional validation (relatively to the protected _setOption method)
*
* @param string $name Name of the option
* @param mixed $value Value of the option
*
* @throws Zend_Cache_Exception
* @return void
*/
public function setOption($name, $value)
{
return $this->_parent->setOption($name, $value);
}
/**
* Public frontend to get an option value
*
* @param string $name Name of the option
*
* @throws Zend_Cache_Exception
* @return mixed option value
*/
public function getOption($name)
{
return $this->_parent->getOption($name);
}
/**
* Force a new lifetime
*
* The new value is set for the core/frontend but for the backend too (directive)
*
* @param int $newLifetime New lifetime (in seconds)
*
* @return void
*/
public function setLifetime($newLifetime)
{
$this->_parent->setLifetime($newLifetime);
}
/**
* Test if a cache is available for the given id and (if yes) return it (false else)
*
* @param string $id Cache id
* @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
* @param boolean $doNotUnserialize Do not serialize (even if automatic_serialization is true) => for internal use
*
* @return mixed|false Cached datas
*/
public function load($id, $doNotTestCacheValidity = false, $doNotUnserialize = false)
{
if (isset($this->_localCache[$id])) {
$data = $this->_localCache[$id];
} else {
$data = $this->_parent->load($id, $doNotTestCacheValidity, $doNotUnserialize);
$this->_localCache[$id] = $data;
}
return $data;
}
/**
* Test if a cache is available for the given id
*
* @param string $id Cache id
*
* @return int|false Last modified time of cache entry if it is available, false otherwise
*/
public function test($id)
{
return $this->_parent->test($id);
}
/**
* Save some data in a cache
*
* @param mixed $data Data to put in cache (can be another type than string if automatic_serialization is on)
* @param string $id Cache id (if not set, the last cache id will be used)
* @param array $tags Cache tags
* @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
* @param int $priority integer between 0 (very low priority) and 10 (maximum priority) used by some particular backends
*
* @throws Zend_Cache_Exception
* @return boolean True if no problem
*/
public function save($data, $id = null, $tags = array(), $specificLifetime = false, $priority = 8)
{
unset($this->_localCache[$id]);
return $this->_parent->save($data, $id, $tags, $specificLifetime, $priority);
}
/**
* Remove a cache
*
* @param string $id Cache id to remove
*
* @return boolean True if ok
*/
public function remove($id)
{
unset($this->_localCache[$id]);
return $this->_parent->remove($id);
}
/**
* Clean cache entries
*
* Available modes are :
* 'all' (default) => remove all cache entries ($tags is not used)
* 'old' => remove too old cache entries ($tags is not used)
* 'matchingTag' => remove cache entries matching all given tags
* ($tags can be an array of strings or a single string)
* 'notMatchingTag' => remove cache entries not matching one of the given tags
* ($tags can be an array of strings or a single string)
* 'matchingAnyTag' => remove cache entries matching any given tags
* ($tags can be an array of strings or a single string)
*
* @param string $mode
* @param array|string $tags
*
* @throws Zend_Cache_Exception
* @return boolean True if ok
*/
public function clean($mode = 'all', $tags = array())
{
// Be aggressive when clearing
$this->_localCache = array();
return $this->_parent->clean($mode, $tags);
}
/**
* Return an array of stored cache ids which match given tags
*
* In case of multiple tags, a logical AND is made between tags
*
* @param array $tags array of tags
*
* @return array array of matching cache ids (string)
*/
public function getIdsMatchingTags($tags = array())
{
return $this->_parent->getIdsMatchingTags($tags);
}
/**
* Return an array of stored cache ids which don't match given tags
*
* In case of multiple tags, a logical OR is made between tags
*
* @param array $tags array of tags
*
* @return array array of not matching cache ids (string)
*/
public function getIdsNotMatchingTags($tags = array())
{
return $this->_parent->getIdsNotMatchingTags($tags);
}
/**
* Return an array of stored cache ids which match any given tags
*
* In case of multiple tags, a logical OR is made between tags
*
* @param array $tags array of tags
*
* @return array array of matching any cache ids (string)
*/
public function getIdsMatchingAnyTags($tags = array())
{
return $this->_parent->getIdsMatchingAnyTags($tags);
}
/**
* Return an array of stored cache ids
*
* @return array array of stored cache ids (string)
*/
public function getIds()
{
return $this->_parent->getIds();
}
/**
* Return an array of stored tags
*
* @return array array of stored tags (string)
*/
public function getTags()
{
return $this->_parent->getTags();
}
/**
* Return the filling percentage of the backend storage
*
* @return int integer between 0 and 100
*/
public function getFillingPercentage()
{
return $this->_parent->getFillingPercentage();
}
/**
* Return an array of metadatas for the given cache id
*
* The array will include these keys :
* - expire : the expire timestamp
* - tags : a string array of tags
* - mtime : timestamp of last modification time
*
* @param string $id cache id
*
* @return array array of metadatas (false if the cache id is not found)
*/
public function getMetadatas($id)
{
return $this->_parent->getMetadatas($id);
}
/**
* Give (if possible) an extra lifetime to the given cache id
*
* @param string $id cache id
* @param int $extraLifetime
*
* @return boolean true if ok
*/
public function touch($id, $extraLifetime)
{
return $this->_parent->touch($id, $extraLifetime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment