1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-16 10:04:21 +02:00

Merge pull request #341 from p810/cache-adapters

Flextype core: update Cache to use adapter to retrieve driver object
This commit is contained in:
Sergey Romanenko
2019-12-25 00:32:40 +03:00
committed by GitHub
11 changed files with 250 additions and 194 deletions

19
flextype/cache/AcpuAdapter.php vendored Normal file
View File

@@ -0,0 +1,19 @@
<?php
namespace Flextype\Cache;
use Psr\Container\ContainerInterface;
use Doctrine\Cache\Common\AcpuCache;
class AcpuAdapter implements CacheAdapterInterface
{
function __construct(ContainerInterface $flextype)
{
$this->flextype = $flextype;
}
public function getDriver() : object
{
return new AcpuCache();
}
}

19
flextype/cache/ArrayAdapter.php vendored Normal file
View File

@@ -0,0 +1,19 @@
<?php
namespace Flextype\Cache;
use Doctrine\Common\Cache\ArrayCache;
use Psr\Container\ContainerInterface;
class ArrayAdapter implements CacheAdapterInterface
{
function __construct(ContainerInterface $flextype)
{
$this->flextype = $flextype;
}
public function getDriver() : object
{
return new ArrayCache();
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Flextype\Cache;
use Psr\Container\ContainerInterface;
interface CacheAdapterInterface
{
/**
* Injects the dependency container
*
* @param \Psr\Container\ContainerInterface $container
* @return void
*/
function __construct(ContainerInterface $container);
/**
* Returns the cache driver object
*
* @return object
*/
public function getDriver() : object;
}

26
flextype/cache/FilesystemAdapter.php vendored Normal file
View File

@@ -0,0 +1,26 @@
<?php
namespace Flextype\Cache;
use Doctrine\Common\Cache\FilesystemCache;
use Flextype\Component\Filesystem\Filesystem;
use Psr\Container\ContainerInterface;
class FilesystemAdapter implements CacheAdapterInterface
{
function __construct(ContainerInterface $flextype)
{
$this->flextype = $flextype;
}
public function getDriver() : object
{
$cache_directory = PATH['cache'] . '/doctrine/';
if (! Filesystem::has($cache_directory)) {
Filesystem::createDir($cache_directory);
}
return new FilesystemCache($cache_directory);
}
}

29
flextype/cache/MemcachedAdapter.php vendored Normal file
View File

@@ -0,0 +1,29 @@
<?php
namespace Flextype\Cache;
use Doctrine\Common\Cache\MemcachedCache;
use Memecached;
use Psr\Container\ContainerInterface;
class MemcachedAdapter implements CacheAdapterInterface
{
function __construct(ContainerInterface $flextype)
{
$this->flextype = $flextype;
}
public function getDriver() : object
{
$memcached = new Memecached();
$memcached->addServer(
$this->flextype['registry']->get('settings.cache.memcached.server', 'localhost'),
$this->flextype['registry']->get('settings.cache.memcache.port', 11211)
);
$driver = new MemcachedCache();
$driver->setMemcached($memcached);
return $driver;
}
}

42
flextype/cache/RedisAdapter.php vendored Normal file
View File

@@ -0,0 +1,42 @@
<?php
namespace Flextype\Cache;
use Psr\Container\ContainerInterface;
use Redis;
use RedisException;
use Doctrine\Common\Cache\RedisCache;
class RedisAdapter implements CacheAdapterInterface
{
function __construct(ContainerInterface $flextype)
{
$this->flextype = $flextype;
}
public function getDriver() : object
{
$redis = new Redis();
$socket = $this->flextype['registry']->get('settings.cache.redis.socket', false);
$password = $this->flextype['registry']->get('settings.cache.redis.password', false);
if ($socket) {
$redis->connect($socket);
} else {
$redis->connect(
$this->flextype['registry']->get('settings.cache.redis.server', 'localhost'),
$this->flextype['registry']->get('settings.cache.redis.port', 6379)
);
}
// Authenticate with password if set
if ($password && ! $redis->auth($password)) {
throw new RedisException('Redis authentication failed');
}
$driver = new RedisCache();
$driver->setRedis($redis);
return $driver;
}
}

29
flextype/cache/SQLite3Adapter.php vendored Normal file
View File

@@ -0,0 +1,29 @@
<?php
namespace Flextype\Cache;
use Flextype\Component\Filesystem\Filesystem;
use SQLite3;
use Doctrine\Common\Cache\SQLite3Cache;
use Psr\Container\ContainerInterface;
class SQLite3Adapter implements CacheAdapterInterface
{
function __construct(ContainerInterface $flextype)
{
$this->flextype = $flextype;
}
public function getDriver() : object
{
$cache_directory = PATH['cache'] . '/doctrine/';
if (! Filesystem::has($cache_directory)) {
Filesystem::createDir($cache_directory);
}
$db = new SQLite3($cache_directory . $this->flextype['registry']->get('settings.cache.sqlite3.database', 'flextype') . '.db');
return new SQLite3Cache($db, $this->flextype['registry']->get('settings.cache.sqlite3.table', 'flextype'));
}
}

19
flextype/cache/WinCacheAdapter.php vendored Normal file
View File

@@ -0,0 +1,19 @@
<?php
namespace Flextype\Cache;
use Doctrine\Common\Cache\WinCacheCache;
use Psr\Container\ContainerInterface;
class WinCacheAdapter implements CacheAdapterInterface
{
function __construct(ContainerInterface $flextype)
{
$this->flextype = $flextype;
}
public function getDriver() : object
{
return new WinCacheCache();
}
}

19
flextype/cache/ZendDataCacheAdapter.php vendored Normal file
View File

@@ -0,0 +1,19 @@
<?php
namespace Flextype\Cache;
use Doctrine\Common\Cache\ZendDataCache;
use Psr\Container\ContainerInterface;
class ZendDataCacheAdapter implements CacheAdapterInterface
{
function __construct(ContainerInterface $flextype)
{
$this->flextype = $flextype;
}
public function getDriver() : object
{
return new ZendDataCache();
}
}

View File

@@ -9,14 +9,8 @@ declare(strict_types=1);
namespace Flextype;
use Doctrine\Common\Cache as DoctrineCache;
use Flextype\Component\Filesystem\Filesystem;
use Memcached;
use Redis;
use RedisException;
use SQLite3;
use function clearstatcache;
use function extension_loaded;
use function function_exists;
use function md5;
use function opcache_reset;
@@ -89,194 +83,7 @@ class Cache
*/
public function getCacheDriver() : object
{
// Try to set default cache driver name
$driver_name = $this->setDefaultCacheDriverName($this->flextype['registry']->get('settings.cache.driver'));
// Set cache driver
return $this->setCacheDriver($driver_name);
}
protected function setCacheDriver(string $driver_name)
{
switch ($driver_name) {
case 'apcu':
$driver = $this->setApcuCacheDriver();
break;
case 'array':
$driver = $this->setArrayCacheDriver();
break;
case 'wincache':
$driver = $this->setWinCacheDriver();
break;
case 'memcached':
$driver = $this->setMemcachedCacheDriver();
break;
case 'sqlite3':
$driver = $this->setSQLite3CacheDriver();
break;
case 'zend':
$driver = $this->setZendDataCacheDriver();
break;
case 'redis':
$driver = $this->setRedisCacheDriver();
break;
default:
$driver = $this->setFilesystemCacheDriver();
break;
}
return $driver;
}
/**
* The ZendDataCache driver uses the Zend Data Cache API available in the Zend Platform.
*
* @access protected
*/
protected function setZendDataCacheDriver()
{
return new DoctrineCache\ZendDataCache();
}
/**
* The SQLite3Cache driver stores the cache data in a SQLite database and depends on the sqlite3 extension
* http://php.net/manual/en/book.sqlite3.php
*
* @access protected
*/
protected function setSQLite3CacheDriver()
{
// Cache directory
$cache_directory = PATH['cache'] . '/doctrine/';
// Create doctrine cache directory if its not exists
! Filesystem::has($cache_directory) and Filesystem::createDir($cache_directory);
$db = new SQLite3($cache_directory . $this->flextype['registry']->get('settings.cache.sqlite3.database', 'flextype') . '.db');
return new DoctrineCache\SQLite3Cache($db, $this->flextype['registry']->get('settings.cache.sqlite3.table', 'flextype'));
}
/**
* The MemcachedCache drivers stores the cache data in Memcached.
*
* @access protected
*/
protected function setMemcachedCacheDriver()
{
$memcached = new Memcached();
$memcached->addServer(
$this->flextype['registry']->get('settings.cache.memcached.server', 'localhost'),
$this->flextype['registry']->get('settings.cache.memcache.port', 11211)
);
$driver = new DoctrineCache\MemcachedCache();
$driver->setMemcached($memcached);
return $driver;
}
/**
* The WinCacheCache driver uses the wincache_ucache_get, wincache_ucache_exists, etc. functions
* that come with the wincache extension
* http://php.net/manual/en/book.wincache.php
*
* @access protected
*/
protected function setWinCacheDriver()
{
return new DoctrineCache\WinCacheCache();
}
/**
* The ArrayCache driver stores the cache data in PHPs memory and is not persisted anywhere.
* This can be useful for caching things in memory for a single process when you don't need the cache to be persistent across processes.
*
* @access protected
*/
protected function setArrayCacheDriver()
{
return new DoctrineCache\ArrayCache();
}
/**
* The ApcuCache driver uses the apcu_fetch, apcu_exists, etc. functions
* that come with PHP so no additional setup is required in order to use it.
*
* @access protected
*/
protected function setApcuCacheDriver()
{
return new DoctrineCache\ApcuCache();
}
/**
* The RedisCache driver stores the cache data in Redis and depends on the phpredis extension
* https://github.com/phpredis/phpredis
*
* @access protected
*/
protected function setRedisCacheDriver()
{
$redis = new Redis();
$socket = $this->flextype['registry']->get('settings.cache.redis.socket', false);
$password = $this->flextype['registry']->get('settings.cache.redis.password', false);
if ($socket) {
$redis->connect($socket);
} else {
$redis->connect(
$this->flextype['registry']->get('settings.cache.redis.server', 'localhost'),
$this->flextype['registry']->get('settings.cache.redis.port', 6379)
);
}
// Authenticate with password if set
if ($password && ! $redis->auth($password)) {
throw new RedisException('Redis authentication failed');
}
$driver = new DoctrineCache\RedisCache();
$driver->setRedis($redis);
return $driver;
}
/**
* Filesystem cache Driver
*
* @access protected
*/
protected function setFilesystemCacheDriver()
{
// Cache directory
$cache_directory = PATH['cache'] . '/doctrine/';
// Create doctrine cache directory if its not exists
! Filesystem::has($cache_directory) and Filesystem::createDir($cache_directory);
return new DoctrineCache\FilesystemCache($cache_directory);
}
/**
* Set Default Cache Driver Name
*
* @param string $driver_name Driver name.
*
* @access protected
*/
protected function setDefaultCacheDriverName(string $driver_name) : string
{
if (! $driver_name || $driver_name === 'auto') {
if (extension_loaded('apcu')) {
$driver_name = 'apcu';
} elseif (extension_loaded('wincache')) {
$driver_name = 'wincache';
} else {
$driver_name = 'file';
}
}
return $driver_name;
return $this->flextype['cache_adapter']->getDriver();
}
/**

View File

@@ -43,6 +43,8 @@ use Slim\Views\TwigExtension;
use Thunder\Shortcode\ShortcodeFacade;
use Twig\Extension\DebugExtension;
use function date;
use function ucfirst;
use function extension_loaded;
/**
* Supply a custom callable resolver, which resolves PSR-15 middlewares.
@@ -104,6 +106,28 @@ $flextype['flash'] = static function ($container) {
return new Messages();
};
/**
* Adds the cache adapter to the Flextype container
*/
$flextype['cache_adapter'] = static function ($container) use ($flextype) {
$driver_name = $container['registry']->get('settings.cache.driver');
if (! $driver_name || $driver_name === 'auto') {
if (extension_loaded('apcu')) {
$driver_name = 'apcu';
} elseif (extension_loaded('wincache')) {
$driver_name = 'wincache';
} else {
$driver_name = 'filesystem';
}
}
$class = ucfirst($driver_name);
$adapter = "Flextype\\Cache\\{$class}Adapter";
return new $adapter($flextype);
};
/**
* Add cache service to Flextype container
*/