2019-08-03 18:48:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Factory;
|
|
|
|
|
2019-08-04 08:26:53 +00:00
|
|
|
use Friendica\Core\Cache\ICache;
|
|
|
|
use Friendica\Core\Cache\IMemoryCache;
|
2019-08-03 18:48:56 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
2019-08-04 08:26:53 +00:00
|
|
|
* @var ICache The memory cache driver in case we use it
|
2019-08-03 18:48:56 +00:00
|
|
|
*/
|
|
|
|
private $cacheDriver;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Profiler The optional profiler if the cached should be profiled
|
|
|
|
*/
|
|
|
|
private $profiler;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var LoggerInterface The Friendica Logger
|
|
|
|
*/
|
|
|
|
private $logger;
|
|
|
|
|
2019-08-04 08:26:53 +00:00
|
|
|
public function __construct(ICache $cacheDriver, Configuration $config, Database $dba, Profiler $profiler, LoggerInterface $logger)
|
2019-08-03 18:48:56 +00:00
|
|
|
{
|
|
|
|
$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':
|
2019-08-04 08:26:53 +00:00
|
|
|
if ($this->cacheDriver instanceof IMemoryCache) {
|
2019-08-03 18:48:56 +00:00
|
|
|
return new Lock\CacheLockDriver($this->cacheDriver);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'database':
|
2019-08-04 08:26:53 +00:00
|
|
|
return new Lock\DatabaseLock($this->dba);
|
2019-08-03 18:48:56 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'semaphore':
|
2019-08-04 08:26:53 +00:00
|
|
|
return new Lock\SemaphoreLock();
|
2019-08-03 18:48:56 +00:00
|
|
|
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
|
|
|
|
*
|
2019-08-04 08:26:53 +00:00
|
|
|
* @return Lock\ILock
|
2019-08-03 18:48:56 +00:00
|
|
|
*/
|
|
|
|
private function useAutoDriver()
|
|
|
|
{
|
|
|
|
|
|
|
|
// 1. Try to use Semaphores for - local - locking
|
|
|
|
if (function_exists('sem_get')) {
|
|
|
|
try {
|
2019-08-04 08:26:53 +00:00
|
|
|
return new Lock\SemaphoreLock();
|
2019-08-03 18:48:56 +00:00
|
|
|
} 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 {
|
2019-08-04 08:26:53 +00:00
|
|
|
if ($this->cacheDriver instanceof IMemoryCache) {
|
2019-08-03 18:48:56 +00:00
|
|
|
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
|
2019-08-04 08:26:53 +00:00
|
|
|
return new Lock\DatabaseLock($this->dba);
|
2019-08-03 18:48:56 +00:00
|
|
|
}
|
|
|
|
}
|