friendica/src/Core/Lock/CacheLock.php

163 lines
3.5 KiB
PHP
Raw Normal View History

<?php
2020-02-09 14:45:36 +00:00
/**
* @copyright Copyright (C) 2020, Friendica
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Core\Lock;
use Friendica\Core\BaseLock;
use Friendica\Core\Cache\Duration;
use Friendica\Core\Cache\IMemoryCache;
class CacheLock extends BaseLock
{
2019-08-13 19:20:41 +00:00
/**
* @var string The static prefix of all locks inside the cache
*/
const CACHE_PREFIX = 'lock:';
/**
* @var \Friendica\Core\Cache\ICache;
*/
private $cache;
/**
* CacheLock constructor.
*
* @param IMemoryCache $cache The CacheDriver for this type of lock
*/
public function __construct(IMemoryCache $cache)
{
$this->cache = $cache;
}
/**
2018-07-05 05:59:56 +00:00
* (@inheritdoc)
*/
public function acquire($key, $timeout = 120, $ttl = Duration::FIVE_MINUTES)
{
$got_lock = false;
$start = time();
$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;
}
// 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 (!$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)
*/
public function release($key, $override = false)
{
$cachekey = self::getLockKey($key);
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
}
$this->markRelease($key);
2019-03-04 20:28:36 +00:00
return $return;
}
/**
2018-07-05 05:59:56 +00:00
* (@inheritdoc)
*/
public function isLocked($key)
{
$cachekey = self::getLockKey($key);
$lock = $this->cache->get($cachekey);
return isset($lock) && ($lock !== false);
}
2019-08-13 19:20:41 +00:00
/**
* {@inheritDoc}
*/
public function getName()
{
return $this->cache->getName();
}
/**
* {@inheritDoc}
*/
public function getLocks(string $prefix = '')
{
$locks = $this->cache->getAllKeys(self::CACHE_PREFIX . $prefix);
array_walk($locks, function (&$lock, $key) {
$lock = substr($lock, strlen(self::CACHE_PREFIX));
});
return $locks;
}
/**
* {@inheritDoc}
*/
public function releaseAll($override = false)
{
$success = parent::releaseAll($override);
$locks = $this->getLocks();
foreach ($locks as $lock) {
if (!$this->release($lock, $override)) {
2019-08-13 19:20:41 +00:00
$success = false;
}
}
return $success;
}
/**
* @param string $key The original key
*
* @return string The cache key used for the cache
*/
private static function getLockKey($key)
{
2019-08-13 19:20:41 +00:00
return self::CACHE_PREFIX . $key;
}
2018-06-26 21:44:30 +00:00
}