mirror of
https://github.com/flextype/flextype.git
synced 2025-08-10 15:14:20 +02:00
Merge branch 'Move_to_PhpFastCache_#457' into dev
This commit is contained in:
@@ -42,6 +42,8 @@
|
||||
"league/event": "~2.2.0",
|
||||
"league/glide": "~1.6.0",
|
||||
|
||||
"phpfastcache/phpfastcache": "~8.0.1",
|
||||
|
||||
"respect/validation": "~2.0.16",
|
||||
"monolog/monolog": "~2.1.1",
|
||||
"cocur/slugify": "~4.0.0",
|
||||
|
@@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flextype\App\Foundation\Cache;
|
||||
|
||||
use Doctrine\Common\Cache\AcpuCache;
|
||||
|
||||
class AcpuCacheAdapter implements CacheAdapterInterface
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function getDriver() : object
|
||||
{
|
||||
return new AcpuCache();
|
||||
}
|
||||
}
|
@@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flextype\App\Foundation\Cache;
|
||||
|
||||
use Doctrine\Common\Cache\ArrayCache;
|
||||
|
||||
class ArrayCacheAdapter implements CacheAdapterInterface
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function getDriver() : object
|
||||
{
|
||||
return new ArrayCache();
|
||||
}
|
||||
}
|
@@ -1,372 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype (https://flextype.org)
|
||||
* Founded by Sergey Romanenko and maintained by Flextype Community.
|
||||
*/
|
||||
|
||||
namespace Flextype\App\Foundation\Cache;
|
||||
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
use function clearstatcache;
|
||||
use function error_reporting;
|
||||
use function function_exists;
|
||||
use function md5;
|
||||
use function opcache_reset;
|
||||
use function time;
|
||||
|
||||
class Cache
|
||||
{
|
||||
/**
|
||||
* Unique cache key
|
||||
*
|
||||
* @var string Cache key.
|
||||
*/
|
||||
private $key;
|
||||
|
||||
/**
|
||||
* Lifetime
|
||||
*
|
||||
* @var int Lifetime.
|
||||
*/
|
||||
private $lifetime;
|
||||
|
||||
/**
|
||||
* Current time
|
||||
*
|
||||
* @var int Current time.
|
||||
*/
|
||||
private $now;
|
||||
|
||||
/**
|
||||
* Cache Driver
|
||||
*
|
||||
* @var object
|
||||
*/
|
||||
private $driver;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
|
||||
// Create Cache Directory
|
||||
! Filesystem::has(PATH['cache']) and Filesystem::createDir(PATH['cache']);
|
||||
|
||||
// Set current time
|
||||
$this->now = time();
|
||||
|
||||
// Create cache key to allow invalidate all cache on configuration changes.
|
||||
$cache_prefix = (flextype('registry')->get('flextype.settings.cache.prefix') ?? 'flextype');
|
||||
$cache_unique_string = md5(PATH['project'] . flextype('registry')->get('flextype.manifest.version'));
|
||||
$this->key = $cache_prefix . $cache_unique_string;
|
||||
|
||||
// Get Cache Driver
|
||||
$this->driver = $this->getCacheDriver();
|
||||
|
||||
// Set the cache namespace to our unique key
|
||||
$this->driver->setNamespace($this->key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Cache Driver
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function getCacheDriver() : object
|
||||
{
|
||||
return flextype('cache_adapter')->getDriver();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns driver variable
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function driver() : object
|
||||
{
|
||||
return $this->driver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cache key.
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function getKey() : string
|
||||
{
|
||||
return $this->key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches an entry from the cache.
|
||||
*
|
||||
* @param string $id The id of the cache entry to fetch.
|
||||
*
|
||||
* @return mixed|false The cached data or FALSE, if no cache entry exists for the given id.
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function fetch(string $id)
|
||||
{
|
||||
if (flextype('registry')->get('flextype.settings.cache.enabled')) {
|
||||
return $this->driver->fetch($id);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches multiplay items from the cache.
|
||||
*
|
||||
* @param array $keys Array of keys to retrieve from cache
|
||||
*
|
||||
* @return array Array of values retrieved for the given keys.
|
||||
*/
|
||||
public function fetchMultiple(array $keys) : array
|
||||
{
|
||||
if (flextype('registry')->get('flextype.settings.cache.enabled')) {
|
||||
return $this->driver->fetchMultiple($keys);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a boolean state of whether or not the item exists in the cache based on id key
|
||||
*
|
||||
* @param string $id the id of the cached data entry
|
||||
*
|
||||
* @return bool true if the cached items exists
|
||||
*/
|
||||
public function contains(string $id) : bool
|
||||
{
|
||||
if (flextype('registry')->get('flextype.settings.cache.enabled')) {
|
||||
return $this->driver->contains($id);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts data into the cache.
|
||||
*
|
||||
* @param string $id The cache id.
|
||||
* @param mixed $data The cache entry/data.
|
||||
* @param int $lifetime The lifetime in number of seconds for this cache entry.
|
||||
* If zero (the default), the entry never expires (although it may be deleted from the cache
|
||||
* to make place for other items).
|
||||
*
|
||||
* @return bool TRUE if the operation was successful, FALSE if it wasn't.
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function save(string $id, $data, ?int $lifetime = null) : bool
|
||||
{
|
||||
if (! flextype('registry')->get('flextype.settings.cache.enabled')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($lifetime === null) {
|
||||
$lifetime = $this->getLifetime();
|
||||
}
|
||||
|
||||
return $this->driver->save($id, $data, $lifetime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts multiple data into the cache.
|
||||
*
|
||||
* @param array $keysAndValues Array of keys and values to save in cache
|
||||
* @param int $lifetime The lifetime. If != 0, sets a specific lifetime for these
|
||||
* cache items (0 => infinite lifeTime).
|
||||
*
|
||||
* @return bool TRUE if the operation was successful, FALSE if it wasn't.
|
||||
*/
|
||||
public function saveMultiple(array $keysAndValues, int $lifetime = 0) : bool
|
||||
{
|
||||
if (! flextype('registry')->get('flextype.settings.cache.enabled')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->saveMultiple($keysAndValues, $lifetime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete item from the cache
|
||||
*
|
||||
* @param string $id The cache id.
|
||||
*
|
||||
* @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise.
|
||||
*/
|
||||
public function delete(string $id) : bool
|
||||
{
|
||||
if (! flextype('registry')->get('flextype.settings.cache.enabled')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->driver->delete($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete multiple item from the cache.
|
||||
*
|
||||
* @param array $keys Array of keys to delete from cache
|
||||
*
|
||||
* @return bool TRUE if the operation was successful, FALSE if it wasn't
|
||||
*/
|
||||
public function deleteMultiple(array $keys) : bool
|
||||
{
|
||||
if (! flextype('registry')->get('flextype.settings.cache.enabled')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->driver->deleteMultiple($keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves cached information from the data store.
|
||||
*
|
||||
* @return array|null An associative array with server's statistics if available, NULL otherwise.
|
||||
*/
|
||||
public function getStats() : ?array
|
||||
{
|
||||
if (! flextype('registry')->get('flextype.settings.cache.enabled')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->driver->getStats();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes all cache
|
||||
*/
|
||||
public function deleteAll() : bool
|
||||
{
|
||||
if (! flextype('registry')->get('flextype.settings.cache.enabled')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->driver->deleteAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Flushes all cache items.
|
||||
*
|
||||
* @return bool TRUE if the cache items were successfully flushed, FALSE otherwise.
|
||||
*/
|
||||
public function flushAll() : bool
|
||||
{
|
||||
if (! flextype('registry')->get('flextype.settings.cache.enabled')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->driver->flushAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the cache lifetime.
|
||||
*
|
||||
* @param int $future timestamp
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function setLifetime(int $future) : void
|
||||
{
|
||||
if (! $future) {
|
||||
return;
|
||||
}
|
||||
|
||||
$interval = $future-$this->now;
|
||||
|
||||
if ($interval <= 0 || $interval >= $this->getLifetime()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->lifetime = $interval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the cache lifetime (in seconds)
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function getLifetime()
|
||||
{
|
||||
if ($this->lifetime === null) {
|
||||
$this->lifetime = flextype('registry')->get('flextype.settings.cache.lifetime') ?: 604800;
|
||||
}
|
||||
|
||||
return $this->lifetime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge specific cache directory
|
||||
*
|
||||
* @param $directory Directory to purge
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function purge(string $directory) : void
|
||||
{
|
||||
// Run event: onCacheBeforePurge
|
||||
flextype('emitter')->emit('onCacheBeforePurge');
|
||||
|
||||
// Remove specific cache directory
|
||||
Filesystem::deleteDir(PATH['cache'] . '/' . $directory);
|
||||
|
||||
// Save and Mute error_reporting
|
||||
$errorReporting = error_reporting();
|
||||
error_reporting(0);
|
||||
|
||||
// Clear stat cache
|
||||
clearstatcache();
|
||||
|
||||
// Clear opcache
|
||||
function_exists('opcache_reset') and opcache_reset();
|
||||
|
||||
// Restore error_reporting
|
||||
error_reporting($errorReporting);
|
||||
|
||||
// Run event: onCacheAfterPurge
|
||||
flextype('emitter')->emit('onCacheAfterPurge');
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge ALL Cache directories
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function purgeAll() : void
|
||||
{
|
||||
// Run event: onCacheAfterPurgeAll
|
||||
flextype('emitter')->emit('onCacheAfterPurgeAll');
|
||||
|
||||
// Remove cache directory
|
||||
Filesystem::deleteDir(PATH['cache']);
|
||||
|
||||
// Save and Mute error_reporting
|
||||
$errorReporting = error_reporting();
|
||||
error_reporting(0);
|
||||
|
||||
// Clear stat cache
|
||||
clearstatcache();
|
||||
|
||||
// Clear opcache
|
||||
function_exists('opcache_reset') and opcache_reset();
|
||||
|
||||
// Restore error_reporting
|
||||
error_reporting($errorReporting);
|
||||
|
||||
// Run event: onCacheAfterPurgeAll
|
||||
flextype('emitter')->emit('onCacheAfterPurgeAll');
|
||||
}
|
||||
}
|
@@ -1,20 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flextype\App\Foundation\Cache;
|
||||
|
||||
interface CacheAdapterInterface
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function __construct();
|
||||
|
||||
/**
|
||||
* Returns the cache driver object
|
||||
*/
|
||||
public function getDriver() : object;
|
||||
}
|
@@ -1,32 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flextype\App\Foundation\Cache;
|
||||
|
||||
use Doctrine\Common\Cache\FilesystemCache;
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
|
||||
class FilesystemCacheAdapter implements CacheAdapterInterface
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function getDriver() : object
|
||||
{
|
||||
$cache_directory = PATH['cache'] . '/doctrine/';
|
||||
|
||||
if (! Filesystem::has($cache_directory)) {
|
||||
Filesystem::createDir($cache_directory);
|
||||
}
|
||||
|
||||
return new FilesystemCache($cache_directory);
|
||||
}
|
||||
}
|
@@ -1,35 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flextype\App\Foundation\Cache;
|
||||
|
||||
use Doctrine\Common\Cache\MemcachedCache;
|
||||
use Memecached;
|
||||
|
||||
class MemcachedCacheAdapter implements CacheAdapterInterface
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function getDriver() : object
|
||||
{
|
||||
$memcached = new Memecached();
|
||||
$memcached->addServer(
|
||||
flextype('registry')->get('flextype.settings.cache.memcached.server', 'localhost'),
|
||||
flextype('registry')->get('flextype.settings.cache.memcache.port', 11211)
|
||||
);
|
||||
|
||||
$driver = new MemcachedCache();
|
||||
$driver->setMemcached($memcached);
|
||||
|
||||
return $driver;
|
||||
}
|
||||
}
|
@@ -1,113 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flextype\App\Foundation\Cache;
|
||||
|
||||
use Doctrine\Common\Cache\FileCache;
|
||||
use function restore_error_handler;
|
||||
use function set_error_handler;
|
||||
use function sprintf;
|
||||
use function time;
|
||||
use function var_export;
|
||||
|
||||
/**
|
||||
* Php file cache driver.
|
||||
*/
|
||||
class PhpArrayFileCache extends FileCache
|
||||
{
|
||||
public const EXTENSION = '.doctrineacache.php';
|
||||
|
||||
/**
|
||||
* @var callable
|
||||
*
|
||||
* This is cached in a local static variable to avoid instantiating a closure each time we need an empty handler
|
||||
*/
|
||||
private static $emptyErrorHandler;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct($directory, $extension = self::EXTENSION, $umask = 0002)
|
||||
{
|
||||
parent::__construct($directory, $extension, $umask);
|
||||
|
||||
self::$emptyErrorHandler = static function () : void {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doFetch($id)
|
||||
{
|
||||
$value = $this->includeFileForId($id);
|
||||
|
||||
if ($value === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($value['lifetime'] !== 0 && $value['lifetime'] < time()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $value['data'];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doContains($id)
|
||||
{
|
||||
$value = $this->includeFileForId($id);
|
||||
|
||||
if ($value === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $value['lifetime'] === 0 || $value['lifetime'] > time();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doSave($id, $data, $lifeTime = 0)
|
||||
{
|
||||
if ($lifeTime > 0) {
|
||||
$lifeTime = time() + $lifeTime;
|
||||
}
|
||||
|
||||
$filename = $this->getFilename($id);
|
||||
|
||||
$value = [
|
||||
'lifetime' => $lifeTime,
|
||||
'data' => $data,
|
||||
];
|
||||
|
||||
$value = var_export($value, true);
|
||||
$code = sprintf('<?php return %s;', $value);
|
||||
|
||||
return $this->writeFile($filename, $code);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|null
|
||||
*/
|
||||
private function includeFileForId(string $id) : ?array
|
||||
{
|
||||
$fileName = $this->getFilename($id);
|
||||
|
||||
// note: error suppression is still faster than `file_exists`, `is_file` and `is_readable`
|
||||
set_error_handler(self::$emptyErrorHandler);
|
||||
|
||||
$value = include $fileName;
|
||||
|
||||
restore_error_handler();
|
||||
|
||||
if (! isset($value['lifetime'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
@@ -1,31 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flextype\App\Foundation\Cache;
|
||||
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
|
||||
class PhpArrayFileCacheAdapter implements CacheAdapterInterface
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function getDriver() : object
|
||||
{
|
||||
$cache_directory = PATH['cache'] . '/doctrine/';
|
||||
|
||||
if (! Filesystem::has($cache_directory)) {
|
||||
Filesystem::createDir($cache_directory);
|
||||
}
|
||||
|
||||
return new PhpArrayFileCache($cache_directory);
|
||||
}
|
||||
}
|
@@ -1,32 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flextype\App\Foundation\Cache;
|
||||
|
||||
use Doctrine\Common\Cache\PhpFileCache;
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
|
||||
class PhpFileCacheAdapter implements CacheAdapterInterface
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function getDriver() : object
|
||||
{
|
||||
$cache_directory = PATH['cache'] . '/doctrine/';
|
||||
|
||||
if (! Filesystem::has($cache_directory)) {
|
||||
Filesystem::createDir($cache_directory);
|
||||
}
|
||||
|
||||
return new PhpFileCache($cache_directory);
|
||||
}
|
||||
}
|
@@ -1,48 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flextype\App\Foundation\Cache;
|
||||
|
||||
use Doctrine\Common\Cache\RedisCache;
|
||||
use Redis;
|
||||
use RedisException;
|
||||
|
||||
class RedisCacheAdapter implements CacheAdapterInterface
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function getDriver() : object
|
||||
{
|
||||
$redis = new Redis();
|
||||
$socket = flextype('registry')->get('flextype.settings.cache.redis.socket', false);
|
||||
$password = flextype('registry')->get('flextype.settings.cache.redis.password', false);
|
||||
|
||||
if ($socket) {
|
||||
$redis->connect($socket);
|
||||
} else {
|
||||
$redis->connect(
|
||||
flextype('registry')->get('flextype.settings.cache.redis.server', 'localhost'),
|
||||
flextype('registry')->get('flextype.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;
|
||||
}
|
||||
}
|
@@ -1,35 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flextype\App\Foundation\Cache;
|
||||
|
||||
use Doctrine\Common\Cache\SQLite3Cache;
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
use SQLite3;
|
||||
|
||||
class SQLite3CacheAdapter implements CacheAdapterInterface
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function getDriver() : object
|
||||
{
|
||||
$cache_directory = PATH['cache'] . '/doctrine/';
|
||||
|
||||
if (! Filesystem::has($cache_directory)) {
|
||||
Filesystem::createDir($cache_directory);
|
||||
}
|
||||
|
||||
$db = new SQLite3($cache_directory . flextype('registry')->get('flextype.settings.cache.sqlite3.database', 'flextype') . '.db');
|
||||
|
||||
return new SQLite3Cache($db, flextype('registry')->get('flextype.settings.cache.sqlite3.table', 'flextype'));
|
||||
}
|
||||
}
|
@@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flextype\App\Foundation\Cache;
|
||||
|
||||
use Doctrine\Common\Cache\WinCacheCache;
|
||||
|
||||
class WinCacheCacheAdapter implements CacheAdapterInterface
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function getDriver() : object
|
||||
{
|
||||
return new WinCacheCache();
|
||||
}
|
||||
}
|
@@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flextype\App\Foundation\Cache;
|
||||
|
||||
use Doctrine\Common\Cache\ZendDataCache;
|
||||
|
||||
class ZendDataCacheCacheAdapter implements CacheAdapterInterface
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function getDriver() : object
|
||||
{
|
||||
return new ZendDataCache();
|
||||
}
|
||||
}
|
@@ -108,9 +108,9 @@ class Entries
|
||||
$entry_cache_id = $this->getCacheID($this->entry_id);
|
||||
|
||||
// Try to get current requested entry from cache
|
||||
if (flextype('cache')->contains($entry_cache_id)) {
|
||||
if (flextype('cache')->has($entry_cache_id)) {
|
||||
// Fetch entry from cache
|
||||
$this->entry = flextype('cache')->fetch($entry_cache_id);
|
||||
$this->entry = flextype('cache')->get($entry_cache_id);
|
||||
|
||||
// Run event: onEntryAfterCacheInitialized
|
||||
flextype('emitter')->emit('onEntryAfterCacheInitialized');
|
||||
@@ -142,7 +142,7 @@ class Entries
|
||||
|
||||
// Save entry data to cache
|
||||
if ($cache) {
|
||||
flextype('cache')->save($entry_cache_id, $this->entry);
|
||||
flextype('cache')->set($entry_cache_id, $this->entry);
|
||||
}
|
||||
|
||||
// Return entry data
|
||||
|
@@ -78,15 +78,15 @@ class Plugins
|
||||
}
|
||||
|
||||
// Get plugins from cache or scan plugins folder and create new plugins cache item
|
||||
if (flextype('cache')->contains($plugins_cache_id)) {
|
||||
flextype('registry')->set('plugins', flextype('cache')->fetch($plugins_cache_id));
|
||||
if (flextype('cache')->has($plugins_cache_id)) {
|
||||
flextype('registry')->set('plugins', flextype('cache')->get($plugins_cache_id));
|
||||
|
||||
if (flextype('cache')->contains($locale)) {
|
||||
I18n::add(flextype('cache')->fetch($locale), $locale);
|
||||
if (flextype('cache')->has($locale)) {
|
||||
I18n::add(flextype('cache')->get($locale), $locale);
|
||||
} else {
|
||||
// Save plugins dictionary
|
||||
$dictionary = $this->getPluginsDictionary($plugins_list, $locale);
|
||||
flextype('cache')->save($locale, $dictionary[$locale]);
|
||||
flextype('cache')->set($locale, $dictionary[$locale]);
|
||||
}
|
||||
} else {
|
||||
// Init plugin configs
|
||||
@@ -167,11 +167,11 @@ class Plugins
|
||||
|
||||
// Save plugins list
|
||||
flextype('registry')->set('plugins', $plugins);
|
||||
flextype('cache')->save($plugins_cache_id, $plugins);
|
||||
flextype('cache')->set($plugins_cache_id, $plugins);
|
||||
|
||||
// Save plugins dictionary
|
||||
$dictionary = $this->getPluginsDictionary($plugins_list, $locale);
|
||||
flextype('cache')->save($locale, $dictionary[$locale]);
|
||||
flextype('cache')->set($locale, $dictionary[$locale]);
|
||||
}
|
||||
|
||||
$this->includeEnabledPlugins();
|
||||
|
@@ -41,12 +41,12 @@ class Markdown
|
||||
if ($cache === true && flextype('registry')->get('flextype.settings.cache.enabled') === true) {
|
||||
$key = $this->getCacheID($input);
|
||||
|
||||
if ($data_from_cache = flextype('cache')->fetch($key)) {
|
||||
if ($data_from_cache = flextype('cache')->get($key)) {
|
||||
return $data_from_cache;
|
||||
}
|
||||
|
||||
$data = $this->_parse($input);
|
||||
flextype('cache')->save($key, $data);
|
||||
flextype('cache')->set($key, $data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
@@ -89,12 +89,12 @@ class Shortcode
|
||||
if ($cache === true && flextype('registry')->get('flextype.settings.cache.enabled') === true) {
|
||||
$key = $this->getCacheID($input);
|
||||
|
||||
if ($data_from_cache = flextype('cache')->fetch($key)) {
|
||||
if ($data_from_cache = flextype('cache')->get($key)) {
|
||||
return $data_from_cache;
|
||||
}
|
||||
|
||||
$data = $this->shortcode->process($input);
|
||||
flextype('cache')->save($key, $data);
|
||||
flextype('cache')->set($key, $data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
@@ -47,12 +47,12 @@ class Frontmatter
|
||||
if ($cache === true && flextype('registry')->get('flextype.settings.cache.enabled') === true) {
|
||||
$key = $this->getCacheID($input);
|
||||
|
||||
if ($data_from_cache = flextype('cache')->fetch($key)) {
|
||||
if ($data_from_cache = flextype('cache')->get($key)) {
|
||||
return $data_from_cache;
|
||||
}
|
||||
|
||||
$data = $this->_decode($input);
|
||||
flextype('cache')->save($key, $data);
|
||||
flextype('cache')->set($key, $data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
@@ -59,12 +59,12 @@ class Json
|
||||
if ($cache === true && flextype('registry')->get('flextype.settings.cache.enabled') === true) {
|
||||
$key = $this->getCacheID($input);
|
||||
|
||||
if ($data_from_cache = flextype('cache')->fetch($key)) {
|
||||
if ($data_from_cache = flextype('cache')->get($key)) {
|
||||
return $data_from_cache;
|
||||
}
|
||||
|
||||
$data = $this->_decode($input, $assoc, $depth, $flags);
|
||||
flextype('cache')->save($key, $data);
|
||||
flextype('cache')->set($key, $data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
@@ -76,12 +76,12 @@ class Yaml
|
||||
if ($cache === true && flextype('registry')->get('flextype.settings.cache.enabled') === true) {
|
||||
$key = $this->getCacheID($input);
|
||||
|
||||
if ($data_from_cache = flextype('cache')->fetch($key)) {
|
||||
if ($data_from_cache = flextype('cache')->get($key)) {
|
||||
return $data_from_cache;
|
||||
}
|
||||
|
||||
$data = $this->_decode($input, $flags);
|
||||
flextype('cache')->save($key, $data);
|
||||
flextype('cache')->set($key, $data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
@@ -11,7 +11,7 @@ namespace Flextype;
|
||||
|
||||
use Bnf\Slim3Psr15\CallableResolver;
|
||||
use Cocur\Slugify\Slugify;
|
||||
use Flextype\App\Foundation\Cache\Cache;
|
||||
//use Flextype\App\Foundation\Cache\Cache;
|
||||
use Flextype\App\Foundation\Cors;
|
||||
use Flextype\App\Foundation\Entries\Entries;
|
||||
use Flextype\App\Foundation\Media\MediaFiles;
|
||||
@@ -51,6 +51,7 @@ use ParsedownExtra;
|
||||
use Thunder\Shortcode\ShortcodeFacade;
|
||||
use function date;
|
||||
use function extension_loaded;
|
||||
use Phpfastcache\Helper\Psr16Adapter as Cache;
|
||||
|
||||
|
||||
/**
|
||||
@@ -98,6 +99,103 @@ flextype()->container()['slugify'] = static function () {
|
||||
]);
|
||||
};
|
||||
|
||||
|
||||
flextype()->container()['cache'] = static function () {
|
||||
|
||||
$drivers = [
|
||||
'apcu' => 'Apcu',
|
||||
'wincache' => 'Wincache',
|
||||
'array' => 'Array',
|
||||
'filesystem' => 'Filesystem',
|
||||
'memcached' => 'Memcached',
|
||||
'redis' => 'Redis',
|
||||
'sqlite3' => 'SQLite3',
|
||||
'zenddatacache' => 'ZendDataCache',
|
||||
];
|
||||
|
||||
$driver_name = flextype('registry')->get('flextype.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 = 'files';
|
||||
}
|
||||
}
|
||||
|
||||
if (flextype('registry')->get('flextype.settings.cache.enabled') === false) {
|
||||
$driver_name = 'Devfalse';
|
||||
}
|
||||
|
||||
switch ($driver_name) {
|
||||
case 'apcu':
|
||||
$config = new \Phpfastcache\Drivers\Apcu\Config([]);
|
||||
break;
|
||||
case 'wincache':
|
||||
$config = new \Phpfastcache\Drivers\Wincache\Config([]);
|
||||
break;
|
||||
case 'files':
|
||||
$config = new \Phpfastcache\Drivers\Files\Config([
|
||||
'path' => flextype('registry')->get('flextype.cache.drivers.files.path') !=='' ? PATH['cache'] . '/' . flextype('registry')->get('flextype.cache.drivers.files.path') : sys_get_temp_dir(),
|
||||
'secureFileManipulation' => flextype('registry')->get('flextype.settings.cache.drivers.files.secure_file_manipulation'),
|
||||
'htaccess' => flextype('registry')->get('flextype.settings.cache.drivers.files.htaccess'),
|
||||
'securityKey' => (string) flextype('registry')->get('flextype.settings.cache.drivers.files.security_key'),
|
||||
'cacheFileExtension' => flextype('registry')->get('flextype.settings.cache.drivers.files.cache_file_extension'),
|
||||
'itemDetailedDate' => flextype('registry')->get('flextype.settings.cache.drivers.files.item_detailed_date'),
|
||||
'autoTmpFallback' => flextype('registry')->get('flextype.settings.cache.drivers.files.auto_tmp_fallback'),
|
||||
'defaultTtl' => flextype('registry')->get('flextype.settings.cache.drivers.files.default_ttl'),
|
||||
'defaultKeyHashFunction' => flextype('registry')->get('flextype.settings.cache.drivers.files.default_key_hash_function'),
|
||||
'defaultFileNameHashFunction' => flextype('registry')->get('flextype.settings.cache.drivers.files.default_file_name_hash_function'),
|
||||
'defaultChmod' => flextype('registry')->get('flextype.settings.cache.drivers.files.default_chmod'),
|
||||
'preventCacheSlams' => flextype('registry')->get('flextype.settings.cache.drivers.files.prevent_cache_slams'),
|
||||
'cacheSlamsTimeout' => flextype('registry')->get('flextype.settings.cache.drivers.files.cache_slams_timeout'),
|
||||
]);
|
||||
break;
|
||||
case 'sqlite':
|
||||
$config = new \Phpfastcache\Drivers\Sqlite\Config([
|
||||
'path' => flextype('registry')->get('flextype.cache.drivers.files.path') !=='' ? PATH['cache'] . '/' . flextype('registry')->get('flextype.cache.drivers.files.path') : sys_get_temp_dir(),
|
||||
'secureFileManipulation' => flextype('registry')->get('flextype.settings.cache.drivers.files.secure_file_manipulation'),
|
||||
'htaccess' => flextype('registry')->get('flextype.settings.cache.drivers.files.htaccess'),
|
||||
'securityKey' => (string) flextype('registry')->get('flextype.settings.cache.drivers.files.security_key'),
|
||||
'cacheFileExtension' => flextype('registry')->get('flextype.settings.cache.drivers.files.cache_file_extension'),
|
||||
'itemDetailedDate' => flextype('registry')->get('flextype.settings.cache.drivers.files.item_detailed_date'),
|
||||
'autoTmpFallback' => flextype('registry')->get('flextype.settings.cache.drivers.files.auto_tmp_fallback'),
|
||||
'defaultTtl' => flextype('registry')->get('flextype.settings.cache.drivers.files.default_ttl'),
|
||||
'defaultChmod' => flextype('registry')->get('flextype.settings.cache.drivers.files.default_chmod'),
|
||||
'preventCacheSlams' => flextype('registry')->get('flextype.settings.cache.drivers.files.prevent_cache_slams'),
|
||||
'cacheSlamsTimeout' => flextype('registry')->get('flextype.settings.cache.drivers.files.cache_slams_timeout'),
|
||||
]);
|
||||
break;
|
||||
default:
|
||||
// code...
|
||||
break;
|
||||
}
|
||||
|
||||
return new Cache($driver_name, $config);
|
||||
};
|
||||
|
||||
|
||||
if(flextype()->container('cache')->has('test-key')){
|
||||
// Setter action
|
||||
|
||||
echo '1';
|
||||
|
||||
|
||||
// Getter action
|
||||
$data = flextype()->container('cache')->get('test-key');
|
||||
|
||||
}else{
|
||||
echo '2';
|
||||
|
||||
$data = 'lorem ipsum';
|
||||
flextype()->container('cache')->set('test-key', 'lorem ipsum', 300);// 5 minutes
|
||||
|
||||
}
|
||||
|
||||
dd($data);
|
||||
|
||||
/**
|
||||
* Adds the cache adapter to the Flextype container
|
||||
*/
|
||||
|
@@ -91,47 +91,154 @@ entries:
|
||||
# - driver: Available drivers: auto (will get one from installed cache drivers), apcu,
|
||||
# apc, array, wincache, xcache, memcache, memcached, redis, file.
|
||||
#
|
||||
# - lifetime: Lifetime of cached data in seconds
|
||||
# - drivers.files.path: The path where the written cache files belong to (system temp directory by default).
|
||||
#
|
||||
# - redis.socket: Path to redis unix socket (e.g. /var/run/redis/redis.sock),
|
||||
# false = use server and port to connect
|
||||
# - drivers.files.secure_file_manipulation: This option enforces a strict I/O manipulation policy by adding more integrity checks.
|
||||
# This option may slow down the write operations, therefore you must use it with caution.
|
||||
#
|
||||
# - redis.password Redis password
|
||||
# - drivers.files.htaccess: Option designed to (dis)allow the auto-generation of .htaccess.
|
||||
#
|
||||
# - redis.server Redis server
|
||||
# - drivers.files.security_key: A security key that define the subdirectory name. 'auto' value will be the HTTP_HOST value.
|
||||
#
|
||||
# - redis.port Redis port
|
||||
# - drivers.files.cache_file_extension: This allows you to setup a custom (but safe) cache file extension. ('txt|cache|db|pfc')
|
||||
#
|
||||
# - memcache.server Memcache server
|
||||
# - drivers.files.item_detailed_dates: This option will define if the Items will make use of detailed dates such as Creation/modification date.
|
||||
# Trying to access to these date without being explicitly enabled will throw a LogicException
|
||||
#
|
||||
# - memcache.port Memcache port
|
||||
# - drivers.files.auto_tmp_fallback: Option designed to automatically attempt to fallback to temporary directory if the cache fails to write on the specified directory.
|
||||
#
|
||||
# - memcached.server Memcached server
|
||||
# - drivers.files.default_ttl: This option define a default ttl (time-to-live, in seconds) for item that has no specified expiration date/ttl.
|
||||
#
|
||||
# - memcached.port Memcached port
|
||||
# - drivers.files.default_key_hash_function: This option will allow you to define a custom hash function for the $item->getEncodedKey() method.
|
||||
# Callable objects are not allowed, but static method such as \Absolute\Namespace\ToStatic::method are allowed.
|
||||
#
|
||||
# - sqlite3.database SQLite3 Database
|
||||
# - drivers.files.default_file_name_hash_function: This option will allow you to define a custom hash function for every I/O method that ends up to write an hashed filename on the disk.
|
||||
#
|
||||
# - sqlite3.table SQLite3 Table
|
||||
# - drivers.files.default_chmod: This option will define the chmod used to write driver cache files.
|
||||
#
|
||||
# - drivers.files.limited_memory_by_object:
|
||||
#
|
||||
# - drivers.files.compress_data:
|
||||
#
|
||||
# - drivers.files.prevent_cache_slams: This option will allow you to prevent cache slams when making use of heavy cache items.
|
||||
#
|
||||
# - drivers.files.cache_slams_timeout: This option defines the cache slams timeout in seconds.
|
||||
|
||||
cache:
|
||||
enabled: true
|
||||
prefix: flextype
|
||||
driver: auto
|
||||
lifetime: 604800
|
||||
memcache:
|
||||
server: localhost
|
||||
port: 11211
|
||||
memcached:
|
||||
server: localhost
|
||||
port: 11211
|
||||
redis:
|
||||
socket: false
|
||||
password: false
|
||||
server: localhost
|
||||
port: 6379
|
||||
sqlite3:
|
||||
database: flextype
|
||||
table: flextype
|
||||
driver: sqlite
|
||||
drivers:
|
||||
apcu: {}
|
||||
cassandra:
|
||||
host: '127.0.0.1'
|
||||
port: 9042
|
||||
timeout: 2
|
||||
username: ''
|
||||
password: ''
|
||||
ssl_enabled: false
|
||||
ssl_verify: false
|
||||
default_ttl: 900
|
||||
cookie:
|
||||
aware_of_untrustable_data: false
|
||||
limited_memory_by_object: 4096
|
||||
default_ttl: 900
|
||||
couchbase:
|
||||
host: '127.0.0.1'
|
||||
port: 8091
|
||||
username: ''
|
||||
password: ''
|
||||
bucket_name: default
|
||||
default_ttl: 900
|
||||
couchdb:
|
||||
database: 'flextype'
|
||||
path: '/'
|
||||
host: '127.0.0.1'
|
||||
port: 8091
|
||||
username: ''
|
||||
password: ''
|
||||
bucket_name: default
|
||||
ssl: false
|
||||
timeout: 10
|
||||
default_ttl: 900
|
||||
devnull: {}
|
||||
devfalse: {}
|
||||
devtrue: {}
|
||||
files:
|
||||
path: /tmp
|
||||
security_key: 'auto'
|
||||
htaccess: true
|
||||
secure_file_manipulation: false
|
||||
cache_file_extension: txt
|
||||
default_ttl: 900
|
||||
leveldb:
|
||||
path: /tmp
|
||||
security_key: 'auto'
|
||||
htaccess: true
|
||||
default_ttl: 900
|
||||
memcache:
|
||||
host: '127.0.0.1'
|
||||
port: 11211
|
||||
sasl_user: ''
|
||||
sasl_password: ''
|
||||
path: '/'
|
||||
compress_data: false
|
||||
servers: {}
|
||||
default_ttl: 900
|
||||
memcached:
|
||||
host: '127.0.0.1'
|
||||
port: 11211
|
||||
sasl_user: ''
|
||||
sasl_password: ''
|
||||
path: '/'
|
||||
compress_data: false
|
||||
servers: {}
|
||||
default_ttl: 900
|
||||
memstatic: {}
|
||||
mongodb:
|
||||
host: '127.0.0.1'
|
||||
port: 27017
|
||||
username: ''
|
||||
password: ''
|
||||
timeout: 3
|
||||
servers: {}
|
||||
collection_name: 'Cache'
|
||||
default_ttl: 900
|
||||
predis:
|
||||
host: '127.0.0.1'
|
||||
port: 6379
|
||||
password: ''
|
||||
timeout: 5
|
||||
database: 0
|
||||
opt_prefix: ''
|
||||
persistent: false
|
||||
default_ttl: 900
|
||||
redis:
|
||||
host: '127.0.0.1'
|
||||
port: 6379
|
||||
password: ''
|
||||
timeout: 5
|
||||
database: 0
|
||||
opt_prefix: ''
|
||||
default_ttl: 900
|
||||
riak:
|
||||
host: '127.0.0.1'
|
||||
port: 8098
|
||||
prefix: 'riak'
|
||||
default_ttl: 900
|
||||
sqlite:
|
||||
path: /tmp
|
||||
security_key: auto
|
||||
htaccess: true
|
||||
default_ttl: 900
|
||||
ssdb:
|
||||
host: 127.0.0.1
|
||||
port: 8888
|
||||
password: ''
|
||||
timeout: 5
|
||||
wincache: {}
|
||||
xcache: {}
|
||||
zenddisk: {}
|
||||
zendshm: {}
|
||||
|
||||
# Whoops
|
||||
#
|
||||
|
Reference in New Issue
Block a user