Adding multihost - locking

Adding Unit-Tests for it
This commit is contained in:
Philipp Holzer 2018-07-04 23:37:22 +02:00
parent b07dfbb03f
commit aac94d1d74
No known key found for this signature in database
GPG key ID: 58160D7D6AF942B6
22 changed files with 741 additions and 154 deletions

View file

@ -11,7 +11,7 @@ use Friendica\Core\Cache;
* @author Hypolite Petovan <mrpetovan@gmail.com>
* @author Roland Haeder <roland@mxchange.org>
*/
class RedisCacheDriver extends BaseObject implements ICacheDriver
class RedisCacheDriver extends BaseObject implements IMemoryCacheDriver
{
/**
* @var Redis
@ -55,14 +55,21 @@ class RedisCacheDriver extends BaseObject implements ICacheDriver
return $return;
}
public function set($key, $value, $duration = Cache::MONTH)
public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
{
// We store with the hostname as key to avoid problems with other applications
return $this->redis->set(
self::getApp()->get_hostname() . ":" . $key,
serialize($value),
time() + $duration
);
if ($ttl > 0) {
return $this->redis->setex(
self::getApp()->get_hostname() . ":" . $key,
time() + $ttl,
serialize($value)
);
} else {
return $this->redis->set(
self::getApp()->get_hostname() . ":" . $key,
serialize($value)
);
}
}
public function delete($key)
@ -74,4 +81,75 @@ class RedisCacheDriver extends BaseObject implements ICacheDriver
{
return true;
}
/**
* @brief Sets a value if it's not already stored
*
* @param string $key The cache key
* @param mixed $value The old value we know from the cache
* @param int $ttl The cache lifespan, must be one of the Cache constants
* @return bool
*/
public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
{
if (!is_int($value)) {
$value = serialize($value);
}
return $this->redis->setnx(self::getApp()->get_hostname() . ":" . $key, $value);
}
/**
* @brief Compares if the old value is set and sets the new value
*
* @param string $key The cache key
* @param mixed $oldValue The old value we know
* @param mixed $newValue The new value we want to set
* @param int $ttl The cache lifespan, must be one of the Cache constants
* @return bool
*/
public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES)
{
if (!is_int($newValue)) {
$newValue = serialize($newValue);
}
$this->redis->watch(self::getApp()->get_hostname() . ":" . $key);
// If the old value isn't what we expected, somebody else changed the key meanwhile
if ($this->get($key) === $oldValue) {
if ($ttl > 0) {
$result = $this->redis->multi()
->setex(self::getApp()->get_hostname() . ":" . $ttl, $key, $newValue)
->exec();
} else {
$result = $this->redis->multi()
->set(self::getApp()->get_hostname() . ":" . $key, $newValue)
->exec();
}
return $result !== false;
}
$this->redis->unwatch();
return false;
}
/**
* @brief Compares if the old value is set and removes it
*
* @param string $key The cache key
* @param mixed $value The old value we know and want to delete
* @return bool
*/
public function compareDelete($key, $value)
{
$this->redis->watch(self::getApp()->get_hostname() . ":" . $key);
// If the old value isn't what we expected, somebody else changed the key meanwhile
if ($this->get($key) === $value) {
$result = $this->redis->multi()
->del(self::getApp()->get_hostname() . ":" . $key)
->exec();
return $result !== false;
}
$this->redis->unwatch();
return false;
}
}