2018-06-28 22:57:17 +02:00
|
|
|
<?php
|
2018-07-05 20:57:31 +02:00
|
|
|
|
2018-06-28 22:57:17 +02:00
|
|
|
/**
|
|
|
|
* @file src/Core/Lock.php
|
|
|
|
* @brief Functions for preventing parallel execution of functions
|
|
|
|
*/
|
|
|
|
|
2019-01-06 22:28:14 +01:00
|
|
|
namespace Friendica\Core;
|
|
|
|
|
2019-08-03 20:48:56 +02:00
|
|
|
use Friendica\BaseObject;
|
|
|
|
use Friendica\Core\Cache\ICacheDriver;
|
|
|
|
use Friendica\Core\Lock\ILockDriver;
|
2018-06-28 22:57:17 +02:00
|
|
|
|
|
|
|
/**
|
2019-08-03 20:48:56 +02:00
|
|
|
* This class contain Functions for preventing parallel execution of functions
|
2018-06-28 22:57:17 +02:00
|
|
|
*/
|
2019-08-03 20:48:56 +02:00
|
|
|
class Lock extends BaseObject
|
2018-06-28 22:57:17 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @brief Acquires a lock for a given name
|
|
|
|
*
|
2019-08-03 20:48:56 +02:00
|
|
|
* @param string $key Name of the lock
|
2018-06-28 22:57:17 +02:00
|
|
|
* @param integer $timeout Seconds until we give up
|
2019-08-03 20:48:56 +02:00
|
|
|
* @param integer $ttl The Lock lifespan, must be one of the Cache constants
|
2018-06-28 22:57:17 +02:00
|
|
|
*
|
|
|
|
* @return boolean Was the lock successful?
|
2019-08-03 20:48:56 +02:00
|
|
|
* @throws \Exception
|
2018-06-28 22:57:17 +02:00
|
|
|
*/
|
2019-08-03 20:48:56 +02:00
|
|
|
public static function acquire($key, $timeout = 120, $ttl = ICacheDriver::FIVE_MINUTES)
|
2018-06-28 22:57:17 +02:00
|
|
|
{
|
2019-08-03 20:48:56 +02:00
|
|
|
return self::getClass(ILockDriver::class)->acquireLock($key, $timeout, $ttl);
|
2018-06-28 22:57:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Releases a lock if it was set by us
|
|
|
|
*
|
2019-02-24 12:24:09 +01:00
|
|
|
* @param string $key Name of the lock
|
|
|
|
* @param bool $override Overrides the lock to get releases
|
2019-08-03 20:48:56 +02:00
|
|
|
*
|
2018-07-05 20:57:31 +02:00
|
|
|
* @return void
|
2019-08-03 20:48:56 +02:00
|
|
|
* @throws \Exception
|
2018-06-28 22:57:17 +02:00
|
|
|
*/
|
2019-02-24 12:24:09 +01:00
|
|
|
public static function release($key, $override = false)
|
2018-06-28 22:57:17 +02:00
|
|
|
{
|
2019-08-03 20:48:56 +02:00
|
|
|
return self::getClass(ILockDriver::class)->releaseLock($key, $override);
|
2018-06-28 22:57:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Releases all lock that were set by us
|
|
|
|
* @return void
|
2019-08-03 20:48:56 +02:00
|
|
|
* @throws \Exception
|
2018-06-28 22:57:17 +02:00
|
|
|
*/
|
|
|
|
public static function releaseAll()
|
|
|
|
{
|
2019-08-03 20:48:56 +02:00
|
|
|
self::getClass(ILockDriver::class)->releaseAll();
|
2018-06-28 22:57:17 +02:00
|
|
|
}
|
|
|
|
}
|