3 * @copyright Copyright (C) 2010-2021, the Friendica project
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\Type;
24 use Friendica\Core\Cache\Enum\Duration;
25 use Friendica\Core\Cache\IMemoryCache;
27 class CacheLock extends BaseLock
30 * @var string The static prefix of all locks inside the cache
32 const CACHE_PREFIX = 'lock:';
35 * @var \Friendica\Core\Cache\ICache;
40 * CacheLock constructor.
42 * @param IMemoryCache $cache The CacheDriver for this type of lock
44 public function __construct(IMemoryCache $cache)
46 $this->cache = $cache;
52 public function acquire($key, $timeout = 120, $ttl = Duration::FIVE_MINUTES)
57 $cachekey = self::getLockKey($key);
60 $lock = $this->cache->get($cachekey);
61 // When we do want to lock something that was already locked by us.
62 if ((int)$lock == getmypid()) {
66 // When we do want to lock something new
68 // At first initialize it with "0"
69 $this->cache->add($cachekey, 0);
70 // Now the value has to be "0" because otherwise the key was used by another process meanwhile
71 if ($this->cache->compareSet($cachekey, 0, getmypid(), $ttl)) {
73 $this->markAcquire($key);
77 if (!$got_lock && ($timeout > 0)) {
78 usleep(rand(10000, 200000));
80 } while (!$got_lock && ((time() - $start) < $timeout));
88 public function release($key, $override = false)
90 $cachekey = self::getLockKey($key);
93 $return = $this->cache->delete($cachekey);
95 $return = $this->cache->compareDelete($cachekey, getmypid());
97 $this->markRelease($key);
105 public function isLocked($key)
107 $cachekey = self::getLockKey($key);
108 $lock = $this->cache->get($cachekey);
109 return isset($lock) && ($lock !== false);
115 public function getName()
117 return $this->cache->getName();
123 public function getLocks(string $prefix = '')
125 $locks = $this->cache->getAllKeys(self::CACHE_PREFIX . $prefix);
127 array_walk($locks, function (&$lock, $key) {
128 $lock = substr($lock, strlen(self::CACHE_PREFIX));
137 public function releaseAll($override = false)
139 $success = parent::releaseAll($override);
141 $locks = $this->getLocks();
143 foreach ($locks as $lock) {
144 if (!$this->release($lock, $override)) {
153 * @param string $key The original key
155 * @return string The cache key used for the cache
157 private static function getLockKey($key)
159 return self::CACHE_PREFIX . $key;