friendica/src/Core/Lock/CacheLockDriver.php

89 lines
1.8 KiB
PHP
Raw Normal View History

<?php
namespace Friendica\Core\Lock;
use Friendica\Core\Cache\IMemoryCacheDriver;
class CacheLockDriver extends AbstractLockDriver
{
/**
* @var \Friendica\Core\Cache\ICacheDriver;
*/
private $cache;
/**
* CacheLockDriver constructor.
*
* @param IMemoryCacheDriver $cache The CacheDriver for this type of lock
*/
public function __construct(IMemoryCacheDriver $cache)
{
$this->cache = $cache;
}
/**
2018-07-05 01:59:56 -04:00
* (@inheritdoc)
*/
2018-07-05 01:59:56 -04:00
public function acquire($key, $timeout = 120)
{
$got_lock = false;
$start = time();
$cachekey = self::getCacheKey($key);
do {
$lock = $this->cache->get($cachekey);
// When we do want to lock something that was already locked by us.
if ((int)$lock == getmypid()) {
$got_lock = true;
}
// When we do want to lock something new
if (is_null($lock)) {
// 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)) {
$got_lock = true;
$this->markAcquire($key);
}
}
if (!$got_lock && ($timeout > 0)) {
usleep(rand(10000, 200000));
}
} while (!$got_lock && ((time() - $start) < $timeout));
return $got_lock;
}
/**
2018-07-05 01:59:56 -04:00
* (@inheritdoc)
*/
2018-07-05 01:59:56 -04:00
public function release($key)
{
$cachekey = self::getCacheKey($key);
$this->cache->compareDelete($cachekey, getmypid());
$this->markRelease($key);
}
/**
2018-07-05 01:59:56 -04:00
* (@inheritdoc)
*/
public function isLocked($key)
{
$cachekey = self::getCacheKey($key);
$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
*/
private static function getCacheKey($key) {
return self::getApp()->get_hostname() . ";lock:" . $key;
}
2018-06-26 17:44:30 -04:00
}