HummingBirdAnimeClient/src/Aviat/Ion/Cache/Driver/RedisDriver.php

117 lines
2.1 KiB
PHP
Raw Normal View History

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\ConfigInterface;
2016-04-08 14:25:45 -04:00
use Predis\Client;
/**
* Cache Driver for a Redis backend
*/
class RedisDriver implements DriverInterface {
use DriverTrait;
2016-04-08 14:25:45 -04:00
/**
* THe Predis library instance
*
* @var Client
2016-04-08 14:25:45 -04:00
*/
protected $redis;
/**
* Create the Redis cache driver
*
* @param ConfigInterface $config The configuration management class
2016-04-08 14:25:45 -04:00
*/
public function __construct(ConfigInterface $config)
2016-04-08 14:25:45 -04:00
{
$redisConfig = $config->get('redis');
// If you don't have a redis password set, and you attempt to send an
// empty string, Redis will think you want to authenticate with a password
// that is an empty string. To work around this, empty string passwords
// are considered to be a lack of a password
if (array_key_exists('password', $redisConfig) && $redisConfig['password'] === '')
2016-04-08 14:25:45 -04:00
{
unset($redisConfig['password']);
2016-04-08 14:25:45 -04:00
}
$this->redis = new Client($redisConfig);
2016-04-08 14:25:45 -04:00
}
2016-04-08 14:25:45 -04:00
/**
* Disconnect from redis
2016-04-08 14:25:45 -04:00
*/
public function __destruct()
{
$this->redis = null;
2016-04-08 14:25:45 -04:00
}
/**
* Retrieve a value from the cache backend
2016-04-08 14:25:45 -04:00
*
* @param string $rawKey
2016-04-08 14:25:45 -04:00
* @return mixed
*/
public function get($rawKey)
2016-04-08 14:25:45 -04:00
{
$key = $this->prefix($rawKey);
$serializedData = $this->redis->get($key);
return $this->unserialize($serializedData);
2016-04-08 14:25:45 -04:00
}
/**
* Set a cached value
*
* @param string $rawKey
2016-04-08 14:25:45 -04:00
* @param mixed $value
* @return DriverInterface
2016-04-08 14:25:45 -04:00
*/
public function set($rawKey, $value)
2016-04-08 14:25:45 -04:00
{
$key = $this->prefix($rawKey);
$serializedData = $this->serialize($value);
$this->redis->set($key, $serializedData);
2016-04-08 14:25:45 -04:00
return $this;
}
/**
* Invalidate a cached value
*
* @param string $rawKey
* @return DriverInterface
2016-04-08 14:25:45 -04:00
*/
public function invalidate($rawKey)
2016-04-08 14:25:45 -04:00
{
$key = $this->prefix($rawKey);
2016-04-08 14:25:45 -04:00
$this->redis->del($key);
2016-04-08 14:25:45 -04:00
return $this;
}
/**
* Clear the contents of the cache
*
* @return void
*/
public function invalidateAll()
{
$this->redis->flushdb();
2016-04-08 14:25:45 -04:00
}
}
// End of RedisDriver.php