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-04 15:58:53 +02:00
|
|
|
use Friendica\Core\Cache\Cache;
|
2019-12-15 23:28:01 +01:00
|
|
|
use Friendica\DI;
|
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-12-15 23:28:01 +01:00
|
|
|
class Lock
|
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-04 15:58:53 +02:00
|
|
|
public static function acquire($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
|
2018-06-28 22:57:17 +02:00
|
|
|
{
|
2019-12-15 23:28:01 +01:00
|
|
|
return DI::lock()->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
|
|
|
*
|
2019-12-15 23:28:01 +01:00
|
|
|
* @return bool
|
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-12-15 23:28:01 +01:00
|
|
|
return DI::lock()->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-12-15 23:28:01 +01:00
|
|
|
DI::lock()->releaseAll();
|
2018-06-28 22:57:17 +02:00
|
|
|
}
|
|
|
|
}
|