Browse Source

CleanUp Lock namespace

- introduce enum Lock - "Type"
- Move Lock/Lock to BaseLock
pull/8131/head
nupplaPhil 1 month ago
parent
commit
41ff43dcdd
No account linked to committer's email address
6 changed files with 33 additions and 21 deletions
  1. +3
    -10
      src/Core/BaseLock.php
  2. +4
    -3
      src/Core/Lock/CacheLock.php
  3. +3
    -2
      src/Core/Lock/DatabaseLock.php
  4. +2
    -2
      src/Core/Lock/ILock.php
  5. +5
    -4
      src/Core/Lock/SemaphoreLock.php
  6. +16
    -0
      src/Core/Lock/Type.php

src/Core/Lock/Lock.php → src/Core/BaseLock.php View File

@@ -1,21 +1,14 @@
<?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, ..)
*/
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
*/

+ 4
- 3
src/Core/Lock/CacheLock.php View File

@@ -2,10 +2,11 @@

namespace Friendica\Core\Lock;

use Friendica\Core\Cache;
use Friendica\Core\BaseLock;
use Friendica\Core\Cache\Duration;
use Friendica\Core\Cache\IMemoryCache;

class CacheLock extends Lock
class CacheLock extends BaseLock
{
/**
* @var string The static prefix of all locks inside the cache
@@ -30,7 +31,7 @@ class CacheLock extends Lock
/**
* (@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;
$start = time();

+ 3
- 2
src/Core/Lock/DatabaseLock.php View File

@@ -2,6 +2,7 @@

namespace Friendica\Core\Lock;

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

/**

+ 2
- 2
src/Core/Lock/ILock.php View File

@@ -2,7 +2,7 @@

namespace Friendica\Core\Lock;

use Friendica\Core\Cache;
use Friendica\Core\Cache\Duration;

/**
* Lock Interface
@@ -30,7 +30,7 @@ interface ILock
*
* @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

+ 5
- 4
src/Core/Lock/SemaphoreLock.php View File

@@ -2,9 +2,10 @@

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 = [];

@@ -36,7 +37,7 @@ class SemaphoreLock extends Lock
/**
* (@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));
if (!empty(self::$semaphore[$key])) {
@@ -85,7 +86,7 @@ class SemaphoreLock extends Lock
*/
public function getName()
{
return self::TYPE_SEMAPHORE;
return Type::SEMAPHORE;
}

/**

+ 16
- 0
src/Core/Lock/Type.php 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';
}

Loading…
Cancel
Save