Luminova Framework

File Cache Examples

Last updated: 2024-06-22 16:36:55

In this documentation, we show the basic usages of the Luminova FileCache, to efficiently store and retrieve reusable data in the file system, reducing the need for frequent database or API calls.


Create an instance of FileCache with specified cache storage and folder names.

use \Luminova\Cache\FileCache;

$cache = new FileCache('cache_name', 'check_folder_name');

Dynamic Initialization:

If storage is not specified during initialization, set the storage name later and then call create method to enable the cache.

use \Luminova\Cache\FileCache;

$cache = new FileCache();

Store and Retrieve Content:

Use the callback method to fetch content if the cache has expired or does not exist.

$content = $cache->onExpired('cache_key', static function(){
    return databaseLoadContent();

Get Cached Content:

Retrieve content from the cache.

$content = $cache->getItem('cache_key');

Force Update Cache Content:

Update cache content forcibly.

$data = 'New content to update';

if ($cache->refresh('cache_key', $data, 60 * 20)) {
    // Cache updated

Save Content:

Save content to the cache. The fourth parameter lock indicates whether the cache should allow automatic expiration and deletion.

$data = 'New content to update';

if ($cache->setItem('cache_key', $data, 60 * 20, 60 * 20, true)) {
    // Cache saved