mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-16 06:23:18 +00:00
add support for redis cache
This commit is contained in:
97
lib/Alchemy/Phrasea/Cache/RedisCache.php
Normal file
97
lib/Alchemy/Phrasea/Cache/RedisCache.php
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Phraseanet
|
||||||
|
*
|
||||||
|
* (c) 2005-2010 Alchemy
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Alchemy\Phrasea\Cache;
|
||||||
|
|
||||||
|
use Doctrine\Common\Cache\AbstractCache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @package
|
||||||
|
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
|
||||||
|
* @link www.phraseanet.com
|
||||||
|
*/
|
||||||
|
class RedisCache extends AbstractCache
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Memcache
|
||||||
|
*/
|
||||||
|
private $_redis;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the redis instance to use.
|
||||||
|
*
|
||||||
|
* @param Redis $memcache
|
||||||
|
*/
|
||||||
|
public function setRedis(\Redis $redis)
|
||||||
|
{
|
||||||
|
$this->_redis = $redis;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the memcache instance used by the cache.
|
||||||
|
*
|
||||||
|
* @return Memcache
|
||||||
|
*/
|
||||||
|
public function getRedis()
|
||||||
|
{
|
||||||
|
return $this->_redis;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function getIds()
|
||||||
|
{
|
||||||
|
return $this->_redis->keys('*');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function _doFetch($id)
|
||||||
|
{
|
||||||
|
return $this->_redis->get($id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function _doContains($id)
|
||||||
|
{
|
||||||
|
return (bool) $this->_redis->get($id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function _doSave($id, $data, $lifeTime = 0)
|
||||||
|
{
|
||||||
|
if (0 === $lifeTime)
|
||||||
|
{
|
||||||
|
return $this->_redis->set($id, $data);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return $this->_redis->setex($id, $lifeTime, $data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function _doDelete($id)
|
||||||
|
{
|
||||||
|
return $this->_redis->delete($id);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
66
lib/unitTest/Alchemy/Phrasea/Cache/RedisCacheTest.php
Normal file
66
lib/unitTest/Alchemy/Phrasea/Cache/RedisCacheTest.php
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Phraseanet
|
||||||
|
*
|
||||||
|
* (c) 2005-2010 Alchemy
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @package
|
||||||
|
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
|
||||||
|
* @link www.phraseanet.com
|
||||||
|
*/
|
||||||
|
require_once __DIR__ . '/../../../PhraseanetPHPUnitAbstract.class.inc';
|
||||||
|
|
||||||
|
class RedisTest extends \PhraseanetPHPUnitAbstract
|
||||||
|
{
|
||||||
|
|
||||||
|
public function testBasics()
|
||||||
|
{
|
||||||
|
if (extension_loaded('memcached'))
|
||||||
|
{
|
||||||
|
$redis = new Redis();
|
||||||
|
$ok = @$redis->connect('127.0.0.1', 6379);
|
||||||
|
if (!$ok)
|
||||||
|
{
|
||||||
|
$this->markTestSkipped('The ' . __CLASS__ . ' requires the use of redis');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$this->markTestSkipped('The ' . __CLASS__ . ' requires the use of redis');
|
||||||
|
}
|
||||||
|
|
||||||
|
$cache = new \Alchemy\Phrasea\Cache\RedisCache();
|
||||||
|
$cache->setRedis($redis);
|
||||||
|
// Test save
|
||||||
|
$cache->save('test_key', 'testing this out');
|
||||||
|
|
||||||
|
// Test contains to test that save() worked
|
||||||
|
$this->assertTrue($cache->contains('test_key'));
|
||||||
|
|
||||||
|
$cache->save('test_key1', 'testing this out', 20);
|
||||||
|
|
||||||
|
// Test contains to test that save() worked
|
||||||
|
$this->assertTrue($cache->contains('test_key1'));
|
||||||
|
|
||||||
|
// Test fetch
|
||||||
|
$this->assertEquals('testing this out', $cache->fetch('test_key'));
|
||||||
|
|
||||||
|
// Test delete
|
||||||
|
$cache->save('test_key2', 'test2');
|
||||||
|
$cache->delete('test_key2');
|
||||||
|
$this->assertFalse($cache->contains('test_key2'));
|
||||||
|
|
||||||
|
$ids = $cache->getIds();
|
||||||
|
$this->assertTrue(in_array('test_key', $ids));
|
||||||
|
|
||||||
|
$this->assertEquals($redis, $cache->getRedis());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user