mimatus / export-cache
PSR-6 Cache without deserialization speeded-up with OPcache
dev-master
2023-08-05 18:29 UTC
Requires
- php: >=8.2
- brick/varexporter: ^0.3.8
- psr/simple-cache: ^2.0|^3.0
Requires (Dev)
- cache/integration-tests: dev-master
- phpbench/phpbench: ^1.2.14
- phpstan/phpstan: ^1.10.26
- phpunit/phpunit: ^10.2.6
- slevomat/coding-standard: ^8.13.1
Provides
- psr/simple-cache-implementation: 2.0|3.0
This package is not auto-updated.
Last update: 2024-12-22 22:00:51 UTC
README
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).
๐ Installation
composer require mimatus/export-cache
It's highly recommanded to enable OPcache for best performance
Usage
Use when ...
- 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
Don't use when...
- basically in any other use-case which is not in Use when ... section ๐
Example
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);
Data limitations
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
Concurrency
- Dirty Reads
- Lost Updates
- Phantom read
- Non-repeatable Reads - WIP
Performance - WIP
To see full results use:
make build make benchmark
Requires: docker
๐ Credits
- serialization - brick/varexporter