2018-06-26 16:31:04 -04:00
|
|
|
<?php
|
|
|
|
|
2018-06-28 16:57:17 -04:00
|
|
|
namespace Friendica\Core\Lock;
|
2018-06-26 16:31:04 -04:00
|
|
|
|
2018-07-04 17:37:22 -04:00
|
|
|
use Friendica\Core\Cache\IMemoryCacheDriver;
|
2018-06-26 16:31:04 -04:00
|
|
|
|
2018-06-28 16:57:17 -04:00
|
|
|
class CacheLockDriver extends AbstractLockDriver
|
2018-06-26 16:31:04 -04:00
|
|
|
{
|
2018-06-28 16:57:17 -04:00
|
|
|
/**
|
|
|
|
* @var \Friendica\Core\Cache\ICacheDriver;
|
|
|
|
*/
|
|
|
|
private $cache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CacheLockDriver constructor.
|
|
|
|
*
|
2018-07-04 17:37:22 -04:00
|
|
|
* @param IMemoryCacheDriver $cache The CacheDriver for this type of lock
|
2018-06-28 16:57:17 -04:00
|
|
|
*/
|
2018-07-04 17:37:22 -04:00
|
|
|
public function __construct(IMemoryCacheDriver $cache)
|
2018-06-28 16:57:17 -04:00
|
|
|
{
|
|
|
|
$this->cache = $cache;
|
|
|
|
}
|
|
|
|
|
2018-06-26 16:31:04 -04:00
|
|
|
/**
|
2018-07-05 01:59:56 -04:00
|
|
|
* (@inheritdoc)
|
2018-06-26 16:31:04 -04:00
|
|
|
*/
|
2018-07-05 14:57:31 -04:00
|
|
|
public function acquireLock($key, $timeout = 120)
|
2018-06-26 16:31:04 -04:00
|
|
|
{
|
|
|
|
$got_lock = false;
|
|
|
|
$start = time();
|
|
|
|
|
2018-07-05 15:47:52 -04:00
|
|
|
$cachekey = self::getLockKey($key);
|
2018-06-26 16:31:04 -04:00
|
|
|
|
|
|
|
do {
|
2018-06-28 16:57:17 -04:00
|
|
|
$lock = $this->cache->get($cachekey);
|
2018-07-04 17:37:22 -04:00
|
|
|
// When we do want to lock something that was already locked by us.
|
|
|
|
if ((int)$lock == getmypid()) {
|
|
|
|
$got_lock = true;
|
|
|
|
}
|
2018-06-26 16:31:04 -04:00
|
|
|
|
2018-07-04 17:37:22 -04:00
|
|
|
// 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);
|
2018-06-26 16:31:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-26 16:31:04 -04:00
|
|
|
*/
|
2018-07-05 14:57:31 -04:00
|
|
|
public function releaseLock($key)
|
2018-06-26 16:31:04 -04:00
|
|
|
{
|
2018-07-05 15:47:52 -04:00
|
|
|
$cachekey = self::getLockKey($key);
|
2018-06-26 16:31:04 -04:00
|
|
|
|
2018-07-04 17:37:22 -04:00
|
|
|
$this->cache->compareDelete($cachekey, getmypid());
|
2018-06-28 16:57:17 -04:00
|
|
|
$this->markRelease($key);
|
2018-07-04 17:37:22 -04:00
|
|
|
}
|
2018-06-26 16:31:04 -04:00
|
|
|
|
2018-07-04 17:37:22 -04:00
|
|
|
/**
|
2018-07-05 01:59:56 -04:00
|
|
|
* (@inheritdoc)
|
2018-07-04 17:37:22 -04:00
|
|
|
*/
|
|
|
|
public function isLocked($key)
|
|
|
|
{
|
2018-07-05 15:47:52 -04:00
|
|
|
$cachekey = self::getLockKey($key);
|
2018-07-04 17:37:22 -04:00
|
|
|
$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
|
|
|
|
*/
|
2018-07-05 15:47:52 -04:00
|
|
|
private static function getLockKey($key) {
|
|
|
|
return "lock:" . $key;
|
2018-06-26 16:31:04 -04:00
|
|
|
}
|
2018-06-26 17:44:30 -04:00
|
|
|
}
|