2014-07-19 15:37:48 +02:00
|
|
|
<?php
|
|
|
|
|
2014-07-21 15:12:51 +02:00
|
|
|
namespace Addr\Cache;
|
2014-07-19 15:37:48 +02:00
|
|
|
|
2014-07-22 03:36:05 +02:00
|
|
|
use Predis\Client as RedisClient;
|
2014-07-19 15:37:48 +02:00
|
|
|
|
2014-07-22 03:36:05 +02:00
|
|
|
class RedisCache extends KeyValueCache
|
2014-07-21 18:48:36 +02:00
|
|
|
{
|
2014-07-19 15:37:48 +02:00
|
|
|
/**
|
|
|
|
* @var RedisClient
|
|
|
|
*/
|
|
|
|
private $redisClient;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lua script to get a value, and flag whether it was cache hit or miss.
|
2014-07-21 18:48:36 +02:00
|
|
|
* Don't delete the "== 1" - it's important in Lua
|
|
|
|
*
|
|
|
|
* @var string
|
2014-07-19 15:37:48 +02:00
|
|
|
*/
|
2014-07-21 18:48:36 +02:00
|
|
|
private $fetchLuaScript = <<< SCRIPT
|
2014-07-19 15:37:48 +02:00
|
|
|
if redis.call("exists", KEYS[1]) == 1
|
|
|
|
then
|
2014-07-22 03:36:05 +02:00
|
|
|
return {1, redis.call("get", KEYS[1])}
|
2014-07-19 15:37:48 +02:00
|
|
|
else
|
|
|
|
return {0, 0}
|
|
|
|
end
|
2014-07-21 18:48:36 +02:00
|
|
|
SCRIPT;
|
2014-07-19 15:37:48 +02:00
|
|
|
|
|
|
|
/**
|
2014-07-21 18:48:36 +02:00
|
|
|
* Constructor
|
|
|
|
*
|
2014-07-19 15:37:48 +02:00
|
|
|
* @param RedisClient $redisClient
|
2014-07-22 03:36:05 +02:00
|
|
|
* @param string $keyPrefix A prefix to prepend to all keys. This can also be
|
2014-07-19 15:37:48 +02:00
|
|
|
* set via the redis client, in which case you may wish to pass an empty string
|
|
|
|
* as $prefixKey
|
|
|
|
*/
|
2014-07-22 03:36:05 +02:00
|
|
|
public function __construct(RedisClient $redisClient, $keyPrefix = __CLASS__)
|
2014-07-21 15:12:51 +02:00
|
|
|
{
|
2014-07-19 15:37:48 +02:00
|
|
|
$this->redisClient = $redisClient;
|
2014-07-22 03:36:05 +02:00
|
|
|
parent::__construct($keyPrefix);
|
2014-07-19 15:37:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-22 03:36:05 +02:00
|
|
|
* Attempt to retrieve a value from the cache
|
2014-07-19 15:37:48 +02:00
|
|
|
*
|
2014-07-21 18:48:36 +02:00
|
|
|
* @param string $name
|
2014-07-22 03:36:05 +02:00
|
|
|
* @param int $type
|
|
|
|
* @param callable $callback
|
2014-07-19 15:37:48 +02:00
|
|
|
*/
|
2014-07-22 03:36:05 +02:00
|
|
|
public function get($name, $type, callable $callback)
|
2014-07-21 15:12:51 +02:00
|
|
|
{
|
2014-07-22 03:36:05 +02:00
|
|
|
list($wasHit, $value) = $this->redisClient->eval($this->fetchLuaScript, 1, $this->generateKey($name, $type));
|
|
|
|
|
|
|
|
if ($wasHit) {
|
|
|
|
$callback(true, $value);
|
|
|
|
return;
|
2014-07-19 15:37:48 +02:00
|
|
|
}
|
2014-07-22 03:36:05 +02:00
|
|
|
|
|
|
|
$callback(false, null);
|
2014-07-19 15:37:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-22 03:36:05 +02:00
|
|
|
* Stores a value in the cache. Overwrites the previous value if there was one.
|
2014-07-19 15:37:48 +02:00
|
|
|
*
|
2014-07-22 03:36:05 +02:00
|
|
|
* @param string $name
|
|
|
|
* @param int $type
|
|
|
|
* @param string $addr
|
|
|
|
* @param int $ttl
|
2014-07-19 15:37:48 +02:00
|
|
|
*/
|
2014-07-22 03:36:05 +02:00
|
|
|
public function store($name, $type, $addr, $ttl = null)
|
2014-07-21 15:12:51 +02:00
|
|
|
{
|
2014-07-22 03:36:05 +02:00
|
|
|
$key = $this->generateKey($name, $type);
|
2014-07-19 15:37:48 +02:00
|
|
|
|
2014-07-22 03:36:05 +02:00
|
|
|
if ($ttl > 0) {
|
|
|
|
$this->redisClient->set($key, $addr, 'EX', (int)$ttl);
|
|
|
|
} else {
|
|
|
|
$this->redisClient->set($key, $addr);
|
|
|
|
}
|
2014-07-19 15:37:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-22 03:36:05 +02:00
|
|
|
* Deletes an entry from the cache.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param int $type
|
2014-07-19 15:37:48 +02:00
|
|
|
*/
|
2014-07-22 03:36:05 +02:00
|
|
|
public function delete($name, $type)
|
2014-07-21 15:12:51 +02:00
|
|
|
{
|
2014-07-22 03:36:05 +02:00
|
|
|
$this->redisClient->del([$this->generateKey($name, $type)]);
|
2014-07-19 15:37:48 +02:00
|
|
|
}
|
|
|
|
}
|