Merge pull request #11099 from nupplaphil/feat/session_cache

Add own distribution cache for Sessions
This commit is contained in:
Hypolite Petovan 2021-12-13 14:49:51 -05:00 committed by GitHub
commit b4c1fb0d9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 67 additions and 36 deletions

View file

@ -81,7 +81,7 @@ class Cache
} }
/** /**
* This method creates a CacheDriver for the given cache driver name * This method creates a CacheDriver for distributed caching with the given cache driver name
* *
* @param string|null $type The cache type to create (default is per config) * @param string|null $type The cache type to create (default is per config)
* *
@ -90,12 +90,42 @@ class Cache
* @throws InvalidCacheDriverException In case the underlying cache driver isn't valid or not configured properly * @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 * @throws CachePersistenceException In case the underlying cache has errors during persistence
*/ */
public function create(string $type = null): ICanCache public function createDistributed(string $type = null): ICanCache
{ {
if (empty($type)) { if ($type === Enum\Type::APCU) {
$type = $this->config->get('system', 'cache_driver', self::DEFAULT_TYPE); throw new InvalidCacheDriverException('apcu doesn\'t support distributed caching.');
} }
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
*
* @param string $type The type of cache
*
* @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
*/
protected function create(string $type): ICanCache
{
switch ($type) { switch ($type) {
case Enum\Type::MEMCACHE: case Enum\Type::MEMCACHE:
$cache = new Type\MemcacheCache($this->hostname, $this->config); $cache = new Type\MemcacheCache($this->hostname, $this->config);

View file

@ -82,22 +82,16 @@ class Lock
case Enum\Type::MEMCACHED: case Enum\Type::MEMCACHED:
case Enum\Type::REDIS: case Enum\Type::REDIS:
case Enum\Type::APCU: case Enum\Type::APCU:
$cache = $this->cacheFactory->create($lock_type); $cache = $this->cacheFactory->createLocal($lock_type);
if ($cache instanceof ICanCacheInMemory) { if ($cache instanceof ICanCacheInMemory) {
return new Type\CacheLock($cache); return new Type\CacheLock($cache);
} else { } else {
throw new \Exception(sprintf('Incompatible cache driver \'%s\' for lock used', $lock_type)); throw new \Exception(sprintf('Incompatible cache driver \'%s\' for lock used', $lock_type));
} }
break;
case 'database': case 'database':
return new Type\DatabaseLock($this->dba); return new Type\DatabaseLock($this->dba);
break;
case 'semaphore': case 'semaphore':
return new Type\SemaphoreLock(); return new Type\SemaphoreLock();
break;
default: default:
return self::useAutoDriver(); return self::useAutoDriver();
} }
@ -132,7 +126,7 @@ class Lock
$cache_type = $this->config->get('system', 'cache_driver', 'database'); $cache_type = $this->config->get('system', 'cache_driver', 'database');
if ($cache_type != Enum\Type::DATABASE) { if ($cache_type != Enum\Type::DATABASE) {
try { try {
$cache = $this->cacheFactory->create($cache_type); $cache = $this->cacheFactory->createLocal($cache_type);
if ($cache instanceof ICanCacheInMemory) { if ($cache instanceof ICanCacheInMemory) {
return new Type\CacheLock($cache); return new Type\CacheLock($cache);
} }

View file

@ -22,8 +22,8 @@
namespace Friendica\Core\Session\Factory; namespace Friendica\Core\Session\Factory;
use Friendica\App; use Friendica\App;
use Friendica\Core\Cache\Capability\ICanCache;
use Friendica\Core\Cache\Enum; use Friendica\Core\Cache\Enum;
use Friendica\Core\Cache\Factory\Cache;
use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Session\Capability\IHandleSessions; use Friendica\Core\Session\Capability\IHandleSessions;
use Friendica\Core\Session\Type; use Friendica\Core\Session\Type;
@ -51,14 +51,14 @@ class Session
* @param App\BaseURL $baseURL * @param App\BaseURL $baseURL
* @param IManageConfigValues $config * @param IManageConfigValues $config
* @param Database $dba * @param Database $dba
* @param ICanCache $cache * @param Cache $cacheFactory
* @param LoggerInterface $logger * @param LoggerInterface $logger
* @param Profiler $profiler * @param Profiler $profiler
* @param array $server * @param array $server
* *
* @return IHandleSessions * @return IHandleSessions
*/ */
public function createSession(App\Mode $mode, App\BaseURL $baseURL, IManageConfigValues $config, Database $dba, ICanCache $cache, LoggerInterface $logger, Profiler $profiler, array $server = []) public function createSession(App\Mode $mode, App\BaseURL $baseURL, IManageConfigValues $config, Database $dba, Cache $cacheFactory, LoggerInterface $logger, Profiler $profiler, array $server = [])
{ {
$profiler->startRecording('session'); $profiler->startRecording('session');
$session = null; $session = null;
@ -75,6 +75,8 @@ class Session
$handler = new Handler\Database($dba, $logger, $server); $handler = new Handler\Database($dba, $logger, $server);
break; break;
case self::HANDLER_CACHE: case self::HANDLER_CACHE:
$cache = $cacheFactory->createDistributed();
// In case we're using the db as cache driver, use the native db session, not the cache // In case we're using the db as cache driver, use the native db session, not the cache
if ($config->get('system', 'cache_driver') === Enum\Type::DATABASE) { if ($config->get('system', 'cache_driver') === Enum\Type::DATABASE) {
$handler = new Handler\Database($dba, $logger, $server); $handler = new Handler\Database($dba, $logger, $server);

View file

@ -141,6 +141,10 @@ return [
// Whether to use Memcache, Memcached, Redis or APCu to store temporary cache. // Whether to use Memcache, Memcached, Redis or APCu to store temporary cache.
'cache_driver' => 'database', 'cache_driver' => 'database',
// distributed_cache_driver (database|memcache|memcached|redis)
// Whether to use database, Memcache, Memcached or Redis as a distributed cache.
'distributed_cache_driver' => 'database',
// config_adapter (jit|preload) // config_adapter (jit|preload)
// Allow to switch the configuration adapter to improve performances at the cost of memory consumption. // Allow to switch the configuration adapter to improve performances at the cost of memory consumption.
'config_adapter' => 'jit', 'config_adapter' => 'jit',

View file

@ -160,13 +160,13 @@ return [
Cache\Capability\ICanCache::class => [ Cache\Capability\ICanCache::class => [
'instanceOf' => Cache\Factory\Cache::class, 'instanceOf' => Cache\Factory\Cache::class,
'call' => [ 'call' => [
['create', [], Dice::CHAIN_CALL], ['createLocal', [], Dice::CHAIN_CALL],
], ],
], ],
Cache\Capability\ICanCacheInMemory::class => [ Cache\Capability\ICanCacheInMemory::class => [
'instanceOf' => Cache\Factory\Cache::class, 'instanceOf' => Cache\Factory\Cache::class,
'call' => [ 'call' => [
['create', [], Dice::CHAIN_CALL], ['createLocal', [], Dice::CHAIN_CALL],
], ],
], ],
Lock\Capability\ICanLock::class => [ Lock\Capability\ICanLock::class => [

View file

@ -52,6 +52,7 @@ return [
// Caching // Caching
'FRIENDICA_CACHE_DRIVER' => ['system', 'cache_driver'], 'FRIENDICA_CACHE_DRIVER' => ['system', 'cache_driver'],
'FRIENDICA_SESSION_HANDLER' => ['system', 'session_handler'], 'FRIENDICA_SESSION_HANDLER' => ['system', 'session_handler'],
'FRIENDICA_DISTRIBUTED_CACHE_DRIVER' => ['system', 'distributed_cache_driver'],
'FRIENDICA_LOCK_DRIVER' => ['system', 'lock_driver'], 'FRIENDICA_LOCK_DRIVER' => ['system', 'lock_driver'],
// Redis Config // Redis Config