From: nupplaPhil Date: Sat, 18 Jan 2020 19:15:46 +0000 (+0100) Subject: CleanUp Lock namespace X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=41ff43dcdd5a50a549734e81ce7e90829d06b186;p=friendica.git CleanUp Lock namespace - introduce enum Lock - "Type" - Move Lock/Lock to BaseLock --- diff --git a/src/Core/BaseLock.php b/src/Core/BaseLock.php new file mode 100644 index 0000000000..2fcf7cad70 --- /dev/null +++ b/src/Core/BaseLock.php @@ -0,0 +1,64 @@ +acquireLock[$key]) && $this->acquiredLocks[$key] === true; + } + + /** + * Mark a locally acquired lock + * + * @param string $key The Name of the lock + */ + protected function markAcquire($key) + { + $this->acquiredLocks[$key] = true; + } + + /** + * Mark a release of a locally acquired lock + * + * @param string $key The Name of the lock + */ + protected function markRelease($key) + { + unset($this->acquiredLocks[$key]); + } + + /** + * {@inheritDoc} + */ + public function releaseAll($override = false) + { + $return = true; + + foreach ($this->acquiredLocks as $acquiredLock => $hasLock) { + if (!$this->release($acquiredLock, $override)) { + $return = false; + } + } + + return $return; + } +} diff --git a/src/Core/Lock/CacheLock.php b/src/Core/Lock/CacheLock.php index 3fec76562c..fd04bda80f 100644 --- a/src/Core/Lock/CacheLock.php +++ b/src/Core/Lock/CacheLock.php @@ -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(); diff --git a/src/Core/Lock/DatabaseLock.php b/src/Core/Lock/DatabaseLock.php index ca72db56b2..df1b9295c9 100644 --- a/src/Core/Lock/DatabaseLock.php +++ b/src/Core/Lock/DatabaseLock.php @@ -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; } /** diff --git a/src/Core/Lock/ILock.php b/src/Core/Lock/ILock.php index 0d3fe55fd2..a1a3f13e7f 100644 --- a/src/Core/Lock/ILock.php +++ b/src/Core/Lock/ILock.php @@ -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 diff --git a/src/Core/Lock/Lock.php b/src/Core/Lock/Lock.php deleted file mode 100644 index a124340c75..0000000000 --- a/src/Core/Lock/Lock.php +++ /dev/null @@ -1,71 +0,0 @@ -acquireLock[$key]) && $this->acquiredLocks[$key] === true; - } - - /** - * Mark a locally acquired lock - * - * @param string $key The Name of the lock - */ - protected function markAcquire($key) - { - $this->acquiredLocks[$key] = true; - } - - /** - * Mark a release of a locally acquired lock - * - * @param string $key The Name of the lock - */ - protected function markRelease($key) - { - unset($this->acquiredLocks[$key]); - } - - /** - * {@inheritDoc} - */ - public function releaseAll($override = false) - { - $return = true; - - foreach ($this->acquiredLocks as $acquiredLock => $hasLock) { - if (!$this->release($acquiredLock, $override)) { - $return = false; - } - } - - return $return; - } -} diff --git a/src/Core/Lock/SemaphoreLock.php b/src/Core/Lock/SemaphoreLock.php index 297adad31e..86f106d56d 100644 --- a/src/Core/Lock/SemaphoreLock.php +++ b/src/Core/Lock/SemaphoreLock.php @@ -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; } /** diff --git a/src/Core/Lock/Type.php b/src/Core/Lock/Type.php new file mode 100644 index 0000000000..fdee39cabe --- /dev/null +++ b/src/Core/Lock/Type.php @@ -0,0 +1,16 @@ +