mirror of
https://github.com/friendica/friendica
synced 2025-04-28 17:44:22 +02:00
Added Unittests for cache
fixed Lock & Cache bugs
This commit is contained in:
parent
1dafaa69c5
commit
80a4e6263f
18 changed files with 317 additions and 25 deletions
|
@ -51,7 +51,7 @@ class ArrayCache extends AbstractCacheDriver implements IMemoryCacheDriver
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function clear()
|
||||
public function clear($outdated = true)
|
||||
{
|
||||
$this->cachedData = [];
|
||||
return true;
|
||||
|
|
|
@ -37,7 +37,7 @@ class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
|
|||
{
|
||||
$fields = [
|
||||
'v' => serialize($value),
|
||||
'expires' => DateTimeFormat::utc('now + ' . $ttl . ' seconds'),
|
||||
'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds'),
|
||||
'updated' => DateTimeFormat::utcNow()
|
||||
];
|
||||
|
||||
|
@ -49,8 +49,12 @@ class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
|
|||
return dba::delete('cache', ['k' => $key]);
|
||||
}
|
||||
|
||||
public function clear()
|
||||
public function clear($outdated = true)
|
||||
{
|
||||
return dba::delete('cache', ['`expires` < NOW()']);
|
||||
if ($outdated) {
|
||||
return dba::delete('cache', ['`expires` < NOW()']);
|
||||
} else {
|
||||
return dba::delete('cache', ['`k` IS NOT NULL ']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,8 +42,9 @@ interface ICacheDriver
|
|||
|
||||
/**
|
||||
* Remove outdated data from the cache
|
||||
* @param boolean $outdated just remove outdated values
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function clear();
|
||||
public function clear($outdated = true);
|
||||
}
|
||||
|
|
|
@ -96,9 +96,13 @@ class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDri
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function clear()
|
||||
public function clear($outdated = true)
|
||||
{
|
||||
return $this->memcache->flush();
|
||||
if ($outdated) {
|
||||
return true;
|
||||
} else {
|
||||
return $this->memcache->flush();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -58,7 +58,7 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr
|
|||
return $this->memcached->set(
|
||||
$cachekey,
|
||||
$value,
|
||||
time() + $ttl
|
||||
$ttl
|
||||
);
|
||||
} else {
|
||||
return $this->memcached->set(
|
||||
|
@ -75,9 +75,13 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr
|
|||
return $this->memcached->delete($cachekey);
|
||||
}
|
||||
|
||||
public function clear()
|
||||
public function clear($outdated = true)
|
||||
{
|
||||
return $this->memcached->flush();
|
||||
if ($outdated) {
|
||||
return true;
|
||||
} else {
|
||||
return $this->memcached->flush();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -61,7 +61,7 @@ class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
|
|||
if ($ttl > 0) {
|
||||
return $this->redis->setex(
|
||||
$cachekey,
|
||||
time() + $ttl,
|
||||
$ttl,
|
||||
$cached
|
||||
);
|
||||
} else {
|
||||
|
@ -78,12 +78,15 @@ class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
|
|||
return ($this->redis->delete($cachekey) > 0);
|
||||
}
|
||||
|
||||
public function clear()
|
||||
public function clear($outdated = true)
|
||||
{
|
||||
return $this->redis->flushAll();
|
||||
if ($outdated) {
|
||||
return true;
|
||||
} else {
|
||||
return $this->redis->flushAll();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
|
@ -92,7 +95,7 @@ class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
|
|||
$cachekey = $this->getCacheKey($key);
|
||||
$cached = json_encode($value);
|
||||
|
||||
return $this->redis->setnx($cachekey, $value);
|
||||
return $this->redis->setnx($cachekey, $cached);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Friendica\Core\Lock;
|
||||
|
||||
use Friendica\Core\Cache;
|
||||
use Friendica\Core\Cache\IMemoryCacheDriver;
|
||||
|
||||
class CacheLockDriver extends AbstractLockDriver
|
||||
|
@ -24,7 +25,7 @@ class CacheLockDriver extends AbstractLockDriver
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function acquireLock($key, $timeout = 120)
|
||||
public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
|
||||
{
|
||||
$got_lock = false;
|
||||
$start = time();
|
||||
|
@ -43,7 +44,7 @@ class CacheLockDriver extends AbstractLockDriver
|
|||
// At first initialize it with "0"
|
||||
$this->cache->add($cachekey, 0);
|
||||
// Now the value has to be "0" because otherwise the key was used by another process meanwhile
|
||||
if ($this->cache->compareSet($cachekey, 0, getmypid(), 300)) {
|
||||
if ($this->cache->compareSet($cachekey, 0, getmypid(), $ttl)) {
|
||||
$got_lock = true;
|
||||
$this->markAcquire($key);
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
namespace Friendica\Core\Lock;
|
||||
|
||||
use dba;
|
||||
use Friendica\Core\Cache;
|
||||
use Friendica\Database\DBM;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
|
||||
|
@ -14,7 +15,7 @@ class DatabaseLockDriver extends AbstractLockDriver
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function acquireLock($key, $timeout = 120)
|
||||
public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
|
||||
{
|
||||
$got_lock = false;
|
||||
$start = time();
|
||||
|
@ -28,16 +29,14 @@ class DatabaseLockDriver extends AbstractLockDriver
|
|||
// We want to lock something that was already locked by us? So we got the lock.
|
||||
if ($lock['pid'] == getmypid()) {
|
||||
$got_lock = true;
|
||||
$this->markAcquire($key);
|
||||
}
|
||||
}
|
||||
if (!$lock['locked']) {
|
||||
dba::update('locks', ['locked' => true, 'pid' => getmypid(), 'expires' => DateTimeFormat::utc('now + 300seconds')], ['name' => $key]);
|
||||
dba::update('locks', ['locked' => true, 'pid' => getmypid(), 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')], ['name' => $key]);
|
||||
$got_lock = true;
|
||||
$this->markAcquire($key);
|
||||
}
|
||||
} else {
|
||||
dba::insert('locks', ['name' => $key, 'locked' => true, 'pid' => getmypid(), 'expires' => DateTimeFormat::utc('now + 300seconds')]);
|
||||
dba::insert('locks', ['name' => $key, 'locked' => true, 'pid' => getmypid(), 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')]);
|
||||
$got_lock = true;
|
||||
$this->markAcquire($key);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Lock;
|
||||
use Friendica\Core\Cache;
|
||||
|
||||
/**
|
||||
* Lock Driver Interface
|
||||
|
@ -23,10 +24,11 @@ interface ILockDriver
|
|||
*
|
||||
* @param string $key The Name of the lock
|
||||
* @param integer $timeout Seconds until we give up
|
||||
* @param integer $ttl Seconds The lock lifespan, must be one of the Cache constants
|
||||
*
|
||||
* @return boolean Was the lock successful?
|
||||
*/
|
||||
public function acquireLock($key, $timeout = 120);
|
||||
public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES);
|
||||
|
||||
/**
|
||||
* Releases a lock if it was set by us
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace Friendica\Core\Lock;
|
||||
|
||||
use Friendica\Core\Cache;
|
||||
|
||||
class SemaphoreLockDriver extends AbstractLockDriver
|
||||
{
|
||||
private static $semaphore = [];
|
||||
|
@ -30,10 +32,9 @@ class SemaphoreLockDriver extends AbstractLockDriver
|
|||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function acquireLock($key, $timeout = 120)
|
||||
public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
|
||||
{
|
||||
self::$semaphore[$key] = sem_get(self::semaphoreKey($key));
|
||||
if (self::$semaphore[$key]) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue