mirror of
https://github.com/friendica/friendica
synced 2025-04-29 13:04:23 +02:00
Refactor Cache/Lock to DICE
- Refactor Cache classes - Refactor Lock classes - Improved test speed (removed some seperate class annotations)
This commit is contained in:
parent
b95d4f41b9
commit
d56bd28a07
40 changed files with 766 additions and 621 deletions
|
@ -1,7 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Lock;
|
||||
use Friendica\BaseObject;
|
||||
|
||||
/**
|
||||
* Class AbstractLockDriver
|
||||
|
@ -10,7 +9,7 @@ use Friendica\BaseObject;
|
|||
*
|
||||
* Basic class for Locking with common functions (local acquired locks, releaseAll, ..)
|
||||
*/
|
||||
abstract class AbstractLockDriver extends BaseObject implements ILockDriver
|
||||
abstract class AbstractLockDriver implements ILockDriver
|
||||
{
|
||||
/**
|
||||
* @var array The local acquired locks
|
||||
|
@ -21,6 +20,7 @@ abstract class AbstractLockDriver extends BaseObject implements ILockDriver
|
|||
* Check if we've locally acquired a lock
|
||||
*
|
||||
* @param string key The Name of the lock
|
||||
*
|
||||
* @return bool Returns true if the lock is set
|
||||
*/
|
||||
protected function hasAcquiredLock($key)
|
||||
|
|
|
@ -28,7 +28,7 @@ class CacheLockDriver extends AbstractLockDriver
|
|||
public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
|
||||
{
|
||||
$got_lock = false;
|
||||
$start = time();
|
||||
$start = time();
|
||||
|
||||
$cachekey = self::getLockKey($key);
|
||||
|
||||
|
@ -65,8 +65,6 @@ class CacheLockDriver extends AbstractLockDriver
|
|||
{
|
||||
$cachekey = self::getLockKey($key);
|
||||
|
||||
$return = false;
|
||||
|
||||
if ($override) {
|
||||
$return = $this->cache->delete($cachekey);
|
||||
} else {
|
||||
|
@ -83,15 +81,17 @@ class CacheLockDriver extends AbstractLockDriver
|
|||
public function isLocked($key)
|
||||
{
|
||||
$cachekey = self::getLockKey($key);
|
||||
$lock = $this->cache->get($cachekey);
|
||||
$lock = $this->cache->get($cachekey);
|
||||
return isset($lock) && ($lock !== false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key The original key
|
||||
* @return string The cache key used for the cache
|
||||
* @param string $key The original key
|
||||
*
|
||||
* @return string The cache key used for the cache
|
||||
*/
|
||||
private static function getLockKey($key) {
|
||||
private static function getLockKey($key)
|
||||
{
|
||||
return "lock:" . $key;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
namespace Friendica\Core\Lock;
|
||||
|
||||
use Friendica\Core\Cache;
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
|
||||
/**
|
||||
|
@ -18,11 +18,17 @@ class DatabaseLockDriver extends AbstractLockDriver
|
|||
*/
|
||||
private $pid;
|
||||
|
||||
/**
|
||||
* @var Database The database connection of Friendica
|
||||
*/
|
||||
private $dba;
|
||||
|
||||
/**
|
||||
* @param null|int $pid The Id of the current process (null means determine automatically)
|
||||
*/
|
||||
public function __construct($pid = null)
|
||||
public function __construct(Database $dba, $pid = null)
|
||||
{
|
||||
$this->dba = $dba;
|
||||
$this->pid = isset($pid) ? $pid : getmypid();
|
||||
}
|
||||
|
||||
|
@ -32,13 +38,13 @@ class DatabaseLockDriver extends AbstractLockDriver
|
|||
public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
|
||||
{
|
||||
$got_lock = false;
|
||||
$start = time();
|
||||
$start = time();
|
||||
|
||||
do {
|
||||
DBA::lock('locks');
|
||||
$lock = DBA::selectFirst('locks', ['locked', 'pid'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
|
||||
$this->dba->lock('locks');
|
||||
$lock = $this->dba->selectFirst('locks', ['locked', 'pid'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
|
||||
|
||||
if (DBA::isResult($lock)) {
|
||||
if ($this->dba->isResult($lock)) {
|
||||
if ($lock['locked']) {
|
||||
// We want to lock something that was already locked by us? So we got the lock.
|
||||
if ($lock['pid'] == $this->pid) {
|
||||
|
@ -46,16 +52,16 @@ class DatabaseLockDriver extends AbstractLockDriver
|
|||
}
|
||||
}
|
||||
if (!$lock['locked']) {
|
||||
DBA::update('locks', ['locked' => true, 'pid' => $this->pid, 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')], ['name' => $key]);
|
||||
$this->dba->update('locks', ['locked' => true, 'pid' => $this->pid, 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')], ['name' => $key]);
|
||||
$got_lock = true;
|
||||
}
|
||||
} else {
|
||||
DBA::insert('locks', ['name' => $key, 'locked' => true, 'pid' => $this->pid, 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')]);
|
||||
$this->dba->insert('locks', ['name' => $key, 'locked' => true, 'pid' => $this->pid, 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')]);
|
||||
$got_lock = true;
|
||||
$this->markAcquire($key);
|
||||
}
|
||||
|
||||
DBA::unlock();
|
||||
$this->dba->unlock();
|
||||
|
||||
if (!$got_lock && ($timeout > 0)) {
|
||||
usleep(rand(100000, 2000000));
|
||||
|
@ -76,7 +82,7 @@ class DatabaseLockDriver extends AbstractLockDriver
|
|||
$where = ['name' => $key, 'pid' => $this->pid];
|
||||
}
|
||||
|
||||
$return = DBA::delete('locks', $where);
|
||||
$return = $this->dba->delete('locks', $where);
|
||||
|
||||
$this->markRelease($key);
|
||||
|
||||
|
@ -88,7 +94,7 @@ class DatabaseLockDriver extends AbstractLockDriver
|
|||
*/
|
||||
public function releaseAll()
|
||||
{
|
||||
$return = DBA::delete('locks', ['pid' => $this->pid]);
|
||||
$return = $this->dba->delete('locks', ['pid' => $this->pid]);
|
||||
|
||||
$this->acquiredLocks = [];
|
||||
|
||||
|
@ -100,9 +106,9 @@ class DatabaseLockDriver extends AbstractLockDriver
|
|||
*/
|
||||
public function isLocked($key)
|
||||
{
|
||||
$lock = DBA::selectFirst('locks', ['locked'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
|
||||
$lock = $this->dba->selectFirst('locks', ['locked'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
|
||||
|
||||
if (DBA::isResult($lock)) {
|
||||
if ($this->dba->isResult($lock)) {
|
||||
return $lock['locked'] !== false;
|
||||
} else {
|
||||
return false;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Lock;
|
||||
|
||||
use Friendica\Core\Cache;
|
||||
|
||||
/**
|
||||
|
@ -13,7 +14,8 @@ interface ILockDriver
|
|||
/**
|
||||
* Checks, if a key is currently locked to a or my process
|
||||
*
|
||||
* @param string $key The name of the lock
|
||||
* @param string $key The name of the lock
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isLocked($key);
|
||||
|
@ -22,13 +24,13 @@ interface ILockDriver
|
|||
*
|
||||
* Acquires a lock for a given name
|
||||
*
|
||||
* @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
|
||||
* @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, $ttl = Cache::FIVE_MINUTES);
|
||||
public function acquireLock($key, $timeout = 120, $ttl = Cache\ICacheDriver::FIVE_MINUTES);
|
||||
|
||||
/**
|
||||
* Releases a lock if it was set by us
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue