Luminova Framework

PHP Luminova: Store and Retrieve Cached Data

Last updated: 2026-11-28 20:57:12

Store and reuse data with the file or Memcached driver. Read several entries, set expiration, and remove stale values.

Start with the file cache and use one storage name throughout the examples. The Memcached examples require the PHP extension and a running Memcached server. Keep each cached value under a key that identifies it, such as a product ID. See the Filecache Storage Caching System.


Initialization

You can initialize the FileCache or MemoryCache using the namespaced cache() helper function. Specify the cache storage name and folder for file storage, or the persistent ID for Memcached:

use function Luminova\Funcs\cache;

$cache = cache('filecache', 'storage_name', 'cache_folder_name'); // FileCache
// Alternative using a running Memcached service
// $cache = cache('memcached', 'storage_name', 'cache_id');

Alternatively, you can directly instantiate the cache classes:

use Luminova\Cache\FileCache;
use Luminova\Cache\MemoryCache;

$fileCache = new FileCache('storage_name', 'cache_folder_name');
$memoryCache = new MemoryCache('storage_name', 'cache_id');

Dynamic Initialization

If the storage name is not specified during initialization, you can set it later before performing any cache operations:

$cache->setStorage('cache_name');

Storing and Retrieving

Use the onExpired() method to retrieve content from the cache or execute a callback if the cache has expired or does not exist:

$content = $cache->onExpired('cache_key', static function() {
    return ['title' => 'Welcome'];
});

Getting Cached Content

Retrieve the cached content using the getItem() method:

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

Retrieving Multiple Entries

Use getItems() to fetch several cached entries in one call.

$items = $cache->getItems(['key1', 'key2']);
print_r($items);

Replacing Cache Content

Replace the content of an existing cache entry using the replace() method:

$data = 'New content to update';

if ($cache->replace('cache_key', $data)) {
    // Cache updated successfully
}

Saving Content

Save an entry with a lifetime of 1,200 seconds (20 minutes).

$data = 'Content to store';

if ($cache->setItem('cache_key', $data, expiration: 1200)) {
    // Cache saved successfully
}

You can also use predefined options for saving content:

$data = 'Content to store';
$cache->setExpire(1200);

if ($cache->set('cache_key', $data)) {
    // Cache saved successfully
}

Delete Cache Content

To delete a specific cache item.

if ($cache->deleteItem('cache_key')) {
    // Cache item deleted
}

Delete Multiple Items

To delete a specified cache keys.

if ($cache->deleteItems(['key1', 'key2'])) {
    // Cache items were deleted
}

Clear All Cache Content

To clear all items in the cache storage.

if ($cache->clear()) {
    // All cache items cleared
}

Flush All Cache Content

Use flush() only when the entire cache directory or server is dedicated to your test. Its scope can include entries from other storage names.

if ($cache->flush()) {
    // All cache items flushed
}

Check if Cache Exists

To check if a specific cache key exists.

if ($cache->hasItem('cache_key')) {
    // Cache item exists
}