An interesting approach. Similarly a Zend_Cache_Backend_Session could be implemented fairly easily based on your excellent code. This allows for caching of user details, and expensive user related queries using just a Zend_Session_Namespace as the backend for your cache.
[code]
data)) {
self::$_namespace->data = array();
}
}
/**
* 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
* @return string cached datas (or false)
*/
public function load($id, $doNotTestCacheValidity = false){
if (!isset(self::$_namespace->data[$id])) {
return false;
}
return self::$_namespace->data[$id];
}
/**
* Test if a cache is available or not (for the given id)
*
* @param string $id cache id
* @return boolean true if exists false if not
*/
public function test($id) {
return isset(self::$_namespace->data[$id]);
}
/**
* Save some string datas into a cache record
*
* Note : $data is always “string” (serialization is done by the
* core not by the backend)
*
* @param string $data datas to cache
* @param string $id cache id
* @param array $tags array of strings, the cache record will be tagged by each string entry
* @param int $specificLifetime if != false, set a specific lifetime for this cache record (null => infinite lifetime)
* @return boolean true if no problem
*/
public function save($data, $id, $tags = array(), $specificLifetime = false) {
self::$_namespace->data[$id] = $data;
if (count($tags) > 0) {
$this->_log(”Zend_Cache_Backend_Registry::save() : tags are unsupported by the session backend”);
}
return true;
}
/**
* Remove a cache record
*
* @param string $id cache id
* @return boolean true if no problem
*/
public function remove($id) {
unset(self::$_namespace->data[$id]);
return true;
}
/**
* Clean some cache records
*
* 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)
*
* @param string $mode clean mode
* @param array $tags array of tags
* @return boolean true if no problem
*/
public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array()) {
if ($mode==Zend_Cache::CLEANING_MODE_ALL) {
self::$rnamespace->data = array();
}
if ($mode==Zend_Cache::CLEANING_MODE_OLD) {
$this->_log(”Zend_Cache_Backend_registry::clean() : CLEANING_MODE_OLD is unsupported by the session backend”);
}
if ($mode==Zend_Cache::CLEANING_MODE_MATCHING_TAG) {
$this->_log(”Zend_Cache_Backend_registry::clean() : tags are unsupported by the session backend”);
}
if ($mode==Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG) {
$this->_log(”Zend_Cache_Backend_registry::clean() : tags are unsupported by the session backend”);
}
}
/**
* Return true if the automatic cleaning is available for the backend
*
* @return boolean
*/
public function isAutomaticCleaningAvailable() {
return false;
}
}
[/code]
About Me
I’m Sameer Parwani a 25 year old web developer from Massachusetts. I aim to use this blog to contribute to the tech community with my own thoughts, tips, and insights. I am occasionally available for contract work. Check the about page for more.
Using Zend_Registry as a Zend_Cache Backend
December 24th, 2008 at 7:58 amHi,
An interesting approach. Similarly a Zend_Cache_Backend_Session could be implemented fairly easily based on your excellent code. This allows for caching of user details, and expensive user related queries using just a Zend_Session_Namespace as the backend for your cache.
[code]
data)) {
self::$_namespace->data = array();
}
}
/**
* 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
* @return string cached datas (or false)
*/
public function load($id, $doNotTestCacheValidity = false){
if (!isset(self::$_namespace->data[$id])) {
return false;
}
return self::$_namespace->data[$id];
}
/**
* Test if a cache is available or not (for the given id)
*
* @param string $id cache id
* @return boolean true if exists false if not
*/
public function test($id) {
return isset(self::$_namespace->data[$id]);
}
/**
* Save some string datas into a cache record
*
* Note : $data is always “string” (serialization is done by the
* core not by the backend)
*
* @param string $data datas to cache
* @param string $id cache id
* @param array $tags array of strings, the cache record will be tagged by each string entry
* @param int $specificLifetime if != false, set a specific lifetime for this cache record (null => infinite lifetime)
* @return boolean true if no problem
*/
public function save($data, $id, $tags = array(), $specificLifetime = false) {
self::$_namespace->data[$id] = $data;
if (count($tags) > 0) {
$this->_log(”Zend_Cache_Backend_Registry::save() : tags are unsupported by the session backend”);
}
return true;
}
/**
* Remove a cache record
*
* @param string $id cache id
* @return boolean true if no problem
*/
public function remove($id) {
unset(self::$_namespace->data[$id]);
return true;
}
/**
* Clean some cache records
*
* 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)
*
* @param string $mode clean mode
* @param array $tags array of tags
* @return boolean true if no problem
*/
public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array()) {
if ($mode==Zend_Cache::CLEANING_MODE_ALL) {
self::$rnamespace->data = array();
}
if ($mode==Zend_Cache::CLEANING_MODE_OLD) {
$this->_log(”Zend_Cache_Backend_registry::clean() : CLEANING_MODE_OLD is unsupported by the session backend”);
}
if ($mode==Zend_Cache::CLEANING_MODE_MATCHING_TAG) {
$this->_log(”Zend_Cache_Backend_registry::clean() : tags are unsupported by the session backend”);
}
if ($mode==Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG) {
$this->_log(”Zend_Cache_Backend_registry::clean() : tags are unsupported by the session backend”);
}
}
/**
* Return true if the automatic cleaning is available for the backend
*
* @return boolean
*/
public function isAutomaticCleaningAvailable() {
return false;
}
}
[/code]