PHP code example of angelleger / laravel-response-cache

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

    

angelleger / laravel-response-cache example snippets


return [
    'ttl' => 300, // default lifetime in seconds
    'store' => env('RESPONSE_CACHE_STORE'), // cache store to use
    'key_prefix' => 'resp_cache:', // prefix for all keys and indexes
    'guest_only' => true, // only cache guests unless auth=true is passed to middleware
    'vary_on_headers' => ['Accept', 'Accept-Language'], // headers ponses larger than this (in kilobytes)
    'etag' => true, // automatically add ETag header to cached responses
    'lock_seconds' => 0, // hold an atomic lock for this many seconds while generating
    'lock_wait' => 10, // seconds to wait for a lock to be released
    'debug' => env('RESPONSE_CACHE_DEBUG', false), // enable debug helpers
];

Route::get('/posts', fn () => Post::all())
    ->middleware('resp.cache:ttl=120');

// Allow authenticated users to be cached
Route::get('/dashboard', fn () => view('dashboard'))
    ->middleware('auth', 'resp.cache:ttl=60,auth=true');

use AngelLeger\ResponseCache\Facades\ResponseCache;

$key = ResponseCache::makeKey(request());
$response = ResponseCache::rememberResponse(request(), fn () => response('ok'), 60);
ResponseCache::forgetByKey($key);
ResponseCache::forgetRoute('posts.index');
bash
php artisan response-cache:clear --key="get:posts::en:guest"
php artisan response-cache:clear --route=posts.index
php artisan response-cache:stats