3 namespace Friendica\Core\Lock;
5 use Friendica\Core\BaseLock;
6 use Friendica\Core\Cache\Duration;
7 use Friendica\Core\Cache\IMemoryCache;
9 class CacheLock extends BaseLock
12 * @var string The static prefix of all locks inside the cache
14 const CACHE_PREFIX = 'lock:';
17 * @var \Friendica\Core\Cache\ICache;
22 * CacheLock constructor.
24 * @param IMemoryCache $cache The CacheDriver for this type of lock
26 public function __construct(IMemoryCache $cache)
28 $this->cache = $cache;
34 public function acquire($key, $timeout = 120, $ttl = Duration::FIVE_MINUTES)
39 $cachekey = self::getLockKey($key);
42 $lock = $this->cache->get($cachekey);
43 // When we do want to lock something that was already locked by us.
44 if ((int)$lock == getmypid()) {
48 // When we do want to lock something new
50 // At first initialize it with "0"
51 $this->cache->add($cachekey, 0);
52 // Now the value has to be "0" because otherwise the key was used by another process meanwhile
53 if ($this->cache->compareSet($cachekey, 0, getmypid(), $ttl)) {
55 $this->markAcquire($key);
59 if (!$got_lock && ($timeout > 0)) {
60 usleep(rand(10000, 200000));
62 } while (!$got_lock && ((time() - $start) < $timeout));
70 public function release($key, $override = false)
72 $cachekey = self::getLockKey($key);
75 $return = $this->cache->delete($cachekey);
77 $return = $this->cache->compareDelete($cachekey, getmypid());
79 $this->markRelease($key);
87 public function isLocked($key)
89 $cachekey = self::getLockKey($key);
90 $lock = $this->cache->get($cachekey);
91 return isset($lock) && ($lock !== false);
97 public function getName()
99 return $this->cache->getName();
105 public function getLocks(string $prefix = '')
107 $locks = $this->cache->getAllKeys(self::CACHE_PREFIX . $prefix);
109 array_walk($locks, function (&$lock, $key) {
110 $lock = substr($lock, strlen(self::CACHE_PREFIX));
119 public function releaseAll($override = false)
121 $success = parent::releaseAll($override);
123 $locks = $this->getLocks();
125 foreach ($locks as $lock) {
126 if (!$this->release($lock, $override)) {
135 * @param string $key The original key
137 * @return string The cache key used for the cache
139 private static function getLockKey($key)
141 return self::CACHE_PREFIX . $key;