DoctrineAdapter.php

Go to the documentation of this file.
00001 <?php
00002 
00038 class Robo47_Cache_DoctrineAdapter extends Doctrine_Cache_Driver
00039 {
00040 
00046     protected $_cache = null;
00052     protected $_prefix = '';
00058     protected $_tags = array();
00059 
00065     public function __construct($cache, $prefix = '', $tags = array())
00066     {
00067         $this->setCache($cache);
00068         $this->setPrefix($prefix);
00069         $this->setTags($tags);
00070     }
00071 
00077     public function getPrefix()
00078     {
00079         return $this->_prefix;
00080     }
00081 
00088     public function setPrefix($prefix)
00089     {
00090         $this->_prefix = $prefix;
00091         return $this;
00092     }
00093 
00099     public function getCache()
00100     {
00101         return $this->_cache;
00102     }
00103 
00110     public function setTags(array $tags = array())
00111     {
00112         $this->_tags = $tags;
00113         return $this;
00114     }
00115 
00121     public function getTags()
00122     {
00123         return $this->_tags;
00124     }
00125 
00131     protected function _cacheFromRegistry($key)
00132     {
00133         if (Zend_Registry::isRegistered($key)) {
00134             return Zend_Registry::get($key);
00135         } else {
00136             $message = 'Registry key "' . $key .
00137                 '" for Cache is not registered.';
00138             throw new Robo47_Cache_Exception($message);
00139         }
00140     }
00141 
00148     public function setCache($cache)
00149     {
00150         if (is_string($cache)) {
00151             $cache = $this->_cacheFromRegistry($cache);
00152         }
00153         if (!$cache instanceof Zend_Cache_Core) {
00154             $message = 'Cache is not instance of Zend_Cache_Core';
00155             throw new Robo47_Cache_Exception($message);
00156         }
00157         $this->_cache = $cache;
00158         return $this;
00159     }
00160 
00169     protected function _doFetch($id, $testCacheValidity = true)
00170     {
00171         return $this->_cache->load($this->_prefix . $id, $testCacheValidity);
00172     }
00173 
00181     protected function _doContains($id)
00182     {
00183         return $this->_cache->test($this->_prefix . $id);
00184     }
00185 
00196     protected function _doSave($id, $data, $lifeTime = false)
00197     {
00198         return $this->_cache->save(
00199             $data,
00200             $this->_prefix . $id,
00201             $this->getTags(),
00202             $lifeTime
00203         );
00204     }
00205 
00213     protected function _doDelete($id)
00214     {
00215         return $this->_cache->remove($this->_prefix . $id);
00216     }
00217 
00223     protected function _getCacheKeys()
00224     {
00225         $ids = $this->_cache->getIds();
00226         $prefix = $this->getPrefix();
00227         $length = strlen($prefix);
00228         foreach($ids as $key => $id) {
00229             $ids[$key] = substr($id, $length);
00230         }
00231         return $ids;
00232     }
00233 }