Add new CacheProxy

This commit is contained in:
Aaron Carlino 2018-05-25 13:22:48 +12:00 committed by Damian Mooyman
parent e987708470
commit 05384df10b
3 changed files with 184 additions and 5 deletions

10
cache/Cache.php vendored
View File

@ -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]);
}
}

149
cache/CacheProxy.php vendored Normal file
View File

@ -0,0 +1,149 @@
<?php
require_once 'Zend/Cache.php';
class CacheProxy extends Zend_Cache_Core
{
/**
* @var Zend_Cache_Backend|Zend_Cache_Backend_ExtendedInterface
*/
protected $container;
/**
* CacheProxy constructor.
* @param Zend_Cache_Core $container
*/
public function __construct(Zend_Cache_Core $container)
{
$this->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;
}
}

View File

@ -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();
}
}