--- /dev/null
+<?php
+
+namespace Friendica\Core;
+
+use Friendica\Core\Lock\ILock;
+
+/**
+ * Basic class for Locking with common functions (local acquired locks, releaseAll, ..)
+ */
+abstract class BaseLock implements ILock
+{
+ /**
+ * @var array The local acquired locks
+ */
+ protected $acquiredLocks = [];
+
+ /**
+ * Check if we've locally acquired a lock
+ *
+ * @param string key The Name of the lock
+ *
+ * @return bool Returns true if the lock is set
+ */
+ protected function hasAcquiredLock($key)
+ {
+ return isset($this->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;
+ }
+}
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
/**
* (@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();
namespace Friendica\Core\Lock;
+use Friendica\Core\BaseLock;
use Friendica\Core\Cache\Duration;
use Friendica\Database\Database;
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
*/
public function getName()
{
- return self::TYPE_DATABASE;
+ return Type::DATABASE;
}
/**
namespace Friendica\Core\Lock;
-use Friendica\Core\Cache;
+use Friendica\Core\Cache\Duration;
/**
* Lock Interface
*
* @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
+++ /dev/null
-<?php
-
-namespace Friendica\Core\Lock;
-
-use Friendica\Core\Cache\Type;
-
-/**
- * Class AbstractLock
- *
- * @package Friendica\Core\Lock
- *
- * Basic class for Locking with common functions (local acquired locks, releaseAll, ..)
- */
-abstract class Lock implements ILock
-{
- const TYPE_DATABASE = Type::DATABASE;
- const TYPE_SEMAPHORE = 'semaphore';
-
- /**
- * @var array The local acquired locks
- */
- protected $acquiredLocks = [];
-
- /**
- * Check if we've locally acquired a lock
- *
- * @param string key The Name of the lock
- *
- * @return bool Returns true if the lock is set
- */
- protected function hasAcquiredLock($key)
- {
- return isset($this->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;
- }
-}
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 = [];
/**
* (@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])) {
*/
public function getName()
{
- return self::TYPE_SEMAPHORE;
+ return Type::SEMAPHORE;
}
/**
--- /dev/null
+<?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';
+}