3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Core\Lock\Factory;
24 use Friendica\Core\Cache\Factory\Cache;
25 use Friendica\Core\Cache\Capability\ICanCacheInMemory;
26 use Friendica\Core\Cache\Enum;
27 use Friendica\Core\Config\Capability\IManageConfigValues;
28 use Friendica\Core\Lock\Capability\ICanLock;
29 use Friendica\Core\Lock\Type;
30 use Friendica\Database\Database;
31 use Psr\Log\LoggerInterface;
36 * @package Friendica\Core\Cache
38 * A basic class to generate a LockDriver
43 * @var string The default driver for caching
45 const DEFAULT_DRIVER = 'default';
48 * @var IManageConfigValues The configuration to read parameters out of the config
53 * @var Database The database connection in case that the cache is used the dba connection
58 * @var Cache The memory cache driver in case we use it
60 private $cacheFactory;
63 * @var LoggerInterface The Friendica Logger
67 public function __construct(Cache $cacheFactory, IManageConfigValues $config, Database $dba, LoggerInterface $logger)
69 $this->cacheFactory = $cacheFactory;
70 $this->config = $config;
72 $this->logger = $logger;
75 public function create()
77 $lock_type = $this->config->get('system', 'lock_driver', self::DEFAULT_DRIVER);
81 case Enum\Type::MEMCACHE:
82 case Enum\Type::MEMCACHED:
83 case Enum\Type::REDIS:
85 $cache = $this->cacheFactory->createLocal($lock_type);
86 if ($cache instanceof ICanCacheInMemory) {
87 return new Type\CacheLock($cache);
89 throw new \Exception(sprintf('Incompatible cache driver \'%s\' for lock used', $lock_type));
92 return new Type\DatabaseLock($this->dba);
94 return new Type\SemaphoreLock();
96 return self::useAutoDriver();
98 } catch (\Exception $exception) {
99 $this->logger->alert('Driver \'' . $lock_type . '\' failed - Fallback to \'useAutoDriver()\'', ['exception' => $exception]);
100 return self::useAutoDriver();
105 * This method tries to find the best - local - locking method for Friendica
107 * The following sequence will be tried:
108 * 1. Semaphore Locking
110 * 3. Database Locking
114 private function useAutoDriver()
116 // 1. Try to use Semaphores for - local - locking
117 if (function_exists('sem_get')) {
119 return new Type\SemaphoreLock();
120 } catch (\Exception $exception) {
121 $this->logger->warning('Using Semaphore driver for locking failed.', ['exception' => $exception]);
125 // 2. Try to use Cache Locking (don't use the DB-Cache Locking because it works different!)
126 $cache_type = $this->config->get('system', 'cache_driver', 'database');
127 if ($cache_type != Enum\Type::DATABASE) {
129 $cache = $this->cacheFactory->createLocal($cache_type);
130 if ($cache instanceof ICanCacheInMemory) {
131 return new Type\CacheLock($cache);
133 } catch (\Exception $exception) {
134 $this->logger->warning('Using Cache driver for locking failed.', ['exception' => $exception]);
138 // 3. Use Database Locking as a Fallback
139 return new Type\DatabaseLock($this->dba);