2018-06-26 20:31:04 +00:00
|
|
|
<?php
|
|
|
|
|
2018-06-28 20:57:17 +00:00
|
|
|
namespace Friendica\Core\Lock;
|
2018-06-26 20:31:04 +00:00
|
|
|
|
2018-07-07 17:46:16 +00:00
|
|
|
use Friendica\Core\Cache;
|
2018-07-04 21:37:22 +00:00
|
|
|
use Friendica\Core\Cache\IMemoryCacheDriver;
|
2018-06-26 20:31:04 +00:00
|
|
|
|
2018-06-28 20:57:17 +00:00
|
|
|
class CacheLockDriver extends AbstractLockDriver
|
2018-06-26 20:31:04 +00:00
|
|
|
{
|
2018-06-28 20:57:17 +00:00
|
|
|
/**
|
|
|
|
* @var \Friendica\Core\Cache\ICacheDriver;
|
|
|
|
*/
|
|
|
|
private $cache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CacheLockDriver constructor.
|
|
|
|
*
|
2018-07-04 21:37:22 +00:00
|
|
|
* @param IMemoryCacheDriver $cache The CacheDriver for this type of lock
|
2018-06-28 20:57:17 +00:00
|
|
|
*/
|
2018-07-04 21:37:22 +00:00
|
|
|
public function __construct(IMemoryCacheDriver $cache)
|
2018-06-28 20:57:17 +00:00
|
|
|
{
|
|
|
|
$this->cache = $cache;
|
|
|
|
}
|
|
|
|
|
2018-06-26 20:31:04 +00:00
|
|
|
/**
|
2018-07-05 05:59:56 +00:00
|
|
|
* (@inheritdoc)
|
2018-06-26 20:31:04 +00:00
|
|
|
*/
|
2018-07-07 17:46:16 +00:00
|
|
|
public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
|
2018-06-26 20:31:04 +00:00
|
|
|
{
|
|
|
|
$got_lock = false;
|
|
|
|
$start = time();
|
|
|
|
|
2018-07-05 19:47:52 +00:00
|
|
|
$cachekey = self::getLockKey($key);
|
2018-06-26 20:31:04 +00:00
|
|
|
|
|
|
|
do {
|
2018-06-28 20:57:17 +00:00
|
|
|
$lock = $this->cache->get($cachekey);
|
2018-07-04 21:37:22 +00:00
|
|
|
// When we do want to lock something that was already locked by us.
|
|
|
|
if ((int)$lock == getmypid()) {
|
|
|
|
$got_lock = true;
|
|
|
|
}
|
2018-06-26 20:31:04 +00:00
|
|
|
|
2018-07-04 21:37:22 +00: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
|
2018-07-07 17:46:16 +00:00
|
|
|
if ($this->cache->compareSet($cachekey, 0, getmypid(), $ttl)) {
|
2018-07-04 21:37:22 +00:00
|
|
|
$got_lock = true;
|
|
|
|
$this->markAcquire($key);
|
2018-06-26 20:31:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$got_lock && ($timeout > 0)) {
|
|
|
|
usleep(rand(10000, 200000));
|
|
|
|
}
|
|
|
|
} while (!$got_lock && ((time() - $start) < $timeout));
|
|
|
|
|
|
|
|
return $got_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-05 05:59:56 +00:00
|
|
|
* (@inheritdoc)
|
2018-06-26 20:31:04 +00:00
|
|
|
*/
|
2019-02-24 11:24:09 +00:00
|
|
|
public function releaseLock($key, $override = false)
|
2018-06-26 20:31:04 +00:00
|
|
|
{
|
2018-07-05 19:47:52 +00:00
|
|
|
$cachekey = self::getLockKey($key);
|
2018-06-26 20:31:04 +00:00
|
|
|
|
2019-03-04 20:28:36 +00:00
|
|
|
$return = false;
|
|
|
|
|
2019-02-24 11:24:09 +00:00
|
|
|
if ($override) {
|
2019-03-04 20:28:36 +00:00
|
|
|
$return = $this->cache->delete($cachekey);
|
2019-02-24 09:08:28 +00:00
|
|
|
} else {
|
2019-03-04 20:28:36 +00:00
|
|
|
$return = $this->cache->compareDelete($cachekey, getmypid());
|
2019-02-24 09:08:28 +00:00
|
|
|
}
|
2018-06-28 20:57:17 +00:00
|
|
|
$this->markRelease($key);
|
2019-03-04 20:28:36 +00:00
|
|
|
|
|
|
|
return $return;
|
2018-07-04 21:37:22 +00:00
|
|
|
}
|
2018-06-26 20:31:04 +00:00
|
|
|
|
2018-07-04 21:37:22 +00:00
|
|
|
/**
|
2018-07-05 05:59:56 +00:00
|
|
|
* (@inheritdoc)
|
2018-07-04 21:37:22 +00:00
|
|
|
*/
|
|
|
|
public function isLocked($key)
|
|
|
|
{
|
2018-07-05 19:47:52 +00:00
|
|
|
$cachekey = self::getLockKey($key);
|
2018-07-04 21:37:22 +00: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 19:47:52 +00:00
|
|
|
private static function getLockKey($key) {
|
|
|
|
return "lock:" . $key;
|
2018-06-26 20:31:04 +00:00
|
|
|
}
|
2018-06-26 21:44:30 +00:00
|
|
|
}
|