mirror of
https://github.com/friendica/friendica
synced 2025-04-25 12:30:11 +00:00
Lock abstraction (like the Cache)
- adding interface - adding seperate drivers - moving Lock to the Core package
This commit is contained in:
parent
258683b49a
commit
0218d16335
8 changed files with 396 additions and 223 deletions
97
src/Core/Lock.php
Normal file
97
src/Core/Lock.php
Normal file
|
@ -0,0 +1,97 @@
|
|||
<?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;
|
||||
|
||||
require_once 'include/dba.php';
|
||||
|
||||
/**
|
||||
* @brief This class contain Functions for preventing parallel execution of functions
|
||||
*/
|
||||
class Lock
|
||||
{
|
||||
/**
|
||||
* @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 integer $timeout Seconds until we give up
|
||||
*
|
||||
* @return boolean Was the lock successful?
|
||||
*/
|
||||
public static function acquireLock($key, $timeout = 120)
|
||||
{
|
||||
return self::getDriver()->acquireLock($key, $timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Releases a lock if it was set by us
|
||||
*
|
||||
* @param string $key Name of the lock
|
||||
* @return mixed
|
||||
*/
|
||||
public static function releaseLock($key)
|
||||
{
|
||||
return self::getDriver()->releaseLock($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Releases all lock that were set by us
|
||||
* @return void
|
||||
*/
|
||||
public static function releaseAll()
|
||||
{
|
||||
self::getDriver()->releaseAll();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue