mirror of
https://github.com/friendica/friendica
synced 2025-04-27 17:10:10 +00:00
Adding possibility to use a different cache-backend for locking and caching
- Renaming *LockDriver to *Lock since it isn't a "driver" anymore
This commit is contained in:
parent
86bf2ee45a
commit
34e4968c06
19 changed files with 149 additions and 64 deletions
97
src/Core/Lock/CacheLock.php
Normal file
97
src/Core/Lock/CacheLock.php
Normal file
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Lock;
|
||||
|
||||
use Friendica\Core\Cache;
|
||||
use Friendica\Core\Cache\IMemoryCache;
|
||||
|
||||
class CacheLock extends AbstractLock
|
||||
{
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function acquireLock($key, $timeout = 120, $ttl = Cache::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;
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function releaseLock($key, $override = false)
|
||||
{
|
||||
$cachekey = self::getLockKey($key);
|
||||
|
||||
if ($override) {
|
||||
$return = $this->cache->delete($cachekey);
|
||||
} else {
|
||||
$return = $this->cache->compareDelete($cachekey, getmypid());
|
||||
}
|
||||
$this->markRelease($key);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue