X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FCore%2FLock.php;h=ef62bc8f76e20770a9f7287226424ae7a8379063;hb=cbe435bb708ccf17a4b5bea1e20c3258c9524700;hp=9892f1f4e4d7e74e2dcab9212da8a09f4943401f;hpb=e41e7d2edd286483b2001b9164ca3039997e7634;p=friendica.git diff --git a/src/Core/Lock.php b/src/Core/Lock.php index 9892f1f4e4..ef62bc8f76 100644 --- a/src/Core/Lock.php +++ b/src/Core/Lock.php @@ -1,140 +1,57 @@ getMessage()); - } - } - - // 2. Try to use Cache Locking (don't use the DB-Cache Locking because it works different!) - $cache_driver = Config::get('system', 'cache_driver', 'database'); - if ($cache_driver != 'database') { - try { - $lock_driver = CacheDriverFactory::create($cache_driver); - if ($lock_driver instanceof IMemoryCacheDriver) { - self::$driver = new Lock\CacheLockDriver($lock_driver); - } - return; - } catch (\Exception $exception) { - logger('Using Cache driver for locking failed: ' . $exception->getMessage()); - } - } - - // 3. Use Database Locking as a Fallback - 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 acquire($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 + * @param string $key Name of the lock + * @param bool $override Overrides the lock to get releases + * * @return void + * @throws \Exception */ - public static function release($key) + public static function release($key, $override = false) { - 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(); } }