PHP code example of jarir-ahmed / cache

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

    

jarir-ahmed / cache example snippets


use JarirAhmed\Cache\Cache;
use JarirAhmed\Cache\Store\ArrayStore;

$cache = Cache::repository(new ArrayStore);

$cache->set('key', 'value', 60);                       // ttl seconds (null = forever)
$cache->get('key', 'default');
$cache->remember('user:1', 300, fn () => loadUser(1)); // compute once, then cache
$cache->increment('hits');
$cache->tags(['users'])->flush();

$cache = Cache::create([
    'default'    => 'redis',
    'prefix'     => 'app:',
    'serializer' => 'php',            // php | json | igbinary
    'stores' => [
        'redis'     => ['driver' => 'redis', 'host' => '127.0.0.1', 'port' => 6379],
        'memcached' => ['driver' => 'memcached', 'host' => '127.0.0.1', 'port' => 11211],
        'files'     => ['driver' => 'file', 'path' => '/var/cache/app'],
        'db'        => ['driver' => 'pdo', 'pdo' => $pdo, 'table' => 'cache'],
        'fast'      => ['driver' => 'tiered', 'layers' => ['array', 'redis']],
    ],
]);

$cache->store('redis')->set('k', $value, 3600);
$cache->store('fast')->remember('hot', 60, fn () => compute());

use JarirAhmed\Cache\Store\RedisStore;

$redis = Cache::repository(RedisStore::connect('127.0.0.1', 6379)); // uses ext-redis if loaded, else raw socket

$psr16 = Cache::psr16(new RedisStore(...));   // Psr\SimpleCache\CacheInterface
$psr16->set('k', 'v', 3600);

$pool  = Cache::psr6(new FileStore('/tmp'));   // Psr\Cache\CacheItemPoolInterface
$item  = $pool->getItem('k');
if (! $item->isHit()) { $item->set(compute())->expiresAfter(600); $pool->save($item); }

$cache->tags(['users', 'profiles'])->put('u:1', $data, 3600);
$cache->tags(['users'])->flush();              // invalidate every 'users' entry at once

// distributed lock
$cache->lock('import', 30)->get(function () {
    runImportOnce();                           // only one process at a time
});

// single-flight: under load, ONE caller computes, the rest wait for the result
$report = $cache->rememberLocked('daily-report', 600, fn () => buildExpensiveReport());

use JarirAhmed\Cache\Http\HttpCache;
use JarirAhmed\Cache\Http\CacheControl;

echo HttpCache::serve($body, [
    'etag'          => true,
    'cache_control' => CacheControl::make()->public()->maxAge(300)->staleWhileRevalidate(60),
]);   // emits ETag + Cache-Control, replies 304 automatically on If-None-Match

use JarirAhmed\Cache\Http\PageCache;

$page = new PageCache($cache->store(), ttl: 120);

if (($html = $page->start('home')) !== null) { echo $html; return; } // cache hit
// ... render your page ...
echo $page->end('home');                                             // capture + store

use JarirAhmed\Cache\Http\CacheMiddleware;

$app->add(new CacheMiddleware($cache->store(), $psr17ResponseFactory, ttl: 60));