3 * @file src/Util/Lock.php
5 namespace Friendica\Core;
8 * @file src/Core/Lock.php
9 * @brief Functions for preventing parallel execution of functions
12 use Friendica\Core\Cache\CacheDriverFactory;
13 use Friendica\Core\Cache\IMemoryCacheDriver;
16 * @brief This class contain Functions for preventing parallel execution of functions
21 * @var Lock\ILockDriver;
23 static $driver = null;
25 public static function init()
27 $lock_driver = Config::get('system', 'lock_driver', 'default');
30 switch ($lock_driver) {
32 $cache_driver = CacheDriverFactory::create('memcache');
33 if ($cache_driver instanceof IMemoryCacheDriver) {
34 self::$driver = new Lock\CacheLockDriver($cache_driver);
39 $cache_driver = CacheDriverFactory::create('memcached');
40 if ($cache_driver instanceof IMemoryCacheDriver) {
41 self::$driver = new Lock\CacheLockDriver($cache_driver);
46 $cache_driver = CacheDriverFactory::create('redis');
47 if ($cache_driver instanceof IMemoryCacheDriver) {
48 self::$driver = new Lock\CacheLockDriver($cache_driver);
53 self::$driver = new Lock\DatabaseLockDriver();
57 self::$driver = new Lock\SemaphoreLockDriver();
61 self::useAutoDriver();
63 } catch (\Exception $exception) {
64 logger ('Driver \'' . $lock_driver . '\' failed - Fallback to \'useAutoDriver()\'');
65 self::useAutoDriver();
70 * @brief This method tries to find the best - local - locking method for Friendica
72 * The following sequence will be tried:
73 * 1. Semaphore Locking
78 private static function useAutoDriver() {
80 // 1. Try to use Semaphores for - local - locking
81 if (function_exists('sem_get')) {
83 self::$driver = new Lock\SemaphoreLockDriver();
85 } catch (\Exception $exception) {
86 logger ('Using Semaphore driver for locking failed: ' . $exception->getMessage());
90 // 2. Try to use Cache Locking (don't use the DB-Cache Locking because it works different!)
91 $cache_driver = Config::get('system', 'cache_driver', 'database');
92 if ($cache_driver != 'database') {
94 $lock_driver = CacheDriverFactory::create($cache_driver);
95 if ($lock_driver instanceof IMemoryCacheDriver) {
96 self::$driver = new Lock\CacheLockDriver($lock_driver);
99 } catch (\Exception $exception) {
100 logger('Using Cache driver for locking failed: ' . $exception->getMessage());
104 // 3. Use Database Locking as a Fallback
105 self::$driver = new Lock\DatabaseLockDriver();
109 * Returns the current cache driver
111 * @return Lock\ILockDriver;
113 private static function getDriver()
115 if (self::$driver === null) {
119 return self::$driver;
123 * @brief Acquires a lock for a given name
125 * @param string $key Name of the lock
126 * @param integer $timeout Seconds until we give up
128 * @return boolean Was the lock successful?
130 public static function acquireLock($key, $timeout = 120)
132 return self::getDriver()->acquireLock($key, $timeout);
136 * @brief Releases a lock if it was set by us
138 * @param string $key Name of the lock
141 public static function releaseLock($key)
143 return self::getDriver()->releaseLock($key);
147 * @brief Releases all lock that were set by us
150 public static function releaseAll()
152 self::getDriver()->releaseAll();