code standards / simplifications
This commit is contained in:
parent
19209f6826
commit
906bb25972
18 changed files with 70 additions and 121 deletions
|
@ -6,7 +6,7 @@ namespace Friendica\Core\Cache;
|
|||
use Friendica\Core\Cache;
|
||||
|
||||
/**
|
||||
* @brief Implementation of the IMemoryCacheDriver mainly for testing purpose
|
||||
* Implementation of the IMemoryCacheDriver mainly for testing purpose
|
||||
*
|
||||
* Class ArrayCache
|
||||
*
|
||||
|
@ -80,4 +80,4 @@ class ArrayCache implements IMemoryCacheDriver
|
|||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,12 +9,12 @@ use Friendica\Core\Config;
|
|||
*
|
||||
* @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
|
||||
|
|
|
@ -12,7 +12,7 @@ use Friendica\Core\Cache;
|
|||
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
|
||||
*
|
||||
|
@ -21,18 +21,18 @@ interface ICacheDriver
|
|||
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
|
||||
*
|
||||
|
@ -41,7 +41,7 @@ interface ICacheDriver
|
|||
public function delete($key);
|
||||
|
||||
/**
|
||||
* @brief Remove outdated data from the cache
|
||||
* Remove outdated data from the cache
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
|
|
|
@ -4,7 +4,7 @@ namespace Friendica\Core\Cache;
|
|||
use Friendica\Core\Cache;
|
||||
|
||||
/**
|
||||
* @brief This interface defines methods for Memory-Caches only
|
||||
* This interface defines methods for Memory-Caches only
|
||||
*
|
||||
* Interface IMemoryCacheDriver
|
||||
*
|
||||
|
@ -13,7 +13,7 @@ use Friendica\Core\Cache;
|
|||
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
|
||||
|
@ -23,7 +23,7 @@ interface IMemoryCacheDriver extends ICacheDriver
|
|||
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
|
||||
|
@ -35,11 +35,11 @@ interface IMemoryCacheDriver extends ICacheDriver
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -90,6 +90,9 @@ class MemcacheCacheDriver extends BaseObject implements IMemoryCacheDriver
|
|||
return $this->memcache->delete($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
return $this->memcache->flush();
|
||||
|
|
|
@ -84,12 +84,7 @@ class RedisCacheDriver extends BaseObject implements IMemoryCacheDriver
|
|||
|
||||
|
||||
/**
|
||||
* @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)
|
||||
{
|
||||
|
@ -101,13 +96,7 @@ class RedisCacheDriver extends BaseObject implements IMemoryCacheDriver
|
|||
}
|
||||
|
||||
/**
|
||||
* @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)
|
||||
{
|
||||
|
@ -133,11 +122,7 @@ class RedisCacheDriver extends BaseObject implements IMemoryCacheDriver
|
|||
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)
|
||||
{
|
||||
|
|
|
@ -7,7 +7,7 @@ use Friendica\Core\Cache;
|
|||
/**
|
||||
* 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
|
||||
*/
|
||||
|
@ -22,7 +22,7 @@ trait TraitCompareDelete
|
|||
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
|
||||
|
@ -42,4 +42,4 @@ trait TraitCompareDelete
|
|||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ use Friendica\Core\Cache;
|
|||
/**
|
||||
* 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
|
||||
*/
|
||||
|
@ -22,7 +22,7 @@ trait TraitCompareSet
|
|||
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
|
||||
|
@ -45,4 +45,4 @@ trait TraitCompareSet
|
|||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,21 +29,9 @@ class Lock
|
|||
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);
|
||||
}
|
||||
|
@ -129,7 +117,7 @@ class Lock
|
|||
*/
|
||||
public static function acquireLock($key, $timeout = 120)
|
||||
{
|
||||
return self::getDriver()->acquireLock($key, $timeout);
|
||||
return self::getDriver()->acquire($key, $timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -140,7 +128,7 @@ class Lock
|
|||
*/
|
||||
public static function releaseLock($key)
|
||||
{
|
||||
return self::getDriver()->releaseLock($key);
|
||||
return self::getDriver()->release($key);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -8,7 +8,7 @@ use Friendica\BaseObject;
|
|||
*
|
||||
* @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
|
||||
{
|
||||
|
@ -18,7 +18,7 @@ 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
|
||||
|
@ -28,7 +28,7 @@ abstract class AbstractLockDriver extends BaseObject implements ILockDriver
|
|||
}
|
||||
|
||||
/**
|
||||
* @brief Mark a locally acquired lock
|
||||
* Mark a locally acquired lock
|
||||
*
|
||||
* @param string $key The Name of the lock
|
||||
*/
|
||||
|
@ -37,7 +37,7 @@ abstract class AbstractLockDriver extends BaseObject implements ILockDriver
|
|||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
@ -46,13 +46,13 @@ abstract class AbstractLockDriver extends BaseObject implements ILockDriver
|
|||
}
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,15 +22,9 @@ class CacheLockDriver extends AbstractLockDriver
|
|||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @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();
|
||||
|
@ -64,11 +58,9 @@ class CacheLockDriver extends AbstractLockDriver
|
|||
}
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
|
@ -77,10 +69,7 @@ class CacheLockDriver extends AbstractLockDriver
|
|||
}
|
||||
|
||||
/**
|
||||
* @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)
|
||||
{
|
||||
|
|
|
@ -14,7 +14,7 @@ class DatabaseLockDriver extends AbstractLockDriver
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function acquireLock($key, $timeout = 120)
|
||||
public function acquire($key, $timeout = 120)
|
||||
{
|
||||
$got_lock = false;
|
||||
$start = time();
|
||||
|
@ -55,7 +55,7 @@ class DatabaseLockDriver extends AbstractLockDriver
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function releaseLock($key)
|
||||
public function release($key)
|
||||
{
|
||||
dba::delete('locks', ['name' => $key, 'pid' => getmypid()]);
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ namespace Friendica\Core\Lock;
|
|||
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
|
||||
|
@ -19,26 +19,26 @@ interface ILockDriver
|
|||
|
||||
/**
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
|
|
|
@ -14,11 +14,7 @@ class SemaphoreLockDriver extends AbstractLockDriver
|
|||
}
|
||||
|
||||
/**
|
||||
* @brief Creates a semaphore key
|
||||
*
|
||||
* @param string $key Name of the lock
|
||||
*
|
||||
* @return integer the semaphore key
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
private static function semaphoreKey($key)
|
||||
{
|
||||
|
@ -35,14 +31,9 @@ class SemaphoreLockDriver extends AbstractLockDriver
|
|||
|
||||
/**
|
||||
*
|
||||
* @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]) {
|
||||
|
@ -56,13 +47,9 @@ class SemaphoreLockDriver extends AbstractLockDriver
|
|||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
|
@ -75,10 +62,7 @@ class SemaphoreLockDriver extends AbstractLockDriver
|
|||
}
|
||||
|
||||
/**
|
||||
* @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)
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Test\src\Core\Lock;
|
||||
namespace Friendica\Test\Core\Lock;
|
||||
|
||||
|
||||
use Friendica\Core\Cache\ArrayCache;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Test\src\Core\Lock;
|
||||
namespace Friendica\Test\Core\Lock;
|
||||
|
||||
use dba;
|
||||
use Friendica\Core\Lock\DatabaseLockDriver;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Test\src\Core\Lock;
|
||||
namespace Friendica\Test\Core\Lock;
|
||||
|
||||
use Friendica\App;
|
||||
use Friendica\Core\Config;
|
||||
|
@ -34,29 +34,29 @@ abstract class LockTest extends TestCase
|
|||
}
|
||||
|
||||
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();
|
||||
|
||||
|
@ -66,11 +66,11 @@ abstract class LockTest extends TestCase
|
|||
}
|
||||
|
||||
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();
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Test\src\Core\Lock;
|
||||
namespace Friendica\Test\Core\Lock;
|
||||
|
||||
|
||||
use Friendica\Core\Lock\SemaphoreLockDriver;
|
||||
|
|
Loading…
Reference in a new issue