Merge pull request #8131 from nupplaphil/task/cleanup_lock

CleanUp Lock namespace
This commit is contained in:
Hypolite Petovan 2020-01-18 14:13:14 -05:00 committed by GitHub
commit 8536b313a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 33 additions and 21 deletions

View File

@ -1,21 +1,14 @@
<?php <?php
namespace Friendica\Core\Lock; namespace Friendica\Core;
use Friendica\Core\Cache\Type; use Friendica\Core\Lock\ILock;
/** /**
* Class AbstractLock
*
* @package Friendica\Core\Lock
*
* Basic class for Locking with common functions (local acquired locks, releaseAll, ..) * Basic class for Locking with common functions (local acquired locks, releaseAll, ..)
*/ */
abstract class Lock implements ILock abstract class BaseLock implements ILock
{ {
const TYPE_DATABASE = Type::DATABASE;
const TYPE_SEMAPHORE = 'semaphore';
/** /**
* @var array The local acquired locks * @var array The local acquired locks
*/ */

View File

@ -2,10 +2,11 @@
namespace Friendica\Core\Lock; namespace Friendica\Core\Lock;
use Friendica\Core\Cache; use Friendica\Core\BaseLock;
use Friendica\Core\Cache\Duration;
use Friendica\Core\Cache\IMemoryCache; use Friendica\Core\Cache\IMemoryCache;
class CacheLock extends Lock class CacheLock extends BaseLock
{ {
/** /**
* @var string The static prefix of all locks inside the cache * @var string The static prefix of all locks inside the cache
@ -30,7 +31,7 @@ class CacheLock extends Lock
/** /**
* (@inheritdoc) * (@inheritdoc)
*/ */
public function acquire($key, $timeout = 120, $ttl = Cache\Duration::FIVE_MINUTES) public function acquire($key, $timeout = 120, $ttl = Duration::FIVE_MINUTES)
{ {
$got_lock = false; $got_lock = false;
$start = time(); $start = time();

View File

@ -2,6 +2,7 @@
namespace Friendica\Core\Lock; namespace Friendica\Core\Lock;
use Friendica\Core\BaseLock;
use Friendica\Core\Cache\Duration; use Friendica\Core\Cache\Duration;
use Friendica\Database\Database; use Friendica\Database\Database;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
@ -9,7 +10,7 @@ use Friendica\Util\DateTimeFormat;
/** /**
* Locking driver that stores the locks in the database * Locking driver that stores the locks in the database
*/ */
class DatabaseLock extends Lock class DatabaseLock extends BaseLock
{ {
/** /**
* The current ID of the process * The current ID of the process
@ -131,7 +132,7 @@ class DatabaseLock extends Lock
*/ */
public function getName() public function getName()
{ {
return self::TYPE_DATABASE; return Type::DATABASE;
} }
/** /**

View File

@ -2,7 +2,7 @@
namespace Friendica\Core\Lock; namespace Friendica\Core\Lock;
use Friendica\Core\Cache; use Friendica\Core\Cache\Duration;
/** /**
* Lock Interface * Lock Interface
@ -30,7 +30,7 @@ interface ILock
* *
* @return boolean Was the lock successful? * @return boolean Was the lock successful?
*/ */
public function acquire($key, $timeout = 120, $ttl = Cache\Duration::FIVE_MINUTES); public function acquire($key, $timeout = 120, $ttl = Duration::FIVE_MINUTES);
/** /**
* Releases a lock if it was set by us * Releases a lock if it was set by us

View File

@ -2,9 +2,10 @@
namespace Friendica\Core\Lock; namespace Friendica\Core\Lock;
use Friendica\Core\Cache; use Friendica\Core\BaseLock;
use Friendica\Core\Cache\Duration;
class SemaphoreLock extends Lock class SemaphoreLock extends BaseLock
{ {
private static $semaphore = []; private static $semaphore = [];
@ -36,7 +37,7 @@ class SemaphoreLock extends Lock
/** /**
* (@inheritdoc) * (@inheritdoc)
*/ */
public function acquire($key, $timeout = 120, $ttl = Cache\Duration::FIVE_MINUTES) public function acquire($key, $timeout = 120, $ttl = Duration::FIVE_MINUTES)
{ {
self::$semaphore[$key] = sem_get(self::semaphoreKey($key)); self::$semaphore[$key] = sem_get(self::semaphoreKey($key));
if (!empty(self::$semaphore[$key])) { if (!empty(self::$semaphore[$key])) {
@ -85,7 +86,7 @@ class SemaphoreLock extends Lock
*/ */
public function getName() public function getName()
{ {
return self::TYPE_SEMAPHORE; return Type::SEMAPHORE;
} }
/** /**

16
src/Core/Lock/Type.php Normal file
View File

@ -0,0 +1,16 @@
<?php
namespace Friendica\Core\Lock;
use Friendica\Core\Cache\Type as CacheType;
/**
* Enumeration for lock types
*
* There's no "Cache" lock type, because the type depends on the concrete, used cache
*/
abstract class Type
{
const DATABASE = CacheType::DATABASE;
const SEMAPHORE = 'semaphore';
}