PHP code example of zidbih / laravel-auto-cache-clear

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

    

zidbih / laravel-auto-cache-clear example snippets


namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Zidbih\LaravelAutoCacheClear\Attributes\ClearCacheWhen;
use Zidbih\LaravelAutoCacheClear\Traits\AutoClearsCache;

#[ClearCacheWhen(['created', 'updated', 'deleted'], cacheKey: 'user:{id}:details', staticKeys: ['users'])]
class User extends Model
{
    use AutoClearsCache;
}

use Zidbih\LaravelAutoCacheClear\Traits\AutoClearsCache;

class User extends Model
{
    use AutoClearsCache;
}

use Illuminate\Support\Facades\Cache;

// Cache a list of users
$users = Cache::remember('users', 60, function () {
    return User::all();
});

// Cache a single user
$user = Cache::remember('user:1:details', 60, function () {
    return User::find(1);
});

#[ClearCacheWhen(['created', 'updated', 'deleted'], staticKeys: ['users'])]
class User extends Model
{
    use AutoClearsCache;
}

$users = Cache::remember('users', 60*60*24, fn () => User::all());