1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-11 15:44:49 +02:00

feat(cache): Move to PhpFastCache - first commit #457

This commit is contained in:
Awilum
2020-08-28 16:08:50 +03:00
parent f2a183ab4c
commit 856a8b8288
4 changed files with 164 additions and 31 deletions

View File

@@ -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",

View File

@@ -348,7 +348,7 @@ class Cache
public function purgeAll() : void
{
// Run event: onCacheAfterPurgeAll
flextype('emitter')->emit('onCacheAfterPurgeAll');
flextype('emitter')->emit('onCacheBeforePurgeAll');
// Remove cache directory
Filesystem::deleteDir(PATH['cache']);

View File

@@ -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
*/

View File

@@ -91,47 +91,80 @@ 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:
files:
path: 'cache'
secure_file_manipulation: false
htaccess: true
security_key: ''
cache_file_extension: 'txt'
item_detailed_date: false
auto_tmp_fallback: false
default_ttl: 900
default_key_hash_function: 'md5'
default_file_name_hash_function: 'md5'
default_chmod: 0777
prevent_cache_slams: false
cache_slams_timeout: 15
sqlite:
path: 'sqlite'
secure_file_manipulation: false
htaccess: true
security_key: ''
item_detailed_date: false
auto_tmp_fallback: false
default_ttl: 900
default_chmod: 0777
prevent_cache_slams: false
cache_slams_timeout: 15
apcu:
-
cassandra:
host: '127.0.0.1'
port: 9042
timeout: 2
username: ''
password: ''
ssl_enabled: false
ssl_verify: false
cookie:
# Whoops
#