mimatus/export-cache

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

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

Read
Write

To see full results use:

make build
make benchmark

Requires: docker

๐Ÿ’Œ Credits

Similar projects