PHP code example of infocyph / cachelayer

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

    

infocyph / cachelayer example snippets


use Infocyph\CacheLayer\Cache\Cache;

$cache = Cache::pdo('app'); // defaults to sqlite file under sys temp cachelayer/pdo

$cache->setTagged('user:1', ['name' => 'Ada'], ['users'], 300);

$user = $cache->remember('user:1', function ($item) {
    $item->expiresAfter(300);
    return ['name' => 'Ada'];
}, tags: ['users']);

$cache->invalidateTag('users');

$metrics = $cache->exportMetrics();

use Infocyph\CacheLayer\Cache\Cache;

$cache = Cache::tiered([
    ['driver' => 'apcu', 'namespace' => 'app'], // L1
    ['driver' => 'valkey', 'namespace' => 'app', 'dsn' => 'valkey://127.0.0.1:6379'], // L2
], writeToL1: false); // optional L1 write-through

$value = $cache->remember('user:42', function ($item) use ($pdo) {
    $item->expiresAfter(300);

    $stmt = $pdo->prepare('SELECT payload FROM users_cache_source WHERE id = ?');
    $stmt->execute([42]);

    return $stmt->fetchColumn();
});

$cache
    ->configurePayloadSecurity(
        integrityKey: 'replace-with-strong-secret',
        maxPayloadBytes: 8_388_608,
    )
    ->configureSerializationSecurity(
        allowClosurePayloads: false,
        allowObjectPayloads: false,
    );