PHP code example of philiprehberger / laravel-cache-toolkit

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

    

philiprehberger / laravel-cache-toolkit example snippets


use PhilipRehberger\CacheToolkit\CacheKeyBuilder;

CacheKeyBuilder::make('client', '123', 'stats');
// "client:123:stats"

CacheKeyBuilder::forModel($client, 'details');
// "client:42:details"

CacheKeyBuilder::forList('clients', ['active' => 1]);
// "clients:list:<md5-hash>"

CacheKeyBuilder::forPaginated('clients', page: 2, perPage: 15);
// "clients:page:2:15:all"

CacheKeyBuilder::forAnalytics('revenue', '2026-01-01', '2026-03-31');
// "analytics:revenue:2026-01-01:2026-03-31"

CacheKeyBuilder::forUser(userId: 5, type: 'dashboard');
// "user:5:dashboard"

use PhilipRehberger\CacheToolkit\Facades\CacheTag;

$value = CacheTag::remember(
    ttlKey:   'dashboard_stats',
    callback: fn () => expensiveQuery(),
    tags:     ['clients'],
    'client', '42', 'stats'
);

CacheTag::put($key, $data, ttl: 300, tags: ['clients']);
CacheTag::get($key, tags: ['clients']);
CacheTag::forget($key, tags: ['clients']);
CacheTag::flush(['clients']);
CacheTag::flushType('clients');

// config/cache-toolkit.php
return [
    'prefixes' => [],
    'ttl' => [
        'default' => 300,
        'short'   => 60,
        'medium'  => 900,
        'long'    => 3600,
        'daily'   => 86400,
    ],
    'tags' => [],
];
bash
php artisan vendor:publish --tag=cache-toolkit-config