adinan-cenci/file-cache

A PSR-16 implementation around the PHP Filesystem.

2.0.1 2021-01-12 09:43 UTC

This package is auto-updated.

Last update: 2024-11-12 17:46:44 UTC


README

This is a PSR-16 implementation build around the php Filesystem.

How to use it

Once instantiated, use like specified in the PSR-16

use AdinanCenci\FileCache\Cache;
$cache = new Cache('my-cache-directory/');

Caching

Inform an unique identifier for the data you desire to cache. Optionally you may inform its time to live, otherwise the cached data will never expire.

$cache->set('somethingCostlyToAcquire', $value, 60 * 60 * 24);

Caching multiple values at once

$cache->setMultiple([
    'foo'           => $bar,
    'hello'         => $world,
    'myObject'      => $myObject
], 60 * 60 * 24);

Retrieving

Use ::get to retrieve your data, if the data doesn't exist in cache or has expired then a fallback value will be returned, which defaults to null if not informed.

$fallback = 'nothing here';
$cache->get('somethingCostlyToAcquire', $fallback);

Retrieving multiple values at once

$cache->getMultiple([
    'object1', 
    'value1',
    'anotherObject'
], $fallback);

How to install

Use composer

composer require adinan-cenci/file-cache

License

MIT