2019-08-03 20:48:56 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Factory;
|
|
|
|
|
2019-08-04 15:51:49 +02:00
|
|
|
use Friendica\Core\Cache\Cache;
|
2019-08-04 10:26:53 +02:00
|
|
|
use Friendica\Core\Cache\IMemoryCache;
|
2019-12-19 20:11:07 +01:00
|
|
|
use Friendica\Core\Config\IConfiguration;
|
2019-08-03 20:48:56 +02:00
|
|
|
use Friendica\Core\Lock;
|
|
|
|
use Friendica\Database\Database;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
|
|
|
/**
|
2019-08-04 15:42:39 +02:00
|
|
|
* Class LockFactory
|
2019-08-03 20:48:56 +02:00
|
|
|
*
|
|
|
|
* @package Friendica\Core\Cache
|
|
|
|
*
|
|
|
|
* A basic class to generate a LockDriver
|
|
|
|
*/
|
2019-08-04 15:42:39 +02:00
|
|
|
class LockFactory
|
2019-08-03 20:48:56 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string The default driver for caching
|
|
|
|
*/
|
|
|
|
const DEFAULT_DRIVER = 'default';
|
|
|
|
|
|
|
|
/**
|
2019-12-19 20:11:07 +01:00
|
|
|
* @var IConfiguration The configuration to read parameters out of the config
|
2019-08-03 20:48:56 +02:00
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Database The database connection in case that the cache is used the dba connection
|
|
|
|
*/
|
|
|
|
private $dba;
|
|
|
|
|
|
|
|
/**
|
2019-08-04 15:42:39 +02:00
|
|
|
* @var CacheFactory The memory cache driver in case we use it
|
2019-08-03 20:48:56 +02:00
|
|
|
*/
|
2019-08-04 15:42:39 +02:00
|
|
|
private $cacheFactory;
|
2019-08-03 20:48:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var LoggerInterface The Friendica Logger
|
|
|
|
*/
|
|
|
|
private $logger;
|
|
|
|
|
2019-12-19 20:11:07 +01:00
|
|
|
public function __construct(CacheFactory $cacheFactory, IConfiguration $config, Database $dba, LoggerInterface $logger)
|
2019-08-03 20:48:56 +02:00
|
|
|
{
|
2019-08-04 15:42:39 +02:00
|
|
|
$this->cacheFactory = $cacheFactory;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->dba = $dba;
|
|
|
|
$this->logger = $logger;
|
2019-08-03 20:48:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function create()
|
|
|
|
{
|
2019-08-04 15:42:39 +02:00
|
|
|
$lock_type = $this->config->get('system', 'lock_driver', self::DEFAULT_DRIVER);
|
2019-08-03 20:48:56 +02:00
|
|
|
|
|
|
|
try {
|
2019-08-04 15:42:39 +02:00
|
|
|
switch ($lock_type) {
|
2019-08-04 15:51:49 +02:00
|
|
|
case Cache::TYPE_MEMCACHE:
|
|
|
|
case Cache::TYPE_MEMCACHED:
|
|
|
|
case Cache::TYPE_REDIS:
|
|
|
|
case Cache::TYPE_APCU:
|
2019-08-04 15:42:39 +02:00
|
|
|
$cache = $this->cacheFactory->create($lock_type);
|
|
|
|
if ($cache instanceof IMemoryCache) {
|
|
|
|
return new Lock\CacheLock($cache);
|
|
|
|
} else {
|
|
|
|
throw new \Exception(sprintf('Incompatible cache driver \'%s\' for lock used', $lock_type));
|
2019-08-03 20:48:56 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'database':
|
2019-08-04 10:26:53 +02:00
|
|
|
return new Lock\DatabaseLock($this->dba);
|
2019-08-03 20:48:56 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'semaphore':
|
2019-08-04 10:26:53 +02:00
|
|
|
return new Lock\SemaphoreLock();
|
2019-08-03 20:48:56 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return self::useAutoDriver();
|
|
|
|
}
|
|
|
|
} catch (\Exception $exception) {
|
2019-08-04 15:42:39 +02:00
|
|
|
$this->logger->alert('Driver \'' . $lock_type . '\' failed - Fallback to \'useAutoDriver()\'', ['exception' => $exception]);
|
2019-08-03 20:48:56 +02:00
|
|
|
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 10:26:53 +02:00
|
|
|
* @return Lock\ILock
|
2019-08-03 20:48:56 +02:00
|
|
|
*/
|
|
|
|
private function useAutoDriver()
|
|
|
|
{
|
|
|
|
// 1. Try to use Semaphores for - local - locking
|
|
|
|
if (function_exists('sem_get')) {
|
|
|
|
try {
|
2019-08-04 10:26:53 +02:00
|
|
|
return new Lock\SemaphoreLock();
|
2019-08-03 20:48:56 +02: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!)
|
2019-08-04 15:42:39 +02:00
|
|
|
$cache_type = $this->config->get('system', 'cache_driver', 'database');
|
2019-08-04 15:51:49 +02:00
|
|
|
if ($cache_type != Cache::TYPE_DATABASE) {
|
2019-08-03 20:48:56 +02:00
|
|
|
try {
|
2019-08-04 15:42:39 +02:00
|
|
|
$cache = $this->cacheFactory->create($cache_type);
|
|
|
|
if ($cache instanceof IMemoryCache) {
|
|
|
|
return new Lock\CacheLock($cache);
|
2019-08-03 20:48:56 +02:00
|
|
|
}
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
$this->logger->debug('Using Cache driver for locking failed.', ['exception' => $exception]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. Use Database Locking as a Fallback
|
2019-08-04 10:26:53 +02:00
|
|
|
return new Lock\DatabaseLock($this->dba);
|
2019-08-03 20:48:56 +02:00
|
|
|
}
|
|
|
|
}
|