Added Unittests for cache

fixed Lock & Cache bugs
This commit is contained in:
Philipp Holzer 2018-07-07 19:46:16 +02:00
parent 1dafaa69c5
commit 80a4e6263f
No known key found for this signature in database
GPG key ID: 58160D7D6AF942B6
18 changed files with 317 additions and 25 deletions

View file

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

View file

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

View file

@ -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

View file

@ -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]) {