PHP code example of kai-init / laravel-normcache

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

    

kai-init / laravel-normcache example snippets


use NormCache\Traits\Cacheable;

class Post extends Model
{
    use Cacheable;
}

Post::all();
Post::where('active', true)->get();
Post::find(1);
Post::paginate(20);

Post::withoutCache()->get();

Author::whereHas('posts', fn($q) => $q->where('published', true))
    ->dependsOn([Post::class])
    ->get();

// Works for any query shape — JOIN, GROUP BY, DISTINCT, subquery WHERE, raw ORDER BY:
Author::join('posts', 'posts.author_id', '=', 'authors.id')->dependsOn([Post::class])->get();
Post::select('author_id', DB::raw('SUM(views) as total'))
    ->groupBy('author_id')->dependsOn([Post::class])->get();

Post::query()->remember(600)->get();

Post::withCount('comments')->get();
Post::withoutAggregateCache()->withCount('comments')->get(); // skip aggregate cache

NormCache::flushModel(Post::class);
NormCache::flushAll();

DB::table('posts')->update(['published' => true]);
NormCache::flushModel(Post::class);

Author::whereHas('posts')->dependsOn([Post::class])->tag('homepage')->get();

NormCache::flushTag(Author::class, 'homepage');   // single model — single-slot scan
NormCache::flushTagAcrossModels('homepage');       // all models — cluster-wide scan

// config/normcache.php
return [
    'connection'     => env('NORMCACHE_CONNECTION', 'cache'),
    'enabled'        => env('NORMCACHE_ENABLED', true),
    'ttl'            => env('NORMCACHE_TTL', 604800),      // model keys: 7 days
    'query_ttl'      => env('NORMCACHE_QUERY_TTL', 3600),  // query/raw/pivot keys: 1 hour
    'key_prefix'     => env('NORMCACHE_PREFIX', ''),
    'cooldown'          => env('NORMCACHE_COOLDOWN', 0),           // version bump debounce in seconds
    'building_lock_ttl' => env('NORMCACHE_BUILDING_LOCK_TTL', 5),  // seconds before an abandoned cache build lock expires
    'stampede_wait_ms'  => env('NORMCACHE_STAMPEDE_WAIT_MS', 200), // ms to wait for a build in progress (Redis 6.0+ for sub-second)
    'cluster'           => env('NORMCACHE_CLUSTER', false),
    'events'            => env('NORMCACHE_EVENTS', true),
    'fallback'          => env('NORMCACHE_FALLBACK', false),
    'fire_retrieved'    => env('NORMCACHE_FIRE_RETRIEVED', false),
    'debugbar'          => env('NORMCACHE_DEBUGBAR', false),
];

'debugbar' => env('NORMCACHE_DEBUGBAR', false),
bash
php artisan normcache:flush --model="App\Models\Post"
php artisan normcache:flush