]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Lock.php
Merge pull request #7754 from annando/aria
[friendica.git] / src / Core / Lock.php
index 9e02d14fcb585dc06418e50b0e51c64300324396..ef62bc8f76e20770a9f7287226424ae7a8379063 100644 (file)
@@ -1,97 +1,57 @@
 <?php
-/**
- * @file src/Core/Lock.php
- */
-namespace Friendica\Util;
 
 /**
  * @file src/Core/Lock.php
  * @brief Functions for preventing parallel execution of functions
  */
 
-use Friendica\Core\Config;
-use Friendica\Core\Lock;
+namespace Friendica\Core;
 
-require_once 'include/dba.php';
+use Friendica\BaseObject;
+use Friendica\Core\Cache\Cache;
+use Friendica\Core\Lock\ILock;
 
 /**
- * @brief This class contain Functions for preventing parallel execution of functions
+ * This class contain Functions for preventing parallel execution of functions
  */
-class Lock
+class Lock extends BaseObject
 {
-       /**
-        * @var Lock\ILockDriver;
-        */
-       static $driver = null;
-
-       public static function init()
-       {
-               switch(Config::get('system', 'lock_driver', 'default')) {
-                       case 'memcache':
-                               self::$driver = new Lock\MemcacheLockDriver();
-                               break;
-                       case 'database':
-                               self::$driver = new Lock\DatabaseLockDriver();
-                               break;
-                       case 'semaphore':
-                               self::$driver = new Lock\SemaphoreLockDriver();
-                               break;
-                       default:
-                               // Determine what's the best/fastest locking driver (default behavior in Friendica)
-                               if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
-                                       self::$driver = new Lock\SemaphoreLockDriver();
-                               } elseif (Config::get('system', 'cache_driver', 'database') == 'memcache') {
-                                       self::$driver = new Lock\MemcacheLockDriver();
-                               } else {
-                                       self::$driver = new Lock\DatabaseLockDriver();
-                               }
-               }
-       }
-
-       /**
-        * Returns the current cache driver
-        *
-        * @return Lock\ILockDriver;
-        */
-       private static function getDriver()
-       {
-               if (self::$driver === null) {
-                       self::init();
-               }
-
-               return self::$driver;
-       }
-
        /**
         * @brief Acquires a lock for a given name
         *
-        * @param string  $key Name of the lock
+        * @param string  $key     Name of the lock
         * @param integer $timeout Seconds until we give up
+        * @param integer $ttl     The Lock lifespan, must be one of the Cache constants
         *
         * @return boolean Was the lock successful?
+        * @throws \Exception
         */
-       public static function acquireLock($key, $timeout = 120)
+       public static function acquire($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
        {
-               return self::getDriver()->acquireLock($key, $timeout);
+               return self::getClass(ILock::class)->acquireLock($key, $timeout, $ttl);
        }
 
        /**
         * @brief Releases a lock if it was set by us
         *
-        * @param string $key Name of the lock
-        * @return mixed
+        * @param string $key      Name of the lock
+        * @param bool   $override Overrides the lock to get releases
+        *
+        * @return void
+        * @throws \Exception
         */
-       public static function releaseLock($key)
+       public static function release($key, $override = false)
        {
-               return self::getDriver()->releaseLock($key);
+               return self::getClass(ILock::class)->releaseLock($key, $override);
        }
 
        /**
         * @brief Releases all lock that were set by us
         * @return void
+        * @throws \Exception
         */
        public static function releaseAll()
        {
-               self::getDriver()->releaseAll();
+               self::getClass(ILock::class)->releaseAll();
        }
 }