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

@ -0,0 +1,83 @@
<?php
namespace Friendica\Core\Cache;
use Friendica\Core\Cache;
/**
* @brief Implementation of the IMemoryCacheDriver mainly for testing purpose
*
* Class ArrayCache
*
* @package Friendica\Core\Cache
*/
class ArrayCache implements IMemoryCacheDriver
{
use TraitCompareDelete;
/** @var array Array with the cached data */
protected $cachedData = array();
/**
* (@inheritdoc)
*/
public function get($key)
{
if (isset($this->cachedData[$key])) {
return $this->cachedData[$key];
}
return null;
}
/**
* (@inheritdoc)
*/
public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
{
$this->cachedData[$key] = $value;
return true;
}
/**
* (@inheritdoc)
*/
public function delete($key)
{
unset($this->cachedData[$key]);
return true;
}
/**
* (@inheritdoc)
*/
public function clear()
{
$this->cachedData = [];
return true;
}
/**
* (@inheritdoc)
*/
public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
{
if (isset($this->cachedData[$key])) {
return false;
} else {
return $this->set($key, $value, $ttl);
}
}
/**
* (@inheritdoc)
*/
public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES)
{
if ($this->get($key) === $oldValue) {
return $this->set($key, $newValue);
} else {
return false;
}
}
}

View file

@ -33,11 +33,11 @@ class DatabaseCacheDriver implements ICacheDriver
return null;
}
public function set($key, $value, $duration = Cache::MONTH)
public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
{
$fields = [
'v' => serialize($value),
'expires' => DateTimeFormat::utc('now + ' . $duration . ' seconds'),
'expires' => DateTimeFormat::utc('now + ' . $ttl . ' seconds'),
'updated' => DateTimeFormat::utcNow()
];

View file

@ -12,7 +12,7 @@ use Friendica\Core\Cache;
interface ICacheDriver
{
/**
* Fetches cached data according to the key
* @brief Fetches cached data according to the key
*
* @param string $key The key to the cached data
*
@ -21,28 +21,27 @@ interface ICacheDriver
public function get($key);
/**
* Stores data in the cache identified by the key. The input $value can have multiple formats.
* @brief Stores data in the cache identified by the key. The input $value can have multiple formats.
*
* @param string $key The cache key
* @param mixed $value The value to store
* @param integer $duration The cache lifespan, must be one of the Cache constants
* @param integer $ttl The cache lifespan, must be one of the Cache constants
*
* @return bool
*/
public function set($key, $value, $duration = Cache::MONTH);
public function set($key, $value, $ttl = Cache::FIVE_MINUTES);
/**
* Delete a key from the cache
* @brief Delete a key from the cache
*
* @param string $key
* @param string $key The cache key
*
* @return bool
*/
public function delete($key);
/**
* Remove outdated data from the cache
* @brief Remove outdated data from the cache
*
* @return bool
*/

View file

@ -0,0 +1,45 @@
<?php
namespace Friendica\Core\Cache;
use Friendica\Core\Cache;
/**
* @brief This interface defines methods for Memory-Caches only
*
* Interface IMemoryCacheDriver
*
* @package Friendica\Core\Cache
*/
interface IMemoryCacheDriver extends ICacheDriver
{
/**
* @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);
/**
* @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 from the cache
* @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);
/**
* @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);
}

View file

@ -10,10 +10,13 @@ use Friendica\Core\Cache;
*
* @author Hypolite Petovan <mrpetovan@gmail.com>
*/
class MemcacheCacheDriver extends BaseObject implements ICacheDriver
class MemcacheCacheDriver extends BaseObject implements IMemoryCacheDriver
{
use TraitCompareSet;
use TraitCompareDelete;
/**
* @var Memcache
* @var \Memcache
*/
private $memcache;
@ -30,6 +33,9 @@ class MemcacheCacheDriver extends BaseObject implements ICacheDriver
}
}
/**
* (@inheritdoc)
*/
public function get($key)
{
$return = null;
@ -54,17 +60,31 @@ class MemcacheCacheDriver extends BaseObject implements ICacheDriver
return $return;
}
public function set($key, $value, $duration = Cache::MONTH)
/**
* (@inheritdoc)
*/
public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
{
// We store with the hostname as key to avoid problems with other applications
return $this->memcache->set(
self::getApp()->get_hostname() . ":" . $key,
serialize($value),
MEMCACHE_COMPRESSED,
time() + $duration
);
if ($ttl > 0) {
return $this->memcache->set(
self::getApp()->get_hostname() . ":" . $key,
serialize($value),
MEMCACHE_COMPRESSED,
time() + $ttl
);
} else {
return $this->memcache->set(
self::getApp()->get_hostname() . ":" . $key,
serialize($value),
MEMCACHE_COMPRESSED
);
}
}
/**
* (@inheritdoc)
*/
public function delete($key)
{
return $this->memcache->delete($key);
@ -72,6 +92,14 @@ class MemcacheCacheDriver extends BaseObject implements ICacheDriver
public function clear()
{
return true;
return $this->memcache->flush();
}
/**
* (@inheritdoc)
*/
public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
{
return $this->memcache->add(self::getApp()->get_hostname() . ":" . $key, $value, $ttl);
}
}

View file

@ -10,8 +10,11 @@ use Friendica\Core\Cache;
*
* @author Hypolite Petovan <mrpetovan@gmail.com>
*/
class MemcachedCacheDriver extends BaseObject implements ICacheDriver
class MemcachedCacheDriver extends BaseObject implements IMemoryCacheDriver
{
use TraitCompareSet;
use TraitCompareDelete;
/**
* @var Memcached
*/
@ -46,14 +49,22 @@ class MemcachedCacheDriver 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->memcached->set(
self::getApp()->get_hostname() . ':' . $key,
$value,
time() + $duration
);
if ($ttl > 0) {
return $this->memcached->set(
self::getApp()->get_hostname() . ':' . $key,
$value,
time() + $ttl
);
} else {
return $this->memcached->set(
self::getApp()->get_hostname() . ':' . $key,
$value
);
}
}
public function delete($key)
@ -67,4 +78,17 @@ class MemcachedCacheDriver 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)
{
return $this->memcached->add(self::getApp()->get_hostname() . ":" . $key, $value, $ttl);
}
}

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;
}
}

