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;
|
|
|
|
|
2016-07-27 13:18:52 -04:00
|
|
|
use Aviat\Ion\ConfigInterface;
|
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
|
|
|
|
|
|
|
/**
|
2016-07-27 13:18:52 -04:00
|
|
|
* THe Predis library instance
|
|
|
|
*
|
|
|
|
* @var Client
|
2016-04-08 14:25:45 -04:00
|
|
|
*/
|
|
|
|
protected $redis;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the Redis cache driver
|
2016-07-27 13:18:52 -04:00
|
|
|
*
|
|
|
|
* @param ConfigInterface $config The configuration management class
|
2016-04-08 14:25:45 -04:00
|
|
|
*/
|
2016-07-27 13:18:52 -04:00
|
|
|
public function __construct(ConfigInterface $config)
|
2016-04-08 14:25:45 -04:00
|
|
|
{
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-27 13:18:52 -04:00
|
|
|
* Retrieve a value from the cache backend
|
2016-04-08 14:25:45 -04:00
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function get($key)
|
|
|
|
{
|
2016-07-28 10:44:13 -04:00
|
|
|
return json_decode($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-28 10:44:13 -04:00
|
|
|
$this->redis->set($key, json_encode($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
|