friendica/src/Core/Cache/Factory/Cache.php

119 lines
4 KiB
PHP
Raw Normal View History

<?php
2020-02-09 15:45:36 +01:00
/**
2023-01-01 15:36:24 +01:00
* @copyright Copyright (C) 2010-2023, the Friendica project
2020-02-09 15:45:36 +01:00
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Core\Cache\Factory;
2021-10-26 21:44:29 +02:00
use Friendica\Core\Cache\Enum;
use Friendica\Core\Cache\Capability\ICanCache;
use Friendica\Core\Cache\Exception\CachePersistenceException;
use Friendica\Core\Cache\Exception\InvalidCacheDriverException;
use Friendica\Core\Cache\Type;
use Friendica\Core\Config\Capability\IManageConfigValues;
2023-07-22 23:57:38 +02:00
use Friendica\Core\Hooks\Capabilities\ICanCreateInstances;
use Friendica\Util\Profiler;
/**
* Class CacheFactory
*
* @package Friendica\Core\Cache
*
2018-07-05 07:59:56 +02:00
* A basic class to generate a CacheDriver
*/
2021-10-26 21:44:29 +02:00
class Cache
{
/**
* @var string The default cache if nothing set
*/
2021-10-26 21:44:29 +02:00
const DEFAULT_TYPE = Enum\Type::DATABASE;
2023-07-22 23:57:38 +02:00
/** @var ICanCreateInstances */
protected $instanceCreator;
/** @var IManageConfigValues */
protected $config;
/** @var Profiler */
protected $profiler;
2023-07-22 23:57:38 +02:00
public function __construct(ICanCreateInstances $instanceCreator, IManageConfigValues $config, Profiler $profiler)
{
2023-07-22 23:57:38 +02:00
$this->config = $config;
$this->instanceCreator = $instanceCreator;
$this->profiler = $profiler;
}
/**
2021-12-13 19:40:38 +01:00
* This method creates a CacheDriver for distributed caching with the given cache driver name
*
2021-10-26 21:44:29 +02:00
* @param string|null $type The cache type to create (default is per config)
*
2021-10-26 21:44:29 +02:00
* @return ICanCache The instance of the CacheDriver
*
* @throws InvalidCacheDriverException In case the underlying cache driver isn't valid or not configured properly
* @throws CachePersistenceException In case the underlying cache has errors during persistence
*/
2021-12-13 19:40:38 +01:00
public function createDistributed(string $type = null): ICanCache
{
2023-07-22 23:57:38 +02:00
if ($type === Type\APCuCache::$NAME) {
2021-12-13 19:40:38 +01:00
throw new InvalidCacheDriverException('apcu doesn\'t support distributed caching.');
}
2021-12-13 19:40:38 +01:00
return $this->create($type ?? $this->config->get('system', 'distributed_cache_driver', self::DEFAULT_TYPE));
}
/**
* This method creates a CacheDriver for local caching with the given cache driver name
*
* @param string|null $type The cache type to create (default is per config)
*
* @return ICanCache The instance of the CacheDriver
*
* @throws InvalidCacheDriverException In case the underlying cache driver isn't valid or not configured properly
* @throws CachePersistenceException In case the underlying cache has errors during persistence
*/
public function createLocal(string $type = null): ICanCache
{
return $this->create($type ?? $this->config->get('system', 'cache_driver', self::DEFAULT_TYPE));
}
/**
* Creates a new Cache instance
*
2023-07-22 23:57:38 +02:00
* @param string $strategy The strategy, which cache instance should be used
2021-12-13 19:40:38 +01:00
*
* @return ICanCache
*
* @throws InvalidCacheDriverException In case the underlying cache driver isn't valid or not configured properly
* @throws CachePersistenceException In case the underlying cache has errors during persistence
*/
2023-07-22 23:57:38 +02:00
protected function create(string $strategy): ICanCache
2021-12-13 19:40:38 +01:00
{
2023-07-22 23:57:38 +02:00
/** @var ICanCache $cache */
$cache = $this->instanceCreator->create(ICanCache::class, $strategy);
$profiling = $this->config->get('system', 'profiling', false);
// In case profiling is enabled, wrap the ProfilerCache around the current cache
if (isset($profiling) && $profiling !== false) {
2021-10-26 21:44:29 +02:00
return new Type\ProfilerCacheDecorator($cache, $this->profiler);
} else {
return $cache;
}
}
}