]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Lock/CacheLock.php
Merge pull request #8269 from MrPetovan/bug/frio-more-actions
[friendica.git] / src / Core / Lock / CacheLock.php
index 36a7b4edfb2216703eed9cbe50cf3c19bfea9602..81046b29c49bebd4e0e83836f45c97007c6218cb 100644 (file)
@@ -1,12 +1,37 @@
 <?php
+/**
+ * @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\Cache;
+use Friendica\Core\BaseLock;
+use Friendica\Core\Cache\Duration;
 use Friendica\Core\Cache\IMemoryCache;
 
-class CacheLock extends Lock
+class CacheLock extends BaseLock
 {
+       /**
+        * @var string The static prefix of all locks inside the cache
+        */
+       const CACHE_PREFIX = 'lock:';
+
        /**
         * @var \Friendica\Core\Cache\ICache;
         */
@@ -25,7 +50,7 @@ class CacheLock extends Lock
        /**
         * (@inheritdoc)
         */
-       public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
+       public function acquire($key, $timeout = 120, $ttl = Duration::FIVE_MINUTES)
        {
                $got_lock = false;
                $start    = time();
@@ -61,7 +86,7 @@ class CacheLock extends Lock
        /**
         * (@inheritdoc)
         */
-       public function releaseLock($key, $override = false)
+       public function release($key, $override = false)
        {
                $cachekey = self::getLockKey($key);
 
@@ -85,6 +110,46 @@ class CacheLock extends Lock
                return isset($lock) && ($lock !== false);
        }
 
+       /**
+        * {@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)) {
+                               $success = false;
+                       }
+               }
+
+               return $success;
+       }
+
        /**
         * @param string $key The original key
         *
@@ -92,6 +157,6 @@ class CacheLock extends Lock
         */
        private static function getLockKey($key)
        {
-               return "lock:" . $key;
+               return self::CACHE_PREFIX . $key;
        }
 }