3 * @copyright Copyright (C) 2010-2021, 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\Factory;
24 use Friendica\Core\Cache\IMemoryCache;
25 use Friendica\Core\Cache\Type;
26 use Friendica\Core\Config\IConfig;
27 use Friendica\Core\Lock;
28 use Friendica\Database\Database;
29 use Psr\Log\LoggerInterface;
34 * @package Friendica\Core\Cache
36 * A basic class to generate a LockDriver
41 * @var string The default driver for caching
43 const DEFAULT_DRIVER = 'default';
46 * @var IConfig The configuration to read parameters out of the config
51 * @var Database The database connection in case that the cache is used the dba connection
56 * @var CacheFactory The memory cache driver in case we use it
58 private $cacheFactory;
61 * @var LoggerInterface The Friendica Logger
65 public function __construct(CacheFactory $cacheFactory, IConfig $config, Database $dba, LoggerInterface $logger)
67 $this->cacheFactory = $cacheFactory;
68 $this->config = $config;
70 $this->logger = $logger;
73 public function create()
75 $lock_type = $this->config->get('system', 'lock_driver', self::DEFAULT_DRIVER);
83 $cache = $this->cacheFactory->create($lock_type);
84 if ($cache instanceof IMemoryCache) {
85 return new Lock\CacheLock($cache);
87 throw new \Exception(sprintf('Incompatible cache driver \'%s\' for lock used', $lock_type));
92 return new Lock\DatabaseLock($this->dba);
96 return new Lock\SemaphoreLock();
100 return self::useAutoDriver();
102 } catch (\Exception $exception) {
103 $this->logger->alert('Driver \'' . $lock_type . '\' failed - Fallback to \'useAutoDriver()\'', ['exception' => $exception]);
104 return self::useAutoDriver();
109 * This method tries to find the best - local - locking method for Friendica
111 * The following sequence will be tried:
112 * 1. Semaphore Locking
114 * 3. Database Locking
118 private function useAutoDriver()
120 // 1. Try to use Semaphores for - local - locking
121 if (function_exists('sem_get')) {
123 return new Lock\SemaphoreLock();
124 } catch (\Exception $exception) {
125 $this->logger->warning('Using Semaphore driver for locking failed.', ['exception' => $exception]);
129 // 2. Try to use Cache Locking (don't use the DB-Cache Locking because it works different!)
130 $cache_type = $this->config->get('system', 'cache_driver', 'database');
131 if ($cache_type != Type::DATABASE) {
133 $cache = $this->cacheFactory->create($cache_type);
134 if ($cache instanceof IMemoryCache) {
135 return new Lock\CacheLock($cache);
137 } catch (\Exception $exception) {
138 $this->logger->warning('Using Cache driver for locking failed.', ['exception' => $exception]);
142 // 3. Use Database Locking as a Fallback
143 return new Lock\DatabaseLock($this->dba);