]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Lock/CacheLockDriver.php
Merge branch 'develop' into item-activities
[friendica.git] / src / Core / Lock / CacheLockDriver.php
index 13d912c1e2979726fff44e3c511180a5739b8410..18d441ffea09d5a0910ec6ac3ada461db885ab7c 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace Friendica\Core\Lock;
 
+use Friendica\Core\Cache;
 use Friendica\Core\Cache\IMemoryCacheDriver;
 
 class CacheLockDriver extends AbstractLockDriver
@@ -22,20 +23,14 @@ class CacheLockDriver extends AbstractLockDriver
        }
 
        /**
-        *
-        * @brief Sets a lock for a given name
-        *
-        * @param string $key The Name of the lock
-        * @param integer $timeout Seconds until we give up
-        *
-        * @return boolean Was the lock successful?
+        * (@inheritdoc)
         */
-       public function acquireLock($key, $timeout = 120)
+       public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
        {
                $got_lock = false;
                $start = time();
 
-               $cachekey = self::getCacheKey($key);
+               $cachekey = self::getLockKey($key);
 
                do {
                        $lock = $this->cache->get($cachekey);
@@ -49,7 +44,7 @@ class CacheLockDriver extends AbstractLockDriver
                                // 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(), 300)) {
+                               if ($this->cache->compareSet($cachekey, 0, getmypid(), $ttl)) {
                                        $got_lock = true;
                                        $this->markAcquire($key);
                                }
@@ -64,27 +59,22 @@ class CacheLockDriver extends AbstractLockDriver
        }
 
        /**
-        * @brief Removes a lock if it was set by us
-        *
-        * @param string $key Name of the lock
+        * (@inheritdoc)
         */
        public function releaseLock($key)
        {
-               $cachekey = self::getCacheKey($key);
+               $cachekey = self::getLockKey($key);
 
                $this->cache->compareDelete($cachekey, getmypid());
                $this->markRelease($key);
        }
 
        /**
-        * @brief Checks, if a key is currently locked to a process
-        *
-        * @param string $key The name of the lock
-        * @return bool
+        * (@inheritdoc)
         */
        public function isLocked($key)
        {
-               $cachekey = self::getCacheKey($key);
+               $cachekey = self::getLockKey($key);
                $lock = $this->cache->get($cachekey);
                return isset($lock) && ($lock !== false);
        }
@@ -93,7 +83,7 @@ class CacheLockDriver extends AbstractLockDriver
         * @param string $key   The original key
         * @return string               The cache key used for the cache
         */
-       private static function getCacheKey($key) {
-               return self::getApp()->get_hostname() . ";lock:" . $key;
+       private static function getLockKey($key) {
+               return "lock:" . $key;
        }
 }