PHP code example of langsys / laravel-request-query-cache

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

    

langsys / laravel-request-query-cache example snippets


use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class PendingInvitation implements ValidationRule
{
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        $invitation = UserInvitation::where('activation_token', $value)
            ->whereNull('redeemed_at')
            ->firstCached();

        if (! $invitation) {
            $fail('This invitation is invalid or has already been used.');
        }
    }
}

public function store(Request $request)
{
    $request->validate([
        'token' => ['vitation = UserInvitation::where('activation_token', $request->token)
        ->whereNull('redeemed_at')
        ->firstCached();

    $invitation->redeem($request->user());

    return response()->json($invitation);
}

// Eloquent collection — caches ->get()
$locales = Locale::query()->getCached();

// Single model — caches ->first()
$invitation = UserInvitation::where('activation_token', $token)
    ->where('user_id', null)
    ->firstCached();

$a = User::where('id', 1)->firstCached(); // hits the DB
$b = User::where('id', 1)->firstCached(); // served from cache, no DB hit
// $a === $b

use App\Http\Controllers\PaymentController;

Route::post('/payments', [PaymentController::class, 'store'])
    ->middleware('idempotent');

  // in your API-key auth middleware, before the idempotent middleware runs
  $request->attributes->set('api_key_id', $apiKey->id);
  

// 24h window, header mandatory, scoped per authenticated user
Route::post('/payments', …)->middleware('idempotent:86400,true,user');

// 5-minute window, optional, global (one key space for everyone)
Route::post('/webhooks/stripe', …)->middleware('idempotent:300,false,global');

// 1h window, optional, per API-key tenant (SDK endpoints with no session user)
Route::post('/translate', …)->middleware('idempotent:3600,false,apikey');

// config/request-query-cache.php → 'idempotency'
'enabled'      => true,
'store'        => null,              // null = default cache store
'header'       => 'Idempotency-Key',
'ttl'          => 86400,            // seconds a response stays replayable (24h)
',
'replay_header'=> 'Idempotency-Replayed',  // null to disable