use Friendica\Core\Cache;
/**
- * @brief Implementation of the IMemoryCacheDriver mainly for testing purpose
+ * Implementation of the IMemoryCacheDriver mainly for testing purpose
*
* Class ArrayCache
*
return false;
}
}
-}
\ No newline at end of file
+}
*
* @package Friendica\Core\Cache
*
- * @brief A basic class to generate a CacheDriver
+ * A basic class to generate a CacheDriver
*/
class CacheDriverFactory
{
/**
- * @brief This method creates a CacheDriver for the given cache driver name
+ * This method creates a CacheDriver for the given cache driver name
*
* @param string $driver The name of the cache driver
* @return ICacheDriver The instance of the CacheDriver
interface ICacheDriver
{
/**
- * @brief Fetches cached data according to the key
+ * Fetches cached data according to the key
*
* @param string $key The key to the cached data
*
public function get($key);
/**
- * @brief Stores data in the cache identified by the key. The input $value can have multiple formats.
+ * Stores data in the cache identified by the key. The input $value can have multiple formats.
*
* @param string $key The cache key
* @param mixed $value The value to store
- * @param integer $ttl The cache lifespan, must be one of the Cache constants
+ * @param integer $ttl The cache lifespan, must be one of the Cache constants
*
* @return bool
*/
public function set($key, $value, $ttl = Cache::FIVE_MINUTES);
/**
- * @brief Delete a key from the cache
+ * Delete a key from the cache
*
* @param string $key The cache key
*
public function delete($key);
/**
- * @brief Remove outdated data from the cache
+ * Remove outdated data from the cache
*
* @return bool
*/
use Friendica\Core\Cache;
/**
- * @brief This interface defines methods for Memory-Caches only
+ * This interface defines methods for Memory-Caches only
*
* Interface IMemoryCacheDriver
*
interface IMemoryCacheDriver extends ICacheDriver
{
/**
- * @brief Sets a value if it's not already stored
+ * Sets a value if it's not already stored
*
* @param string $key The cache key
* @param mixed $value The old value we know from the cache
public function add($key, $value, $ttl = Cache::FIVE_MINUTES);
/**
- * @brief Compares if the old value is set and sets the new value
+ * Compares if the old value is set and sets the new value
*
* @param string $key The cache key
* @param mixed $oldValue The old value we know from the cache
public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES);
/**
- * @brief Compares if the old value is set and removes it
+ * Compares if the old value is set and removes it
*
* @param string $key The cache key
* @param mixed $value The old value we know and want to delete
* @return bool
*/
public function compareDelete($key, $value);
-}
\ No newline at end of file
+}
return $this->memcache->delete($key);
}
+ /**
+ * (@inheritdoc)
+ */
public function clear()
{
return $this->memcache->flush();
/**
- * @brief Sets a value if it's not already stored
- *
- * @param string $key The cache key
- * @param mixed $value The old value we know from the cache
- * @param int $ttl The cache lifespan, must be one of the Cache constants
- * @return bool
+ * (@inheritdoc)
*/
public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
{
}
/**
- * @brief Compares if the old value is set and sets the new value
- *
- * @param string $key The cache key
- * @param mixed $oldValue The old value we know
- * @param mixed $newValue The new value we want to set
- * @param int $ttl The cache lifespan, must be one of the Cache constants
- * @return bool
+ * (@inheritdoc)
*/
public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES)
{
return false;
}
/**
- * @brief Compares if the old value is set and removes it
- *
- * @param string $key The cache key
- * @param mixed $value The old value we know and want to delete
- * @return bool
+ * (@inheritdoc)
*/
public function compareDelete($key, $value)
{
/**
* Trait TraitCompareSetDelete
*
- * @brief This Trait is to compensate non native "exclusive" sets/deletes in caches
+ * This Trait is to compensate non native "exclusive" sets/deletes in caches
*
* @package Friendica\Core\Cache
*/
abstract public function add($key, $value, $ttl = Cache::FIVE_MINUTES);
/**
- * @brief NonNative - Compares if the old value is set and removes it
+ * NonNative - Compares if the old value is set and removes it
*
* @param string $key The cache key
* @param mixed $value The old value we know and want to delete
return false;
}
}
-}
\ No newline at end of file
+}
/**
* Trait TraitCompareSetDelete
*
- * @brief This Trait is to compensate non native "exclusive" sets/deletes in caches
+ * This Trait is to compensate non native "exclusive" sets/deletes in caches
*
* @package Friendica\Core\Cache
*/
abstract public function add($key, $value, $ttl = Cache::FIVE_MINUTES);
/**
- * @brief NonNative - Compares if the old value is set and sets the new value
+ * NonNative - Compares if the old value is set and sets the new value
*
* @param string $key The cache key
* @param mixed $oldValue The old value we know from the cache
return false;
}
}
-}
\ No newline at end of file
+}
try {
switch ($lock_driver) {
case 'memcache':
- $cache_driver = CacheDriverFactory::create('memcache');
- if ($cache_driver instanceof IMemoryCacheDriver) {
- self::$driver = new Lock\CacheLockDriver($cache_driver);
- }
- break;
-
case 'memcached':
- $cache_driver = CacheDriverFactory::create('memcached');
- if ($cache_driver instanceof IMemoryCacheDriver) {
- self::$driver = new Lock\CacheLockDriver($cache_driver);
- }
- break;
-
case 'redis':
- $cache_driver = CacheDriverFactory::create('redis');
+ $cache_driver = CacheDriverFactory::create($lock_driver);
if ($cache_driver instanceof IMemoryCacheDriver) {
self::$driver = new Lock\CacheLockDriver($cache_driver);
}
*/
public static function acquireLock($key, $timeout = 120)
{
- return self::getDriver()->acquireLock($key, $timeout);
+ return self::getDriver()->acquire($key, $timeout);
}
/**
*/
public static function releaseLock($key)
{
- return self::getDriver()->releaseLock($key);
+ return self::getDriver()->release($key);
}
/**
*
* @package Friendica\Core\Lock
*
- * @brief Basic class for Locking with common functions (local acquired locks, releaseAll, ..)
+ * Basic class for Locking with common functions (local acquired locks, releaseAll, ..)
*/
abstract class AbstractLockDriver extends BaseObject implements ILockDriver
{
protected $acquiredLocks = [];
/**
- * @brief Check if we've locally acquired a lock
+ * 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
}
/**
- * @brief Mark a locally acquired lock
+ * Mark a locally acquired lock
*
* @param string $key The Name of the lock
*/
}
/**
- * @brief Mark a release of a locally acquired lock
+ * Mark a release of a locally acquired lock
*
* @param string $key The Name of the lock
*/
}
/**
- * @brief Releases all lock that were set by us
+ * Releases all lock that were set by us
*
* @return void
*/
public function releaseAll() {
foreach ($this->acquiredLocks as $acquiredLock => $hasLock) {
- $this->releaseLock($acquiredLock);
+ $this->release($acquiredLock);
}
}
}
}
/**
- *
- * @brief Sets a lock for a given name
- *
- * @param string $key The Name of the lock
- * @param integer $timeout Seconds until we give up
- *
- * @return boolean Was the lock successful?
+ * (@inheritdoc)
*/
- public function acquireLock($key, $timeout = 120)
+ public function acquire($key, $timeout = 120)
{
$got_lock = false;
$start = time();
}
/**
- * @brief Removes a lock if it was set by us
- *
- * @param string $key Name of the lock
+ * (@inheritdoc)
*/
- public function releaseLock($key)
+ public function release($key)
{
$cachekey = self::getCacheKey($key);
}
/**
- * @brief Checks, if a key is currently locked to a process
- *
- * @param string $key The name of the lock
- * @return bool
+ * (@inheritdoc)
*/
public function isLocked($key)
{
/**
* (@inheritdoc)
*/
- public function acquireLock($key, $timeout = 120)
+ public function acquire($key, $timeout = 120)
{
$got_lock = false;
$start = time();
/**
* (@inheritdoc)
*/
- public function releaseLock($key)
+ public function release($key)
{
dba::delete('locks', ['name' => $key, 'pid' => getmypid()]);
interface ILockDriver
{
/**
- * @brief Checks, if a key is currently locked to a or my process
+ * Checks, if a key is currently locked to a or my process
*
* @param string $key The name of the lock
* @return bool
/**
*
- * @brief Acquires a lock for a given name
+ * Acquires a lock for a given name
*
* @param string $key The Name of the lock
* @param integer $timeout Seconds until we give up
*
* @return boolean Was the lock successful?
*/
- public function acquireLock($key, $timeout = 120);
+ public function acquire($key, $timeout = 120);
/**
- * @brief Releases a lock if it was set by us
+ * Releases a lock if it was set by us
*
* @param string $key The Name of the lock
*
* @return void
*/
- public function releaseLock($key);
+ public function release($key);
/**
- * @brief Releases all lock that were set by us
+ * Releases all lock that were set by us
*
* @return void
*/
}
/**
- * @brief Creates a semaphore key
- *
- * @param string $key Name of the lock
- *
- * @return integer the semaphore key
+ * (@inheritdoc)
*/
private static function semaphoreKey($key)
{
/**
*
- * @brief Sets a lock for a given name
- *
- * @param string $key The Name of the lock
- * @param integer $timeout Seconds until we give up
- *
- * @return boolean Was the lock successful?
+ * (@inheritdoc)
*/
- public function acquireLock($key, $timeout = 120)
+ public function acquire($key, $timeout = 120)
{
self::$semaphore[$key] = sem_get(self::semaphoreKey($key));
if (self::$semaphore[$key]) {
}
/**
- * @brief Removes a lock if it was set by us
- *
- * @param string $key Name of the lock
- *
- * @return mixed
+ * (@inheritdoc)
*/
- public function releaseLock($key)
+ public function release($key)
{
if (empty(self::$semaphore[$key])) {
return false;
}
/**
- * @brief Checks, if a key is currently locked to a process
- *
- * @param string $key The name of the lock
- * @return bool
+ * (@inheritdoc)
*/
public function isLocked($key)
{
<?php
-namespace Friendica\Test\src\Core\Lock;
+namespace Friendica\Test\Core\Lock;
use Friendica\Core\Cache\ArrayCache;
<?php
-namespace Friendica\Test\src\Core\Lock;
+namespace Friendica\Test\Core\Lock;
use dba;
use Friendica\Core\Lock\DatabaseLockDriver;
<?php
-namespace Friendica\Test\src\Core\Lock;
+namespace Friendica\Test\Core\Lock;
use Friendica\App;
use Friendica\Core\Config;
}
public function testLock() {
- $this->instance->acquireLock('foo', 1);
+ $this->instance->acquire('foo', 1);
$this->assertTrue($this->instance->isLocked('foo'));
$this->assertFalse($this->instance->isLocked('bar'));
}
public function testDoubleLock() {
- $this->instance->acquireLock('foo', 1);
+ $this->instance->acquire('foo', 1);
$this->assertTrue($this->instance->isLocked('foo'));
// We already locked it
- $this->assertTrue($this->instance->acquireLock('foo', 1));
+ $this->assertTrue($this->instance->acquire('foo', 1));
}
public function testReleaseLock() {
- $this->instance->acquireLock('foo', 1);
+ $this->instance->acquire('foo', 1);
$this->assertTrue($this->instance->isLocked('foo'));
- $this->instance->releaseLock('foo');
+ $this->instance->release('foo');
$this->assertFalse($this->instance->isLocked('foo'));
}
public function testReleaseAll() {
- $this->instance->acquireLock('foo', 1);
- $this->instance->acquireLock('bar', 1);
- $this->instance->acquireLock('#/$%§', 1);
+ $this->instance->acquire('foo', 1);
+ $this->instance->acquire('bar', 1);
+ $this->instance->acquire('#/$%§', 1);
$this->instance->releaseAll();
}
public function testReleaseAfterUnlock() {
- $this->instance->acquireLock('foo', 1);
- $this->instance->acquireLock('bar', 1);
- $this->instance->acquireLock('#/$%§', 1);
+ $this->instance->acquire('foo', 1);
+ $this->instance->acquire('bar', 1);
+ $this->instance->acquire('#/$%§', 1);
- $this->instance->releaseLock('foo');
+ $this->instance->release('foo');
$this->instance->releaseAll();
<?php
-namespace Friendica\Test\src\Core\Lock;
+namespace Friendica\Test\Core\Lock;
use Friendica\Core\Lock\SemaphoreLockDriver;