Add Redis Cache driver
This commit is contained in:
parent
454324aba6
commit
4b4a259f8a
@ -14,7 +14,7 @@ show_anime_collection = true
|
||||
# do you wish to show the manga collection?
|
||||
show_manga_collection = false
|
||||
|
||||
# cache driver for api calls (SQLDriver, RedisDriver, MemcacheDriver)
|
||||
# cache driver for api calls (SQLDriver, RedisDriver)
|
||||
cache_driver = SQLDriver
|
||||
|
||||
# path to public directory on the server
|
||||
|
15
app/config/redis.toml.example
Normal file
15
app/config/redis.toml.example
Normal file
@ -0,0 +1,15 @@
|
||||
################################################################################
|
||||
# Redis Cache Configuration #
|
||||
################################################################################
|
||||
|
||||
# Host or socket to connect to
|
||||
host = "127.0.0.1"
|
||||
|
||||
# Connection port
|
||||
#port = 6379
|
||||
|
||||
# Connection password
|
||||
#password = ""
|
||||
|
||||
# Database number
|
||||
database = 13
|
@ -55,10 +55,11 @@ class AnimeCollection extends DB {
|
||||
|
||||
// Is database valid? If not, set a flag so the
|
||||
// app can be run without a valid database
|
||||
$db_file_name = $this->db_config['collection']['file'];
|
||||
if ($db_file_name !== ':memory:')
|
||||
if ($this->db_config['collection']['type'] === 'sqlite')
|
||||
{
|
||||
if (file_exists($db_file_name))
|
||||
$db_file_name = $this->db_config['collection']['file'];
|
||||
|
||||
if ($db_file_name !== ':memory:' && file_exists($db_file_name))
|
||||
{
|
||||
$db_file = file_get_contents($db_file_name);
|
||||
$this->valid_database = (strpos($db_file, 'SQLite format 3') === 0);
|
||||
|
@ -4,10 +4,10 @@
|
||||
*
|
||||
* Building blocks for web development
|
||||
*
|
||||
* @package Ion
|
||||
* @author Timothy J. Warren
|
||||
* @package Ion
|
||||
* @author Timothy J. Warren
|
||||
* @copyright Copyright (c) 2015 - 2016
|
||||
* @license MIT
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
namespace Aviat\Ion\Cache;
|
||||
@ -16,36 +16,36 @@ namespace Aviat\Ion\Cache;
|
||||
* Interface for cache drivers
|
||||
*/
|
||||
interface CacheDriverInterface {
|
||||
/**
|
||||
* Retreive a value from the cache backend
|
||||
*
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key);
|
||||
/**
|
||||
* Retreive a value from the cache backend
|
||||
*
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key);
|
||||
|
||||
/**
|
||||
* Set a cached value
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @return CacheDriverInterface
|
||||
*/
|
||||
public function set($key, $value);
|
||||
/**
|
||||
* Set a cached value
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @return CacheDriverInterface
|
||||
*/
|
||||
public function set($key, $value);
|
||||
|
||||
/**
|
||||
* Invalidate a cached value
|
||||
*
|
||||
* @param string $key
|
||||
* @return CacheDriverInterface
|
||||
*/
|
||||
public function invalidate($key);
|
||||
/**
|
||||
* Invalidate a cached value
|
||||
*
|
||||
* @param string $key
|
||||
* @return CacheDriverInterface
|
||||
*/
|
||||
public function invalidate($key);
|
||||
|
||||
/**
|
||||
* Clear the contents of the cache
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function invalidateAll();
|
||||
/**
|
||||
* Clear the contents of the cache
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function invalidateAll();
|
||||
}
|
||||
// End of CacheDriverInterface.php
|
108
src/Aviat/Ion/Cache/Driver/RedisDriver.php
Normal file
108
src/Aviat/Ion/Cache/Driver/RedisDriver.php
Normal file
@ -0,0 +1,108 @@
|
||||
<?php
|
||||
/**
|
||||
* Ion
|
||||
*
|
||||
* Building blocks for web development
|
||||
*
|
||||
* @package Ion
|
||||
* @author Timothy J. Warren
|
||||
* @copyright Copyright (c) 2015 - 2016
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
namespace Aviat\Ion\Cache\Driver;
|
||||
|
||||
use Aviat\Ion\Di\ContainerInterface;
|
||||
|
||||
class RedisDriver implements \Aviat\Ion\Cache\CacheDriverInterface {
|
||||
|
||||
/**
|
||||
* The redis extension class instance
|
||||
* #var Redis
|
||||
*/
|
||||
protected $redis;
|
||||
|
||||
/**
|
||||
* Create the Redis cache driver
|
||||
*/
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$config = $container->get('config');
|
||||
$redisConfig = $config->get('redis');
|
||||
|
||||
$this->redis = new \Redis();
|
||||
|
||||
(array_key_exists('port', $redisConfig))
|
||||
? $this->redis->pconnect($redisConfig['host'], $redisConfig['port'])
|
||||
: $this->redis->pconnect($redisConfig['host']);
|
||||
|
||||
// If there is a password, authorize
|
||||
if (array_key_exists('password', $redisConfig))
|
||||
{
|
||||
$this->redis->auth($redisConfig['password']);
|
||||
}
|
||||
|
||||
// If there is a database selected, connect to the specified database
|
||||
if (array_key_exists('database', $redisConfig))
|
||||
{
|
||||
$this->redis->select($redisConfig['database']);
|
||||
}
|
||||
|
||||
$this->redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor to disconnect from redis
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->redis->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retreive a value from the cache backend
|
||||
*
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key)
|
||||
{
|
||||
return $this->redis->get($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a cached value
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @return CacheDriverInterface
|
||||
*/
|
||||
public function set($key, $value)
|
||||
{
|
||||
$this->redis->set($key, $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidate a cached value
|
||||
*
|
||||
* @param string $key
|
||||
* @return CacheDriverInterface
|
||||
*/
|
||||
public function invalidate($key)
|
||||
{
|
||||
$this->redis->del($key);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the contents of the cache
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function invalidateAll()
|
||||
{
|
||||
$this->redis->flushDB();
|
||||
}
|
||||
}
|
||||
// End of RedisDriver.php
|
@ -72,14 +72,7 @@ class SQLDriver extends DB implements \Aviat\Ion\Cache\CacheDriverInterface {
|
||||
'value' => serialize($value),
|
||||
]);
|
||||
|
||||
/*if ( ! empty($this->get($key)))
|
||||
{
|
||||
$this->db->update('cache');
|
||||
}
|
||||
else*/
|
||||
{
|
||||
$this->db->insert('cache');
|
||||
}
|
||||
$this->db->insert('cache');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
@ -71,6 +71,10 @@ class AnimeClient_TestCase extends PHPUnit_Framework_TestCase {
|
||||
'routes' => [
|
||||
|
||||
]
|
||||
],
|
||||
'redis' => [
|
||||
'host' => 'localhost',
|
||||
'database' => 13
|
||||
]
|
||||
];
|
||||
|
||||
|
18
tests/Ion/Cache/Driver/RedisDriverTest.php
Normal file
18
tests/Ion/Cache/Driver/RedisDriverTest.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
require_once('CacheDriverBase.php');
|
||||
|
||||
use Aviat\Ion\Friend;
|
||||
use Aviat\Ion\Cache\Driver\RedisDriver;
|
||||
|
||||
class CacheRedisDriverTest extends AnimeClient_TestCase {
|
||||
use CacheDriverBase;
|
||||
|
||||
protected $driver;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->driver = new RedisDriver($this->container);
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
require('CacheDriverBase.php');
|
||||
require_once('CacheDriverBase.php');
|
||||
|
||||
use Aviat\Ion\Friend;
|
||||
use Aviat\Ion\Cache\Driver\SQLDriver;
|
||||
|
Loading…
Reference in New Issue
Block a user