View file

@ -0,0 +1,45 @@
<?php
namespace Friendica\Core\Cache;
use Friendica\Core\Cache;
/**
* Trait TraitCompareSetDelete
*
* @brief This Trait is to compensate non native "exclusive" sets/deletes in caches
*
* @package Friendica\Core\Cache
*/
trait TraitCompareDelete
{
abstract public function get($key);
abstract public function set($key, $value, $ttl = Cache::FIVE_MINUTES);
abstract public function delete($key);
abstract public function add($key, $value, $ttl = Cache::FIVE_MINUTES);
/**
* @brief NonNative - 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) {
if ($this->add($key . "_lock", true)) {
if ($this->get($key) === $value) {
$this->delete($key);
$this->delete($key . "_lock");
return true;
} else {
$this->delete($key . "_lock");
return false;
}
} else {
return false;
}
}
}

View file

@ -0,0 +1,48 @@
<?php
namespace Friendica\Core\Cache;
use Friendica\Core\Cache;
/**
* Trait TraitCompareSetDelete
*
* @brief This Trait is to compensate non native "exclusive" sets/deletes in caches
*
* @package Friendica\Core\Cache
*/
trait TraitCompareSet
{
abstract public function get($key);
abstract public function set($key, $value, $ttl = Cache::FIVE_MINUTES);
abstract public function delete($key);
abstract public function add($key, $value, $ttl = Cache::FIVE_MINUTES);
/**
* @brief NonNative - 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 from the cache
* @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 ($this->add($key . "_lock", true)) {
if ($this->get($key) === $oldValue) {
$this->set($key, $newValue, $ttl);
$this->delete($key . "_lock");
return true;
} else {
$this->delete($key . "_lock");
return false;
}
} else {
return false;
}
}
}