PHP code example of jardisadapter / cache

1. Go to this page and download the library: Download jardisadapter/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/ */

    

jardisadapter / cache example snippets


use JardisAdapter\Cache\Cache;
use JardisAdapter\Cache\Adapter\CacheMemory;
use JardisAdapter\Cache\Adapter\CacheRedis;

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

// Two-layer cache: L1 = in-process memory, L2 = Redis
$cache = new Cache([
    new CacheMemory('myapp'),
    new CacheRedis($redis, 'myapp'),
]);

$cache->set('user:42', $userData, ttl: 300);
$user = $cache->get('user:42');

use JardisAdapter\Cache\Cache;
use JardisAdapter\Cache\Adapter\CacheMemory;
use JardisAdapter\Cache\Adapter\CacheApcu;
use JardisAdapter\Cache\Adapter\CacheRedis;
use JardisAdapter\Cache\Adapter\CacheDatabase;

// Four-layer cascade: L1 memory → L2 APCu → L3 Redis → L4 database
// A miss at L1 checks L2, then L3, then L4.
// When found, the value is written back into all faster layers automatically.
$cache = new Cache([
    new CacheMemory('orders'),
    new CacheApcu('orders'),
    new CacheRedis($redis, 'orders'),
    new CacheDatabase($pdo, namespace: 'orders'),
]);

// Bulk operations — all layers updated in one call
$cache->setMultiple([
    'order:101' => $order101,
    'order:102' => $order102,
], ttl: 600);

$orders = $cache->getMultiple(['order:101', 'order:102']);

// Invalidate a key across all layers
$cache->delete('order:101');

// Expire stale database entries explicitly
$dbLayer = new CacheDatabase($pdo, namespace: 'orders');
$dbLayer->cleanExpired();