PHP code example of ghostcompiler / laravel-model-caching

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

    

ghostcompiler / laravel-model-caching example snippets


use GhostCompiler\LaravelModelCaching\Concerns\HasModelCaching;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use HasModelCaching;
}

$users = User::with('posts.comments')
    ->where('active', true)
    ->remember(60)
    ->get();

$tenant = User::whereHas('brand.domains', fn ($q) => $q->where('domain', $host))
    ->with('brand')
    ->remember(300)
    ->firstOrFail();

// config/model-cache.php
'auto_remember' => true,

$user = User::findCached(1);

$user->loadCached('posts.comments');

$users = User::remember(600)
    ->dontCache()
    ->where('active', true)
    ->get();

User::with('posts')->remember(60)->get();
User::with('posts.comments')->remember(60)->get();
User::with(['posts', 'roles'])->remember(60)->get();

'morph_map_version' => 2,

$users = User::with('posts')->remember(300)->paginate(10);

return [
    'enabled' => true,
    'default_ttl' => 3600,
    'auto_remember' => false,
    'store' => null,
    'prefix' => 'model-cache',
    'cache_tags' => true,
    'dependency_ttl' => 604800,
    'use_redis_sets' => true,
    'auto_observe_models' => true,
    '

'context_callbacks' => [
    'tenant' => fn () => tenant('id'),
],
bash
php artisan vendor:publish --tag=model-cache-config
bash
php artisan model-cache:inspect "App\Models\Post" 5
bash
php artisan model-cache:flush "App\Models\Post" 5
bash
php artisan model-cache:flush --known