X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FCore%2FLock%2FCacheLockDriver.php;h=69db1c27f83c7e5fcc679fc661ce2f2d34196ad4;hb=86bf2ee45a7c7409dbc470b5b1706b19e7e40507;hp=0adca140d16433c9fe5efcb41e739bdde71d9228;hpb=3f7e4f5bb67c67586423d9e1105ce274b83767c3;p=friendica.git diff --git a/src/Core/Lock/CacheLockDriver.php b/src/Core/Lock/CacheLockDriver.php index 0adca140d1..69db1c27f8 100644 --- a/src/Core/Lock/CacheLockDriver.php +++ b/src/Core/Lock/CacheLockDriver.php @@ -2,88 +2,96 @@ namespace Friendica\Core\Lock; -use Friendica\Core\Cache\ICacheDriver; +use Friendica\Core\Cache; +use Friendica\Core\Cache\IMemoryCache; -class CacheLockDriver extends AbstractLockDriver +class CacheLockDriver extends AbstractLock { /** - * @var \Friendica\Core\Cache\ICacheDriver; + * @var \Friendica\Core\Cache\ICache; */ private $cache; /** * CacheLockDriver constructor. * - * @param ICacheDriver $cache The CacheDriver for this type of lock + * @param IMemoryCache $cache The CacheDriver for this type of lock */ - public function __construct(ICacheDriver $cache) + public function __construct(IMemoryCache $cache) { $this->cache = $cache; } /** - * - * @brief Sets a lock for a given name - * - * @param string $key The Name of the lock - * @param integer $timeout Seconds until we give up - * - * @return boolean Was the lock successful? + * (@inheritdoc) */ - public function acquireLock(string $key, int $timeout = 120) + public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES) { $got_lock = false; - $start = time(); + $start = time(); - $cachekey = get_app()->get_hostname() . ";lock:" . $key; + $cachekey = self::getLockKey($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; + } - if (!is_bool($lock)) { - $pid = (int)$lock; - - // When the process id isn't used anymore, we can safely claim the lock for us. - // Or we do want to lock something that was already locked by us. - if (!posix_kill($pid, 0) || ($pid == getmypid())) { - $lock = false; + // 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(), $ttl)) { + $got_lock = true; + $this->markAcquire($key); } } - if (is_bool($lock)) { - $this->cache->set($cachekey, getmypid(), 300); - $got_lock = true; - } if (!$got_lock && ($timeout > 0)) { usleep(rand(10000, 200000)); } } while (!$got_lock && ((time() - $start) < $timeout)); - $this->markAcquire($key); - return $got_lock; } /** - * @brief Removes a lock if it was set by us - * - * @param string $key Name of the lock - * - * @return mixed + * (@inheritdoc) */ - public function releaseLock(string $key) + public function releaseLock($key, $override = false) { - $cachekey = get_app()->get_hostname() . ";lock:" . $key; - $lock = $this->cache->get($cachekey); + $cachekey = self::getLockKey($key); - if (!is_bool($lock)) { - if ((int)$lock == getmypid()) { - $this->cache->delete($cachekey); - } + if ($override) { + $return = $this->cache->delete($cachekey); + } else { + $return = $this->cache->compareDelete($cachekey, getmypid()); } - $this->markRelease($key); - return; + return $return; + } + + /** + * (@inheritdoc) + */ + public function isLocked($key) + { + $cachekey = self::getLockKey($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 getLockKey($key) + { + return "lock:" . $key; } }