mirror of
https://github.com/friendica/friendica
synced 2025-04-27 09:10:12 +00:00
Refactor Cache/Lock to DICE
- Refactor Cache classes - Refactor Lock classes - Improved test speed (removed some seperate class annotations)
This commit is contained in:
parent
b95d4f41b9
commit
d56bd28a07
40 changed files with 766 additions and 621 deletions
|
@ -4,7 +4,11 @@ namespace Friendica\Factory;
|
|||
|
||||
use Friendica\Core\Cache;
|
||||
use Friendica\Core\Cache\ICacheDriver;
|
||||
use Friendica\Core\Config;
|
||||
use Friendica\Core\Config\Configuration;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Util\BaseURL;
|
||||
use Friendica\Util\Profiler;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Class CacheDriverFactory
|
||||
|
@ -15,43 +19,79 @@ use Friendica\Core\Config;
|
|||
*/
|
||||
class CacheDriverFactory
|
||||
{
|
||||
/**
|
||||
* @var string The default driver for caching
|
||||
*/
|
||||
const DEFAULT_DRIVER = 'database';
|
||||
|
||||
/**
|
||||
* @var Configuration The configuration to read parameters out of the config
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var Database The database connection in case that the cache is used the dba connection
|
||||
*/
|
||||
private $dba;
|
||||
|
||||
/**
|
||||
* @var string The hostname, used as Prefix for Caching
|
||||
*/
|
||||
private $hostname;
|
||||
|
||||
/**
|
||||
* @var Profiler The optional profiler if the cached should be profiled
|
||||
*/
|
||||
private $profiler;
|
||||
|
||||
/**
|
||||
* @var LoggerInterface The Friendica Logger
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
public function __construct(BaseURL $baseURL, Configuration $config, Database $dba, Profiler $profiler, LoggerInterface $logger)
|
||||
{
|
||||
$this->hostname = $baseURL->getHostname();
|
||||
$this->config = $config;
|
||||
$this->dba = $dba;
|
||||
$this->profiler = $profiler;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @throws \Exception The exception if something went wrong during the CacheDriver creation
|
||||
*/
|
||||
public static function create($driver) {
|
||||
public function create()
|
||||
{
|
||||
$driver = $this->config->get('system', 'cache_driver', self::DEFAULT_DRIVER);
|
||||
|
||||
switch ($driver) {
|
||||
case 'memcache':
|
||||
$memcache_host = Config::get('system', 'memcache_host');
|
||||
$memcache_port = Config::get('system', 'memcache_port');
|
||||
|
||||
return new Cache\MemcacheCacheDriver($memcache_host, $memcache_port);
|
||||
$cache = new Cache\MemcacheCacheDriver($this->hostname, $this->config);
|
||||
break;
|
||||
|
||||
case 'memcached':
|
||||
$memcached_hosts = Config::get('system', 'memcached_hosts');
|
||||
|
||||
return new Cache\MemcachedCacheDriver($memcached_hosts);
|
||||
$cache = new Cache\MemcachedCacheDriver($this->hostname, $this->config, $this->logger);
|
||||
break;
|
||||
case 'redis':
|
||||
$redis_host = Config::get('system', 'redis_host');
|
||||
$redis_port = Config::get('system', 'redis_port');
|
||||
$redis_pw = Config::get('system', 'redis_password');
|
||||
$redis_db = Config::get('system', 'redis_db', 0);
|
||||
|
||||
return new Cache\RedisCacheDriver($redis_host, $redis_port, $redis_db, $redis_pw);
|
||||
$cache = new Cache\RedisCacheDriver($this->hostname, $this->config);
|
||||
break;
|
||||
|
||||
case 'apcu':
|
||||
return new Cache\APCuCache();
|
||||
$cache = new Cache\APCuCache($this->hostname);
|
||||
break;
|
||||
|
||||
default:
|
||||
return new Cache\DatabaseCacheDriver();
|
||||
$cache = new Cache\DatabaseCacheDriver($this->hostname, $this->dba);
|
||||
}
|
||||
|
||||
$profiling = $this->config->get('system', 'profiling', false);
|
||||
|
||||
// In case profiling is enabled, wrap the ProfilerCache around the current cache
|
||||
if (isset($profiling) && $profiling !== false) {
|
||||
return new Cache\ProfilerCache($cache, $this->profiler);
|
||||
} else {
|
||||
return $cache;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
128
src/Factory/LockDriverFactory.php
Normal file
128
src/Factory/LockDriverFactory.php
Normal file
|
@ -0,0 +1,128 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Factory;
|
||||
|
||||
use Friendica\Core\Cache\ICacheDriver;
|
||||
use Friendica\Core\Cache\IMemoryCacheDriver;
|
||||
use Friendica\Core\Config\Configuration;
|
||||
use Friendica\Core\Lock;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Util\Profiler;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Class LockDriverFactory
|
||||
*
|
||||
* @package Friendica\Core\Cache
|
||||
*
|
||||
* A basic class to generate a LockDriver
|
||||
*/
|
||||
class LockDriverFactory
|
||||
{
|
||||
/**
|
||||
* @var string The default driver for caching
|
||||
*/
|
||||
const DEFAULT_DRIVER = 'default';
|
||||
|
||||
/**
|
||||
* @var Configuration The configuration to read parameters out of the config
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var Database The database connection in case that the cache is used the dba connection
|
||||
*/
|
||||
private $dba;
|
||||
|
||||
/**
|
||||
* @var ICacheDriver The memory cache driver in case we use it
|
||||
*/
|
||||
private $cacheDriver;
|
||||
|
||||
/**
|
||||
* @var Profiler The optional profiler if the cached should be profiled
|
||||
*/
|
||||
private $profiler;
|
||||
|
||||
/**
|
||||
* @var LoggerInterface The Friendica Logger
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
public function __construct(ICacheDriver $cacheDriver, Configuration $config, Database $dba, Profiler $profiler, LoggerInterface $logger)
|
||||
{
|
||||
$this->cacheDriver = $cacheDriver;
|
||||
$this->config = $config;
|
||||
$this->dba = $dba;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$lock_driver = $this->config->get('system', 'lock_driver', self::DEFAULT_DRIVER);
|
||||
|
||||
try {
|
||||
switch ($lock_driver) {
|
||||
case 'memcache':
|
||||
case 'memcached':
|
||||
case 'redis':
|
||||
if ($this->cacheDriver instanceof IMemoryCacheDriver) {
|
||||
return new Lock\CacheLockDriver($this->cacheDriver);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'database':
|
||||
return new Lock\DatabaseLockDriver($this->dba);
|
||||
break;
|
||||
|
||||
case 'semaphore':
|
||||
return new Lock\SemaphoreLockDriver();
|
||||
break;
|
||||
|
||||
default:
|
||||
return self::useAutoDriver();
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
$this->logger->alert('Driver \'' . $lock_driver . '\' failed - Fallback to \'useAutoDriver()\'', ['exception' => $exception]);
|
||||
return self::useAutoDriver();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This method tries to find the best - local - locking method for Friendica
|
||||
*
|
||||
* The following sequence will be tried:
|
||||
* 1. Semaphore Locking
|
||||
* 2. Cache Locking
|
||||
* 3. Database Locking
|
||||
*
|
||||
* @return Lock\ILockDriver
|
||||
*/
|
||||
private function useAutoDriver()
|
||||
{
|
||||
|
||||
// 1. Try to use Semaphores for - local - locking
|
||||
if (function_exists('sem_get')) {
|
||||
try {
|
||||
return new Lock\SemaphoreLockDriver();
|
||||
} catch (\Exception $exception) {
|
||||
$this->logger->debug('Using Semaphore driver for locking failed.', ['exception' => $exception]);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Try to use Cache Locking (don't use the DB-Cache Locking because it works different!)
|
||||
$cache_driver = $this->config->get('system', 'cache_driver', 'database');
|
||||
if ($cache_driver != 'database') {
|
||||
try {
|
||||
if ($this->cacheDriver instanceof IMemoryCacheDriver) {
|
||||
return new Lock\CacheLockDriver($this->cacheDriver);
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
$this->logger->debug('Using Cache driver for locking failed.', ['exception' => $exception]);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Use Database Locking as a Fallback
|
||||
return new Lock\DatabaseLockDriver($this->dba);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue