PSR-6 Cache without deserialization speeded-up with OPcache

dev-master 2023-08-05 18:29 UTC

This package is not auto-updated.

Last update: 2024-02-18 18:17:39 UTC


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


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...


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'));


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


  • Dirty Reads
  • Lost Updates
  • Phantom read
  • Non-repeatable Reads - WIP

Performance - WIP

Read bench-read.png
Write bench-write.png

To see full results use:

make build
make benchmark

Requires: docker

💌 Credits

Similar projects