diff --git a/cache/Cache.php b/cache/Cache.php index 672355261..654a7540e 100644 --- a/cache/Cache.php +++ b/cache/Cache.php @@ -146,7 +146,7 @@ class SS_Cache { * @param string $frontend (optional) The type of Zend_Cache frontend * @param array $frontendOptions (optional) Any frontend options to use. * - * @return Zend_Cache_Core The cache object + * @return CacheProxy */ public static function factory($for, $frontend='Output', $frontendOptions=null) { self::init(); @@ -188,8 +188,10 @@ class SS_Cache { require_once 'Zend/Cache.php'; - return Zend_Cache::factory( - $frontend, $backend[0], $frontendOptions, $backend[1] - ); + $container = Zend_Cache::factory( + $frontend, $backend[0], $frontendOptions, $backend[1] + ); + + return Injector::inst()->createWithArgs('CacheProxy', [$container]); } } diff --git a/cache/CacheProxy.php b/cache/CacheProxy.php new file mode 100644 index 000000000..3f2eafda2 --- /dev/null +++ b/cache/CacheProxy.php @@ -0,0 +1,149 @@ +container = $container; + + parent::__construct(); + } + + /** + * @param array $directives + */ + public function setDirectives($directives) + { + $this->container->setDirectives($directives); + } + + public function setConfig(Zend_Config $config) + { + return $this->container->setConfig($config); + } + + public function setBackend(Zend_Cache_Backend $backendObject) + { + return $this->container->setBackend($backendObject); + } + + public function getBackend() + { + return $this->container->getBackend(); + } + + /** + * @param string $name + * @param mixed $value + */ + public function setOption($name, $value) + { + $this->container->setOption($name, $value); + } + + public function getOption($name) + { + return $this->container->getOption($name); + } + + public function setLifetime($newLifetime) + { + return $this->container->setLifetime($newLifetime); + } + + public function getIds() + { + return $this->container->getIds(); + } + + public function getTags() + { + return $this->container->getTags(); + } + + public function getIdsMatchingTags($tags = array()) + { + return $this->container->getIdsMatchingTags($tags); + } + + public function getIdsNotMatchingTags($tags = array()) + { + return $this->container->getIdsNotMatchingTags($tags); + } + + public function getIdsMatchingAnyTags($tags = array()) + { + return $this->container->getIdsMatchingAnyTags($tags); + } + + public function getFillingPercentage() + { + return $this->container->getFillingPercentage(); + } + + public function getMetadatas($id) + { + return $this->container->getMetadatas($this->createKey($id)); + } + + public function touch($id, $extraLifetime) + { + return $this->container->touch($this->createKey($id), $extraLifetime); + } + + public function load($id, $doNotTestCacheValidity = false, $doNotUnserialize = false) + { + return $this->container->load($this->createKey($id), $doNotTestCacheValidity, $doNotUnserialize); + } + + public function test($id) + { + return $this->container->test($this->createKey($id)); + } + + public function save($data, $id = null, $tags = array(), $specificLifetime = false, $priority = 8) + { + return $this->container->save( + $data, + $this->createKey($id), + $tags, + $specificLifetime, + $priority + ); + } + + public function remove($id) + { + return $this->container->remove($this->createKey($id)); + } + + public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array()) + { + return $this->container->clean($mode, $tags); + } + + /** + * Creates a dynamic key based on versioned state + * @param $key + * @return string + */ + protected function createKey($key) + { + $state = Versioned::get_reading_mode(); + if ($state) { + return $key . '_' . md5($state); + } + return $key; + } +} \ No newline at end of file diff --git a/tests/cache/CacheTest.php b/tests/cache/CacheTest.php index 9ab385df5..5a5467d24 100644 --- a/tests/cache/CacheTest.php +++ b/tests/cache/CacheTest.php @@ -2,7 +2,12 @@ class CacheTest extends SapphireTest { - public function testCacheBasics() { + public function setUpOnce() { + parent::setUpOnce(); + Versioned::set_reading_mode('Stage.Live'); + } + + public function testCacheBasics() { $cache = SS_Cache::factory('test'); $cache->save('Good', 'cachekey'); @@ -64,5 +69,28 @@ class CacheTest extends SapphireTest { $this->assertEquals(1200, $cache->getOption('lifetime')); } + public function testVersionedCacheSegmentation() { + $cacheInstance = SS_Cache::factory('versioned'); + $cacheInstance->clean(); + + Versioned::set_reading_mode('Stage.Live'); + $result = $cacheInstance->load('test'); + $this->assertFalse($result); + $cacheInstance->save('uncle', 'test'); + $this->assertEquals('uncle', $cacheInstance->load('test')); + Versioned::set_reading_mode('Stage.Stage'); + $this->assertFalse($cacheInstance->load('test')); + $cacheInstance->save('cheese', 'test'); + $cacheInstance->save('bar', 'foo'); + $this->assertEquals('cheese', $cacheInstance->load('test')); + $this->assertEquals('bar', $cacheInstance->load('foo')); + Versioned::set_reading_mode('Stage.Live'); + $this->assertFalse($cacheInstance->load('foo')); + $this->assertEquals('uncle', $cacheInstance->load('test')); + + $cacheInstance->clean(); + + } + }