3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Core\Lock;
24 use Friendica\Core\BaseLock;
25 use Friendica\Core\Cache\Duration;
26 use Friendica\Core\Cache\IMemoryCache;
28 class CacheLock extends BaseLock
31 * @var string The static prefix of all locks inside the cache
33 const CACHE_PREFIX = 'lock:';
36 * @var \Friendica\Core\Cache\ICache;
41 * CacheLock constructor.
43 * @param IMemoryCache $cache The CacheDriver for this type of lock
45 public function __construct(IMemoryCache $cache)
47 $this->cache = $cache;
53 public function acquire($key, $timeout = 120, $ttl = Duration::FIVE_MINUTES)
58 $cachekey = self::getLockKey($key);
61 $lock = $this->cache->get($cachekey);
62 // When we do want to lock something that was already locked by us.
63 if ((int)$lock == getmypid()) {
67 // When we do want to lock something new
69 // At first initialize it with "0"
70 $this->cache->add($cachekey, 0);
71 // Now the value has to be "0" because otherwise the key was used by another process meanwhile
72 if ($this->cache->compareSet($cachekey, 0, getmypid(), $ttl)) {
74 $this->markAcquire($key);
78 if (!$got_lock && ($timeout > 0)) {
79 usleep(rand(10000, 200000));
81 } while (!$got_lock && ((time() - $start) < $timeout));
89 public function release($key, $override = false)
91 $cachekey = self::getLockKey($key);
94 $return = $this->cache->delete($cachekey);
96 $return = $this->cache->compareDelete($cachekey, getmypid());
98 $this->markRelease($key);
106 public function isLocked($key)
108 $cachekey = self::getLockKey($key);
109 $lock = $this->cache->get($cachekey);
110 return isset($lock) && ($lock !== false);
116 public function getName()
118 return $this->cache->getName();
124 public function getLocks(string $prefix = '')
126 $locks = $this->cache->getAllKeys(self::CACHE_PREFIX . $prefix);
128 array_walk($locks, function (&$lock, $key) {
129 $lock = substr($lock, strlen(self::CACHE_PREFIX));
138 public function releaseAll($override = false)
140 $success = parent::releaseAll($override);
142 $locks = $this->getLocks();
144 foreach ($locks as $lock) {
145 if (!$this->release($lock, $override)) {
154 * @param string $key The original key
156 * @return string The cache key used for the cache
158 private static function getLockKey($key)
160 return self::CACHE_PREFIX . $key;