2016-04-08 14:25:45 -04:00
|
|
|
<?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;
|
2016-07-22 17:22:00 -04:00
|
|
|
use Aviat\Ion\Cache\CacheDriverInterface;
|
2016-04-08 14:25:45 -04:00
|
|
|
|
2016-07-22 17:22:00 -04:00
|
|
|
use Predis\Client;
|
|
|
|
|
|
|
|
class RedisDriver implements CacheDriverInterface {
|
2016-04-08 14:25:45 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The redis extension class instance
|
2016-04-08 18:05:52 -04:00
|
|
|
* @var Redis
|
2016-04-08 14:25:45 -04:00
|
|
|
*/
|
|
|
|
protected $redis;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the Redis cache driver
|
|
|
|
*/
|
|
|
|
public function __construct(ContainerInterface $container)
|
|
|
|
{
|
|
|
|
$config = $container->get('config');
|
|
|
|
$redisConfig = $config->get('redis');
|
2016-07-22 17:22:00 -04:00
|
|
|
|
|
|
|
if (array_key_exists('password', $redisConfig) && $redisConfig['password'] === '')
|
2016-04-08 14:25:45 -04:00
|
|
|
{
|
2016-07-22 17:22:00 -04:00
|
|
|
unset($redisConfig['password']);
|
2016-04-08 14:25:45 -04:00
|
|
|
}
|
|
|
|
|
2016-07-22 17:22:00 -04:00
|
|
|
$this->redis = new Client($redisConfig);
|
2016-04-08 14:25:45 -04:00
|
|
|
}
|
2016-07-22 17:22:00 -04:00
|
|
|
|
2016-04-08 14:25:45 -04:00
|
|
|
/**
|
2016-07-22 17:22:00 -04:00
|
|
|
* Disconnect from redis
|
2016-04-08 14:25:45 -04:00
|
|
|
*/
|
|
|
|
public function __destruct()
|
|
|
|
{
|
2016-07-22 17:22:00 -04:00
|
|
|
$this->redis = null;
|
2016-04-08 14:25:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retreive a value from the cache backend
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function get($key)
|
|
|
|
{
|
2016-07-22 17:22:00 -04:00
|
|
|
return unserialize($this->redis->get($key));
|
2016-04-08 14:25:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a cached value
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param mixed $value
|
|
|
|
* @return CacheDriverInterface
|
|
|
|
*/
|
|
|
|
public function set($key, $value)
|
|
|
|
{
|
2016-07-22 17:22:00 -04:00
|
|
|
$this->redis->set($key, serialize($value));
|
2016-04-08 14:25:45 -04:00
|
|
|
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
|