1
0
Fork 0

Refactor Cache/Lock to DICE

- Refactor Cache classes
- Refactor Lock classes
- Improved test speed (removed some seperate class annotations)
This commit is contained in:
Philipp Holzer 2019-08-03 20:48:56 +02:00
commit d56bd28a07
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90
40 changed files with 766 additions and 621 deletions

View file

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