PSR-6 Cache without deserialization speeded-up with OPcache
This cache package aim to target small niche use-cases when deserialization of stored data can have significant performance impact and when data stored in cache are updated rarely (or never, mainly because of write performance).
composer require mimatus/export-cache
It's highly recommanded to enable OPcache for best performance
- OPcache is enabled
- cached data are rarely changing (too frequent changes might lead to periodical reset of the OPcache memory)
- needs cache shared by PHP processes
- basically in any other use-case which is not in Use when ... section 😅
use MiMatus\ExportCache\ExportCache;
$storagePath = sys_get_temp_dir() . \DIRECTORY_SEPARATOR . 'export-cache';
$cache = new ExportCache($storagePath);
$closure = function () {
return 'data'
};
$cache->set('key0', 'data', new DateInterval('P1D'));
$cache->set('key1', ['data']);
$cache->set('key2', $closure);
$cache->set('key3', 'expired data', new DateInterval('P1S'));
sleep(2);
assert($cache->get('key0') === 'data');
assert($cache->get('key1') === ['data']);
assert($cache->get('key2')() === 'data');
assert($cache->get('key3') === null);
Thanks to brick/varexporter which is used for data serialization, it's possible to cache almost any PHP value, even closures, however with some limitations:
- PHP Internal objects - SplFileInfo, XMLReader, etc.
- objects with circular references
- annonymous classes
- eval()'d or same line declared Closures, more info
- Dirty Reads
- Lost Updates
- Phantom read
- Non-repeatable Reads - WIP
To see full results use:
make build
make benchmark
Requires: docker
- serialization - brick/varexporter