php-strict / storable-cache
Storable cache store items more than their lifetime, up to time to save.
v1.0.0
2019-12-06 11:42 UTC
Requires
- php: ^7.2
- php-strict/config: ~1.0
Requires (Dev)
- codeception/codeception: ^2.5.1
This package is auto-updated.
Last update: 2024-10-16 16:48:05 UTC
README
Storable cache store items more than their lifetime, up to time to save, and allow use stored items after expiration, in cases, for example, of db overload or errors.
Supported storages:
- arrays (not real storage, only for testing),
- files,
- memcached,
- redis,
- SQLite,
- MySQL (uses main db connection from app).
Requirements
- PHP >= 7.2
- php-strict/config
Install
Install with Composer:
composer require php-strict/storable-cache
Usage
Basic usage:
use PhpStrict\Config\Config; use PhpStrict\StorableCache\StorableCache; //instance of application configuration class, extending Config //must provide cacheType property with correct storable cache type //see PhpStrict\StorableCache\StorageTypes class $config = new AppConfig(); $config->loadFromFile('config.ini'); //instance of StorableCache $cache = new StorableCache($config); //part of generating content method if ($cache->has('contentKey') && !$cache->expired('contentKey')) { return $cache->get('contentKey'); } //part of generating content method //saving generated content: key, value, ttl, tts (time to save) $cache->set('contentKey', $content, 60, 3600);
Usage if content generating main process was failed and it is correct to use expired data:
use PhpStrict\StorableCache\StorableCache; //part of generating content method //generating content failed if ($cache->has('contentKey')) { return $cache->get('contentKey'); } throw Exception('Generating content failed'); //part of generating content method
Tests
To execute the test suite, you'll need Codeception.
vendor\bin\codecept run