]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Lock/CacheLockDriver.php
Rename *CacheDriver to *Cache because they don't act as driver anymore
[friendica.git] / src / Core / Lock / CacheLockDriver.php
index 13d912c1e2979726fff44e3c511180a5739b8410..69db1c27f83c7e5fcc679fc661ce2f2d34196ad4 100644 (file)
@@ -2,40 +2,35 @@
 
 namespace Friendica\Core\Lock;
 
-use Friendica\Core\Cache\IMemoryCacheDriver;
+use Friendica\Core\Cache;
+use Friendica\Core\Cache\IMemoryCache;
 
-class CacheLockDriver extends AbstractLockDriver
+class CacheLockDriver extends AbstractLock
 {
        /**
-        * @var \Friendica\Core\Cache\ICacheDriver;
+        * @var \Friendica\Core\Cache\ICache;
         */
        private $cache;
 
        /**
         * CacheLockDriver constructor.
         *
-        * @param IMemoryCacheDriver $cache The CacheDriver for this type of lock
+        * @param IMemoryCache $cache The CacheDriver for this type of lock
         */
-       public function __construct(IMemoryCacheDriver $cache)
+       public function __construct(IMemoryCache $cache)
        {
                $this->cache = $cache;
        }
 
        /**
-        *
-        * @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();
+               $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,36 +59,39 @@ 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)
+       public function releaseLock($key, $override = false)
        {
-               $cachekey = self::getCacheKey($key);
+               $cachekey = self::getLockKey($key);
 
-               $this->cache->compareDelete($cachekey, getmypid());
+               if ($override) {
+                       $return = $this->cache->delete($cachekey);
+               } else {
+                       $return = $this->cache->compareDelete($cachekey, getmypid());
+               }
                $this->markRelease($key);
+
+               return $return;
        }
 
        /**
-        * @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);
-               $lock = $this->cache->get($cachekey);
+               $cachekey = self::getLockKey($key);
+               $lock     = $this->cache->get($cachekey);
                return isset($lock) && ($lock !== false);
        }
 
        /**
-        * @param string $key   The original key
-        * @return string               The cache key used for the cache
+        * @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;
        }
 }