andydune / retain-cache-on-data-absent
Code allow restore data in cache if new data can not be retrieved.
Requires
- php: >=5.6
Requires (Dev)
- phpunit/phpunit: ^5.7.15
- symfony/cache: 3.*
This package is auto-updated.
Last update: 2024-10-14 21:51:40 UTC
README
It allows to restore data in cache if new data can not be retrieved.
Installation
Installation using composer:
composer require andydune/retain-cache-on-data-absent
Or if composer was not installed globally:
php composer.phar require andydune/retain-cache-on-data-absent
Or edit your composer.json
:
"require" : {
"andydune/retain-cache-on-data-absent": "^1"
}
And execute command:
php composer.phar update
Problem
Yor script gets data from external API. For example currency rate from crr.
There is no need to extract data every time it is needed. So we are using cache. But what will be if cache get old, but no data appears from api? There is network error or bank site breakage.
This little library helps to avoid data absent from retriever. Cached data do not expires and if no new data appears it uses old data from cache.
Using
use Symfony\Component\Cache\Simple\FilesystemCache; use AndyDune\RetainCacheOnDataAbsent\Cache; $cacheAdapter = new FilesystemCache('test', 3600, '<root cache dir>'); $cache = new Cache($cacheAdapter, function () { /* $data = false; // no data - return data from old cache $data = 'very useful data to cache and use'; // update cache and return this data */ return $data; }); $result = $cache->get('data'); // use any key - it is used only for cache key