Fresh code drop

Know whether a cache item was warm

PHP

Laravel 13 API example based on official Laravel documentation. Source: https://api.laravel.com/docs/13.x/Illuminate/Cache/Repository.html#method_rememberWithWarmth

PHP
CodeSnap // free canvas

Know whether a cache item was warm

<?php

use Illuminate\Support\Facades\Cache;

[$products, $warm] = Cache::rememberWithWarmth(
    'products:featured',
    now()->addHour(),
    fn () => Product::featured()->get(),
);

return response()->json($products)
    ->header('X-Cache', $warm ? 'HIT' : 'MISS');

// Further reading: https://api.laravel.com/docs/13.x/Illuminate/Cache/Repository.html#method_rememberWithWarmth

Turn your code into a post.Five templates are free—no account needed.

Remix this snap