mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-12 20:43:25 +00:00
Refactor Cache services
This commit is contained in:
@@ -22,9 +22,34 @@ use Doctrine\Common\Cache\ApcCache as DoctrineApc;
|
||||
class ApcCache extends DoctrineApc implements Cache
|
||||
{
|
||||
|
||||
public function flushAll()
|
||||
public function isServer()
|
||||
{
|
||||
return apc_clear_cache() && apc_clear_cache('user');
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getStats()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function get($key)
|
||||
{
|
||||
if (!$this->contains($key))
|
||||
{
|
||||
throw new Exception('Unable to retrieve the value');
|
||||
}
|
||||
|
||||
return $this->fetch($key);
|
||||
}
|
||||
|
||||
public function deleteMulti(array $array_keys)
|
||||
{
|
||||
foreach ($array_keys as $id)
|
||||
{
|
||||
$this->delete($id);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
@@ -22,10 +22,35 @@ use Doctrine\Common\Cache\ArrayCache as DoctrineArray;
|
||||
class ArrayCache extends DoctrineArray implements Cache
|
||||
{
|
||||
|
||||
public function flushAll()
|
||||
public function isServer()
|
||||
{
|
||||
$this->data = array();
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getStats()
|
||||
{
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function get($id)
|
||||
{
|
||||
if (!$this->contains($id))
|
||||
{
|
||||
throw new Exception(sprintf('Unable to find key %s', $id));
|
||||
}
|
||||
return $this->fetch($id);
|
||||
}
|
||||
|
||||
public function deleteMulti(array $array_keys)
|
||||
{
|
||||
foreach ($array_keys as $id)
|
||||
{
|
||||
$this->delete($id);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
@@ -22,6 +22,12 @@ use Doctrine\Common\Cache\Cache as DoctrineCache;
|
||||
interface Cache extends DoctrineCache
|
||||
{
|
||||
|
||||
public function flushAll();
|
||||
public function isServer();
|
||||
|
||||
public function getStats();
|
||||
|
||||
public function get($key);
|
||||
|
||||
public function deleteMulti(array $array_keys);
|
||||
|
||||
}
|
22
lib/Alchemy/Phrasea/Cache/Exception.php
Normal file
22
lib/Alchemy/Phrasea/Cache/Exception.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Phraseanet
|
||||
*
|
||||
* (c) 2005-2012 Alchemy
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Alchemy\Phrasea\Cache;
|
||||
|
||||
/**
|
||||
*
|
||||
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
|
||||
* @link www.phraseanet.com
|
||||
*/
|
||||
class Exception extends \Exception
|
||||
{
|
||||
|
||||
}
|
@@ -11,7 +11,9 @@
|
||||
|
||||
namespace Alchemy\Phrasea\Cache;
|
||||
|
||||
use Alchemy\Phrasea\Core\Configuration\Parser as FileParser;
|
||||
use Alchemy\Phrasea\Core\Configuration\Parser as FileParser,
|
||||
\Alchemy\Phrasea\Core\Service\Builder,
|
||||
\Alchemy\Phrasea\Core;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -27,6 +29,7 @@ class Manager
|
||||
* @var \SplFileObject
|
||||
*/
|
||||
protected $cacheFile;
|
||||
protected $core;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -40,32 +43,67 @@ class Manager
|
||||
*/
|
||||
protected $registry = array();
|
||||
|
||||
public function __construct(\SplFileObject $file, FileParser $parser)
|
||||
public function __construct(Core $core, \SplFileObject $file, FileParser $parser)
|
||||
{
|
||||
$this->cacheFile = $file;
|
||||
$this->parser = $parser;
|
||||
$this->core = $core;
|
||||
|
||||
$this->registry = $parser->parse($file);
|
||||
}
|
||||
|
||||
public function exists($name)
|
||||
protected function exists($name)
|
||||
{
|
||||
return isset($this->registry[$name]);
|
||||
}
|
||||
|
||||
public function get($name)
|
||||
public function flushAll()
|
||||
{
|
||||
return $this->exists($name) ?
|
||||
$this->registry[$name] : null;
|
||||
foreach ($this->registry as $cacheKey => $service_name)
|
||||
{
|
||||
$this->get($cacheKey, $service_name)->getDriver()->deleteAll();
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasChange($name, $driver)
|
||||
public function get($cacheKey, $service_name)
|
||||
{
|
||||
if (!$this->exists($cacheKey))
|
||||
{
|
||||
$this->registry[$cacheKey] = $service_name;
|
||||
}
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
$configuration = $this->core->getConfiguration()->getService($service_name);
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
$configuration = new \Symfony\Component\DependencyInjection\ParameterBag\ParameterBag(
|
||||
array('type' => 'Cache\\ArrayCache')
|
||||
);
|
||||
}
|
||||
|
||||
$driver = Builder::create($this->core, $service_name, $configuration);
|
||||
|
||||
if ($this->hasChange($cacheKey, $service_name))
|
||||
{
|
||||
$driver->getDriver()->deleteAll();
|
||||
$this->save($cacheKey, $service_name);
|
||||
}
|
||||
|
||||
return $driver;
|
||||
}
|
||||
|
||||
protected function hasChange($name, $driver)
|
||||
{
|
||||
return $this->exists($name) ?
|
||||
$this->registry[$name] !== $driver : true;
|
||||
}
|
||||
|
||||
public function save($name, $driver)
|
||||
protected function save($name, $driver)
|
||||
{
|
||||
$date = new \DateTime();
|
||||
|
||||
|
@@ -11,7 +11,7 @@
|
||||
|
||||
namespace Alchemy\Phrasea\Cache;
|
||||
|
||||
use Doctrine\Common\Cache\MemcacheCache as DoctrineMemcache;
|
||||
use \Doctrine\Common\Cache\MemcacheCache as DoctrineMemcache;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -22,9 +22,34 @@ use Doctrine\Common\Cache\MemcacheCache as DoctrineMemcache;
|
||||
class MemcacheCache extends DoctrineMemcache implements Cache
|
||||
{
|
||||
|
||||
public function flushAll()
|
||||
public function isServer()
|
||||
{
|
||||
return $this->getMemcache()->flush();
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getStats()
|
||||
{
|
||||
return $this->getMemcache()->getstats();
|
||||
}
|
||||
|
||||
public function get($key)
|
||||
{
|
||||
if (!$this->contains($key))
|
||||
{
|
||||
throw new Exception('Unable to retrieve the value');
|
||||
}
|
||||
|
||||
return $this->fetch($key);
|
||||
}
|
||||
|
||||
public function deleteMulti(array $array_keys)
|
||||
{
|
||||
foreach ($array_keys as $id)
|
||||
{
|
||||
$this->delete($id);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
@@ -94,9 +94,34 @@ class RedisCache extends AbstractCache implements Cache
|
||||
return $this->_redis->delete($id);
|
||||
}
|
||||
|
||||
public function flushAll()
|
||||
public function isServer()
|
||||
{
|
||||
return $this->_redis->flushAll();
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getStats()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function get($key)
|
||||
{
|
||||
if (!$this->contains($key))
|
||||
{
|
||||
throw new Exception('Unable to retrieve the value');
|
||||
}
|
||||
|
||||
return $this->fetch($key);
|
||||
}
|
||||
|
||||
public function deleteMulti(array $array_keys)
|
||||
{
|
||||
foreach ($array_keys as $id)
|
||||
{
|
||||
$this->delete($id);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -22,13 +22,34 @@ use Doctrine\Common\Cache\XcacheCache as DoctrineXcache;
|
||||
class XcacheCache extends DoctrineXcache implements Cache
|
||||
{
|
||||
|
||||
public function flushAll()
|
||||
public function isServer()
|
||||
{
|
||||
$this->_checkAuth();
|
||||
return false;
|
||||
}
|
||||
|
||||
xcache_clear_cache(XC_TYPE_VAR, 0);
|
||||
public function getStats()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return true;
|
||||
public function get($key)
|
||||
{
|
||||
if (!$this->contains($key))
|
||||
{
|
||||
throw new Exception('Unable to retrieve the value');
|
||||
}
|
||||
|
||||
return $this->fetch($key);
|
||||
}
|
||||
|
||||
public function deleteMulti(array $array_keys)
|
||||
{
|
||||
foreach ($array_keys as $id)
|
||||
{
|
||||
$this->delete($id);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
49
lib/unitTest/Alchemy/Phrasea/Cache/ApcCacheTest.php
Normal file
49
lib/unitTest/Alchemy/Phrasea/Cache/ApcCacheTest.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../../../PhraseanetPHPUnitAbstract.class.inc';
|
||||
|
||||
/**
|
||||
* Test class for ApcCache.
|
||||
* Generated by PHPUnit on 2012-02-21 at 16:39:56.
|
||||
*/
|
||||
class ApcCacheTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @var ApcCache
|
||||
*/
|
||||
protected $object;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->object = new \Alchemy\Phrasea\Cache\ApcCache;
|
||||
}
|
||||
|
||||
public function testIsServer()
|
||||
{
|
||||
$this->assertTrue(is_bool($this->object->isServer()));
|
||||
}
|
||||
|
||||
public function testGetStats()
|
||||
{
|
||||
$this->assertTrue(is_array($this->object->getStats()) || is_null($this->object->getStats()));
|
||||
}
|
||||
|
||||
public function testGet()
|
||||
{
|
||||
// Remove the following lines when you implement this test.
|
||||
$this->markTestIncomplete(
|
||||
'This test has not been implemented yet.'
|
||||
);
|
||||
}
|
||||
|
||||
public function testDeleteMulti()
|
||||
{
|
||||
// Remove the following lines when you implement this test.
|
||||
$this->markTestIncomplete(
|
||||
'This test has not been implemented yet.'
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
53
lib/unitTest/Alchemy/Phrasea/Cache/ArrayCacheTest.php
Normal file
53
lib/unitTest/Alchemy/Phrasea/Cache/ArrayCacheTest.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../../../PhraseanetPHPUnitAbstract.class.inc';
|
||||
|
||||
/**
|
||||
* Test class for ArrayCache.
|
||||
* Generated by PHPUnit on 2012-02-21 at 16:37:10.
|
||||
*/
|
||||
class ArrayCacheTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @var ArrayCache
|
||||
*/
|
||||
protected $object;
|
||||
|
||||
/**
|
||||
* Sets up the fixture, for example, opens a network connection.
|
||||
* This method is called before a test is executed.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
$this->object = new \Alchemy\Phrasea\Cache\ArrayCache;
|
||||
}
|
||||
|
||||
public function testIsServer()
|
||||
{
|
||||
$this->assertTrue(is_bool($this->object->isServer()));
|
||||
}
|
||||
|
||||
public function testGetStats()
|
||||
{
|
||||
$this->assertTrue(is_array($this->object->getStats()) || is_null($this->object->getStats()));
|
||||
}
|
||||
|
||||
public function testGet()
|
||||
{
|
||||
// Remove the following lines when you implement this test.
|
||||
$this->markTestIncomplete(
|
||||
'This test has not been implemented yet.'
|
||||
);
|
||||
}
|
||||
|
||||
public function testDeleteMulti()
|
||||
{
|
||||
// Remove the following lines when you implement this test.
|
||||
$this->markTestIncomplete(
|
||||
'This test has not been implemented yet.'
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
51
lib/unitTest/Alchemy/Phrasea/Cache/ManagerTest.php
Normal file
51
lib/unitTest/Alchemy/Phrasea/Cache/ManagerTest.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../../../PhraseanetPHPUnitAbstract.class.inc';
|
||||
|
||||
/**
|
||||
* Test class for Manager.
|
||||
* Generated by PHPUnit on 2012-02-21 at 16:37:11.
|
||||
*/
|
||||
class ManagerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @var Manager
|
||||
*/
|
||||
protected $object;
|
||||
|
||||
/**
|
||||
* Sets up the fixture, for example, opens a network connection.
|
||||
* This method is called before a test is executed.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
$this->object = new \Alchemy\Phrasea\Cache\Manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers {className}::{origMethodName}
|
||||
* @todo Implement testFlushAll().
|
||||
*/
|
||||
public function testFlushAll()
|
||||
{
|
||||
// Remove the following lines when you implement this test.
|
||||
$this->markTestIncomplete(
|
||||
'This test has not been implemented yet.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers {className}::{origMethodName}
|
||||
* @todo Implement testGet().
|
||||
*/
|
||||
public function testGet()
|
||||
{
|
||||
// Remove the following lines when you implement this test.
|
||||
$this->markTestIncomplete(
|
||||
'This test has not been implemented yet.'
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
66
lib/unitTest/Alchemy/Phrasea/Cache/MemcacheCacheTest.php
Normal file
66
lib/unitTest/Alchemy/Phrasea/Cache/MemcacheCacheTest.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../../../PhraseanetPHPUnitAbstract.class.inc';
|
||||
|
||||
/**
|
||||
* Test class for MemcacheCache.
|
||||
* Generated by PHPUnit on 2012-02-21 at 16:37:11.
|
||||
*/
|
||||
class MemcacheCacheTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @var MemcacheCache
|
||||
*/
|
||||
protected $object;
|
||||
|
||||
/**
|
||||
* Sets up the fixture, for example, opens a network connection.
|
||||
* This method is called before a test is executed.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
$this->object = new \Alchemy\Phrasea\Cache\MemcacheCache;
|
||||
|
||||
if(!class_exists('Memcache'))
|
||||
{
|
||||
$this->markTestSkipped('No memcache extension');
|
||||
}
|
||||
|
||||
$memcache = new Memcache();
|
||||
if(!@$memcache->connect('localhost', 11211))
|
||||
{
|
||||
$this->markTestSkipped('No memcache server');
|
||||
}
|
||||
|
||||
$this->object->setMemcache($memcache);
|
||||
}
|
||||
|
||||
public function testIsServer()
|
||||
{
|
||||
$this->assertTrue(is_bool($this->object->isServer()));
|
||||
}
|
||||
|
||||
public function testGetStats()
|
||||
{
|
||||
$this->assertTrue(is_array($this->object->getStats()) || is_null($this->object->getStats()));
|
||||
}
|
||||
|
||||
public function testGet()
|
||||
{
|
||||
// Remove the following lines when you implement this test.
|
||||
$this->markTestIncomplete(
|
||||
'This test has not been implemented yet.'
|
||||
);
|
||||
}
|
||||
|
||||
public function testDeleteMulti()
|
||||
{
|
||||
// Remove the following lines when you implement this test.
|
||||
$this->markTestIncomplete(
|
||||
'This test has not been implemented yet.'
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
53
lib/unitTest/Alchemy/Phrasea/Cache/XcacheCacheTest.php
Normal file
53
lib/unitTest/Alchemy/Phrasea/Cache/XcacheCacheTest.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../../../PhraseanetPHPUnitAbstract.class.inc';
|
||||
|
||||
/**
|
||||
* Test class for XcacheCache.
|
||||
* Generated by PHPUnit on 2012-02-21 at 16:39:57.
|
||||
*/
|
||||
class XcacheCacheTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @var XcacheCache
|
||||
*/
|
||||
protected $object;
|
||||
|
||||
/**
|
||||
* Sets up the fixture, for example, opens a network connection.
|
||||
* This method is called before a test is executed.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
$this->object = new \Alchemy\Phrasea\Cache\XcacheCache;
|
||||
}
|
||||
|
||||
public function testIsServer()
|
||||
{
|
||||
$this->assertTrue(is_bool($this->object->isServer()));
|
||||
}
|
||||
|
||||
public function testGetStats()
|
||||
{
|
||||
$this->assertTrue(is_array($this->object->getStats()) || is_null($this->object->getStats()));
|
||||
}
|
||||
|
||||
public function testGet()
|
||||
{
|
||||
// Remove the following lines when you implement this test.
|
||||
$this->markTestIncomplete(
|
||||
'This test has not been implemented yet.'
|
||||
);
|
||||
}
|
||||
|
||||
public function testDeleteMulti()
|
||||
{
|
||||
// Remove the following lines when you implement this test.
|
||||
$this->markTestIncomplete(
|
||||
'This test has not been implemented yet.'
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user