Laravel Model Caching

PHP MIT

High-performance Eloquent model caching for Laravel with automatic cache invalidation, relationship-aware caching, query caching, cache tags, and Redis support.

Stars
30
Forks
3
Downloads
2,347
Open Issues
0

Developer Guide

This guide shows how to use this package from a downloaded local copy before it is published to Packagist.

Local Path Installation

Assume the package is downloaded here:

/Users/ghostcompiler/Desktop/laravel-model-caching

In your Laravel application's composer.json, add a local path repository:

{
    "repositories": [
        {
            "type": "path",
            "url": "../laravel-model-caching",
            "options": {
                "symlink": true
            }
        }
    ]
}

Use the correct relative path from your Laravel app to this package. For example, if both projects are on the Desktop:

"url": "../laravel-model-caching"

Then require the package inside your Laravel app:

composer require ghostcompiler/laravel-model-caching:@dev

For Laravel 13 apps, make sure this package version includes Laravel 13 constraints:

"illuminate/cache": "^10.0|^11.0|^12.0|^13.0"

If Composer already has a cached package resolution, update it:

composer update ghostcompiler/laravel-model-caching

If the package was previously rejected by Composer, run this from the Laravel app after updating the downloaded package:

composer clear-cache
composer require ghostcompiler/laravel-model-caching:@dev -W

The -W flag allows Composer to resolve related dependencies when needed.

Publish Config

php artisan vendor:publish --tag=model-cache-config

This creates:

config/model-cache.php

Enable on Models

Add the trait only to models that should support caching:

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

class User extends Model
{
    use HasModelCaching;
}

Then use:

User::with('posts.comments')->remember()->get();
User::with('posts')->remember(300)->paginate(10);
User::where('active', true)->remember()->firstOrFail();
User::findCached(1);

remember() with no argument uses default_ttl from config/model-cache.php.

The trait does not cache by itself. Cached builder methods are get, first, firstOrFail, find, findOrFail, and pagination helpers.

To cache every read on trait-enabled models without chaining remember():

'auto_remember' => true,

Use dontCache() to skip caching for a specific chain.

For development:

'store' => null,
'auto_remember' => false,
'cache_tags' => false,
'debug' => true,

For tenant middleware that resolves on every request:

'auto_remember' => true,
'debug' => true,
'context_callbacks' => [
    'host' => fn () => request()->getHost(),
],

For production with Redis:

'store' => 'redis',
'cache_tags' => true,
'use_redis_sets' => true,

For multi-tenant apps, include tenant context:

'context_callbacks' => [
    'tenant' => fn () => tenant('id'),
],

Package Development Commands

From this package directory:

composer install
composer validate --strict
composer analyse
composer test

Run one syntax check manually:

find src config tests -name '*.php' -print0 | xargs -0 -n1 php -l

Testing in a Laravel App

After path installing, use the package exactly like a normal dependency:

php artisan config:clear
php artisan cache:clear
php artisan model-cache:warm "App\Models\User" --with=posts --ttl=600
php artisan model-cache:inspect "App\Models\Post" 5
php artisan model-cache:flush "App\Models\Post" 5

If Laravel does not auto-discover the provider, add it manually in config/app.php:

'providers' => [
    GhostCompiler\LaravelModelCaching\ModelCacheServiceProvider::class,
],

GitHub Actions

This repository includes a test workflow at:

.github/workflows/tests.yml

It runs:

  • Composer validation
  • PHP syntax checks
  • PHPUnit on Laravel 10, 11, 12, and 13

The workflow runs on pushes and pull requests to main and master.

Releasing Later

When the package is ready to publish:

  1. Push this repository to GitHub.
  2. Create a tagged release, for example v1.0.0.
  3. Submit the repository to Packagist.
  4. In Laravel apps, replace the path repository install with:
composer require ghostcompiler/laravel-model-caching