Light, simple and standalone PHP in-file caching class
Advantages
Click here to view the code.
Install via composer:
composer require wruczek/php-file-cache
Usage is fairly straightforward.
use Wruczek\PhpFileCache\PhpFileCache; $app = Flight::app(); // You pass the directory the cache will be stored in into the constructor $app->register('cache', PhpFileCache::class, [ __DIR__ . '/../cache/' ], function(PhpFileCache $cache) { // This ensures that the cache is only used when in production mode // ENVIRONMENT is a constant that is set in your bootstrap file or elsewhere in your app $cache->setDevMode(ENVIRONMENT === 'development'); });
Then you can use it in your code like this:
// Get cache instance $cache = Flight::cache(); $data = $cache->refreshIfExpired('simple-cache-test', function () { return date("H:i:s"); // return data to be cached }, 10); // 10 seconds // or $data = $cache->retrieve('simple-cache-test'); if(empty($data)) { $data = date("H:i:s"); $cache->store('simple-cache-test', $data, 10); // 10 seconds }
Visit https://github.com/Wruczek/PHP-File-Cache for full documentation and make sure you see the examples folder.