PHP code example of lithemod / cache

1. Go to this page and download the library: Download lithemod/cache library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lithemod / cache example snippets


use Lithe\Support\Cache;

// Define the cache directory
Cache::dir(__DIR__ . '/cache');

// Add data to the cache
Cache::add('my_data', ['foo' => 'bar'], 3600, 'serialize'); // Using serialize

// Retrieve data from the cache
$data = Cache::get('my_data');

if ($data === null) {
    echo "Data not found or expired.";
} else {
    print_r($data);
}

// Check if a single key exists
if (Cache::has('my_data')) {
    echo "Data is in cache.";
}

// Check multiple keys
if (Cache::has(['key1', 'key2'])) {
    echo "All keys are in cache.";
} else {
    echo "One or more keys were not found or are expired.";
}

// Invalidate a single cache key
Cache::invalidate('my_data');

// Invalidate multiple keys
Cache::invalidate(['key1', 'key2', 'key3']);

$data = Cache::remember('my_key', function () {
    // Logic to fetch data if not in cache
    return ['foo' => 'bar'];
}, 3600, 'serialize'); // Using serialize

print_r($data);