mikebronner / laravel-model-caching
Automatic caching for Eloquent models.
Package info
github.com/mikebronner/laravel-model-caching
pkg:composer/mikebronner/laravel-model-caching
Requires
- php: >=8.2
- illuminate/cache: ^11.0|^12.0|^13.0
- illuminate/config: ^11.0|^12.0|^13.0
- illuminate/console: ^11.0|^12.0|^13.0
- illuminate/container: ^11.0|^12.0|^13.0
- illuminate/database: ^11.0|^12.0|^13.0
- illuminate/http: ^11.0|^12.0|^13.0
- illuminate/support: ^11.0|^12.0|^13.0
- mikebronner/laravel-pivot-events: *
Requires (Dev)
- doctrine/dbal: ^3.3|^4.2
- fakerphp/faker: ^1.11
- larastan/larastan: ^2.0|^3.0
- laravel/pint: ^1.27
- orchestra/testbench: ^9.0|^10.0|^11.0
- phpunit/phpunit: ^10.5|^11.5.3|^12.5.12
- slevomat/coding-standard: ^7.0|^8.14
- squizlabs/php_codesniffer: ^3.6|^4.0
- symfony/thanks: ^1.2
- dev-master
- 13.0.2
- 13.0.1
- 13.0.0
- 12.1.0-RC3
- 12.1.0-RC2
- 12.1.0-RC1
- 12.0.4
- 12.0.3
- 12.0.2
- 12.0.1
- 12.0.0
- 11.0.1
- 11.0.0
- 0.13.9
- 0.13.8
- 0.13.7
- 0.13.6
- 0.13.5
- 0.13.4
- 0.13.3
- 0.13.2
- 0.13.1
- 0.13.0
- 0.12.5
- 0.12.4
- 0.12.3
- 0.12.2
- 0.12.1
- 0.12.0
- 0.11.7
- 0.11.6
- 0.11.5
- 0.11.4
- 0.11.3
- 0.11.2
- 0.11.1
- 0.11.0
- 0.10.2
- 0.10.1
- 0.10.0
- 0.9.0
- 0.8.10
- 0.8.9
- 0.8.8
- 0.8.7
- 0.8.6
- 0.8.5
- 0.8.4
- 0.8.3
- 0.8.2
- 0.8.1
- 0.8.0
- 0.7.4
- 0.7.3
- 0.7.2
- 0.7.1
- 0.7.0
- 0.6.3
- 0.6.2
- 0.6.1
- 0.6.0
- 0.5.6
- 0.5.5
- 0.5.4
- 0.5.3
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.24
- 0.4.23
- 0.4.22
- 0.4.21
- 0.4.20
- 0.4.19
- 0.4.18
- 0.4.17
- 0.4.16
- 0.4.15
- 0.4.14
- 0.4.13
- 0.4.12
- 0.4.11
- 0.4.10
- 0.4.9
- 0.4.8
- 0.4.7
- 0.4.6
- 0.4.5
- 0.4.4
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.7
- 0.3.6
- 0.3.5
- 0.3.4
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.64
- 0.2.63
- 0.2.62
- 0.2.61
- 0.2.60
- 0.2.59
- 0.2.58
- 0.2.57
- 0.2.56
- 0.2.55
- 0.2.54
- 0.2.53
- 0.2.52
- 0.2.51
- 0.2.50
- 0.2.49
- 0.2.48
- 0.2.47
- 0.2.46
- 0.2.45
- 0.2.44
- 0.2.43
- 0.2.42
- 0.2.41
- 0.2.40
- 0.2.39
- 0.2.38
- 0.2.37
- 0.2.36
- 0.2.35
- 0.2.34
- 0.2.33
- 0.2.32
- 0.2.31
- 0.2.30
- 0.2.29
- 0.2.28
- 0.2.27
- 0.2.26
- 0.2.25
- 0.2.24
- 0.2.23
- 0.2.22
- 0.2.21
- 0.2.20
- 0.2.19
- 0.2.18
- 0.2.17
- 0.2.16
- 0.2.15
- 0.2.14
- 0.2.13
- 0.2.12
- 0.2.11
- 0.2.10
- 0.2.9
- 0.2.8
- 0.2.7
- 0.2.6
- 0.2.5
- 0.2.4
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.0
- dev-develop
- dev-feat/545-cache-invalidation-joined-tables
- dev-feat/538-cache-invalidation-morphtomany-hasma
- dev-feat/533-cache-connection-fallback
- dev-feat/539-eager-loaded-morphto-resolves-wrong-type
- dev-feat/552-implement-caching-as-eloquent-builder-extension
- dev-feat/550-only-flush-cache-on-actual-delete
- dev-fix/526-custom-builder-support
- dev-feat/549-programmatic-cache-invalidation-facade
- dev-feat/544-cache-invalidation-morphto-morphedbymany
- dev-feat/541-cursor-pagination
- dev-feat/548-pivot-observer-caching
- dev-feat/546-missing-use-import
- dev-feat/542-lock-for-update-bypass-cache
- dev-feat/535-trait-collision
- dev-feat/536-belongsToMany-sync-memory
- dev-feat/543-paginator-base-url
- dev-feat/534-cachable-fillable-fix
- dev-feat/547-fix-cachkey-uuid-validation
- dev-feat/551-cache-pivot-invalidation
- dev-feat/537-livewire-pagination-caching
- dev-feat/556-add-php-8-5-support
- dev-feat/540-retrieved-observer-cache-hit
- dev-feat/php-8-5-support
- dev-feat/553-test-reorder
- dev-dependabot/composer/slevomat/coding-standard-tw-7.0
- dev-laravel-9
- dev-dependabot/add-v2-config-file
- dev-feature/implement-query-builder-override-fix
- dev-feature/allow-flushing-by-database
- dev-feature/add-nova-tests
- dev-feature/update-to-laravel-6
- dev-feature/performance-improvements
- dev-laravel-5.5
- dev-laravel-5.6
This package is auto-updated.
Last update: 2026-03-02 15:52:50 UTC
README
ποΈ Table of Contents
- π Summary
- π¦ Installation
- π Getting Started
- βοΈ Configuration
- π€ Contributing
- β¬οΈ Upgrading
- π Security
- π Further Reading
π Summary
Automatic, self-invalidating Eloquent model and relationship caching. Add a trait to your models and all query results are cached automatically β no manual cache keys, no forgetting to invalidate. When a model is created, updated, or deleted the relevant cache entries are flushed for you.
β‘ Typical performance improvements range from 100β900% reduction in database queries on read-heavy pages. π§ͺ Backed by 335+ integration tests across PHP 8.2β8.5 and Laravel 11β13.
Use this package when your application makes many repeated Eloquent queries and you want a drop-in caching layer that stays in sync with your data without any manual bookkeeping.
π Before & After
β Without this package β manual cache keys, manual invalidation:
$posts = Cache::remember('posts:active:page:1', 3600, function () { return Post::where('active', true)->with('comments')->paginate(); }); // And in every observer or event listenerβ¦ Cache::forget('posts:active:page:1'); // Hope you remembered every key variant! π
β With this package β add the trait, query normally:
// Just query. Caching and invalidation happen automatically. β¨ $posts = Post::where('active', true)->with('comments')->paginate();
β What Gets Cached
- Model queries (
get,first,find,all,paginate,pluck,value,exists) - Aggregations (
count,sum,avg,min,max) - Eager-loaded relationships (via
with())
π« What Does Not Get Cached
- Lazy-loaded relationships β only eager-loaded (
with()) relationships are cached. Usewith()to benefit from caching. - Queries using
select()clauses β custom column selections bypass the cache. - Queries inside transactions β cache is not automatically flushed when a transaction commits; call
flushCache()manually if needed. inRandomOrder()queries β caching is automatically disabled since results should differ each time.
πΎ Cache Drivers
| Driver | Supported |
|---|---|
| Redis | β (recommended) |
| Memcached | β |
| APC | β |
| Array | β |
| File | β |
| Database | β |
| DynamoDB | β |
π Requirements
- PHP 8.2+
- Laravel 11, 12, or 13
π¦ Installation
composer require genealabs/laravel-model-caching
β¨ The service provider is auto-discovered. No additional setup is required.
π Getting Started
Add the Cachable trait to your models. The recommended approach is a base
model that all other models extend:
<?php namespace App\Models; use GeneaLabs\LaravelModelCaching\Traits\Cachable; use Illuminate\Database\Eloquent\Model; abstract class BaseModel extends Model { use Cachable; }
Alternatively, extend the included CachedModel directly:
<?php namespace App\Models; use GeneaLabs\LaravelModelCaching\CachedModel; class Post extends CachedModel { // ... }
π That's it β all Eloquent queries and eager-loaded relationships on these models are now cached and automatically invalidated.
β οΈ Note: You can cache the
Usermodel β theCachabletrait does not conflict with Laravel's authentication. Just avoid using cache cool-down periods on it, and ensure user updates always go through Eloquent (not rawDB::table()queries) so cache invalidation fires correctly.
π Real-World Example
Consider a blog with posts, comments, and tags:
class Post extends BaseModel { public function comments() { return $this->hasMany(Comment::class); } public function tags() { return $this->belongsToMany(Tag::class); } } // All cached automatically β the query, the eager loads, everything. πͺ $posts = Post::with('comments', 'tags') ->where('published', true) ->latest() ->paginate(15);
When a new comment is created, the cache for Post and Comment queries is
automatically invalidated β no manual Cache::forget() calls needed. π§Ή
βοΈ Configuration
Publish the config file:
php artisan modelCache:publish --config
This creates config/laravel-model-caching.php:
return [ 'cache-prefix' => '', 'enabled' => env('MODEL_CACHE_ENABLED', true), 'use-database-keying' => env('MODEL_CACHE_USE_DATABASE_KEYING', true), 'store' => env('MODEL_CACHE_STORE'), 'fallback-to-database' => env('MODEL_CACHE_FALLBACK_TO_DB', false), ];
π§ Environment Variables
| Variable | Default | Description |
|---|---|---|
MODEL_CACHE_ENABLED |
true |
β Enable or disable caching globally. |
MODEL_CACHE_STORE |
null |
πΎ Cache store name from config/cache.php. Uses the default store when not set. |
MODEL_CACHE_USE_DATABASE_KEYING |
true |
π Include database connection and name in cache keys. Important for multi-tenant or multi-database apps. |
MODEL_CACHE_FALLBACK_TO_DB |
false |
π‘οΈ When true, falls back to direct database queries if the cache backend is unavailable (e.g. Redis is down) instead of throwing an exception. |
π Note: The
cache-prefixoption is set directly in the config file (not via an environment variable). For dynamic prefixes (e.g. multi-tenant), use the per-model$cachePrefixproperty shown below.
πΎ Custom Cache Store
To use a dedicated cache store for model caching, define one in
config/cache.php and reference it:
MODEL_CACHE_STORE=model-cache
π·οΈ Cache Key Prefix
For multi-tenant applications you can isolate cache entries per tenant. Set the prefix globally in config:
'cache-prefix' => 'tenant-123',
Or per-model via a property:
<?php namespace App\Models; use GeneaLabs\LaravelModelCaching\Traits\Cachable; use Illuminate\Database\Eloquent\Model; class Post extends Model { use Cachable; protected $cachePrefix = 'tenant-123'; }
π Multiple Database Connections
When use-database-keying is enabled (the default), cache keys automatically
include the database connection and name. This keeps cache entries separate
across connections without any extra configuration.
π« Disabling Cache
There are three ways to bypass caching:
1. Per-query (only affects this query chain, not subsequent queries):
$results = MyModel::disableCache()->where('active', true)->get();
2. Globally via environment:
MODEL_CACHE_ENABLED=false
3. For a block of code:
$result = app('model-cache')->runDisabled(function () { return MyModel::get(); }); // or via the Facade use GeneaLabs\LaravelModelCaching\Facades\ModelCache; ModelCache::runDisabled(function () { return MyModel::get(); });
π‘ Tip: Use option 1 in seeders to avoid pulling stale cached data during reseeds.
βοΈ Cache Cool-Down Period
In high-traffic scenarios (e.g. frequent comment submissions) you may want to prevent every write from immediately flushing the cache. Cool-down requires two steps:
Declare the default duration on the model (this alone does nothing β it just sets the value):
<?php namespace App\Models; use GeneaLabs\LaravelModelCaching\Traits\Cachable; use Illuminate\Database\Eloquent\Model; class Comment extends Model { use Cachable; protected $cacheCooldownSeconds = 300; // 5 minutes β±οΈ }
Activate the cool-down by calling withCacheCooldownSeconds() in your
query. This writes the cool-down window into the cache store:
// Activate using the model's default (300 seconds) Comment::withCacheCooldownSeconds()->get(); // Or override with a specific duration Comment::withCacheCooldownSeconds(30)->get();
Once activated, writes during the cool-down window will not flush the cache. After the window expires, the next write triggers a flush and re-warms the cache. π
π‘οΈ Graceful Fallback
When enabled, if the cache backend (e.g. Redis) is unavailable the package logs a warning and falls back to querying the database directly β your application continues to function without caching rather than throwing an exception.
MODEL_CACHE_FALLBACK_TO_DB=true
π§Ή Cache Invalidation
Cache is automatically flushed when:
| Trigger | Behavior |
|---|---|
| Model created | Flush model cache |
| Model updated/saved | Flush model cache |
| Model deleted | Flush only if rows were actually deleted |
| Model force-deleted | Flush only if rows were actually deleted |
Pivot attach / detach / sync / updateExistingPivot |
Flush relationship cache |
increment / decrement |
Flush model cache |
insert / update (builder) |
Flush model cache |
truncate |
Flush model cache |
Cache tags are generated for the primary model, each eager-loaded relationship, joined tables, and morph-to target types, so only the relevant entries are invalidated. π―
π BelongsToMany with Custom Pivot Models
Cache invalidation works for BelongsToMany relationships using custom pivot
models (->using(CustomPivot::class)) as long as either the parent or the
related model uses the Cachable trait.
π§Ή Manual Cache Flushing
Artisan command β single model:
php artisan modelCache:clear --model='App\Models\Post'
Artisan command β all models:
php artisan modelCache:clear
π§ Programmatic via Facade:
use GeneaLabs\LaravelModelCaching\Facades\ModelCache; // Single model ModelCache::invalidate(App\Models\Post::class); // Multiple models ModelCache::invalidate([ App\Models\Post::class, App\Models\Comment::class, ]);
β° Cache Expiration (TTL)
Cached queries are stored indefinitely (rememberForever) and rely on automatic
invalidation (see above) to stay fresh. There is no per-query TTL option. If you
need time-based expiry, use the cool-down period feature or flush the cache on a
schedule via the Artisan command.
π§ͺ Testing
In your test suite you can either disable model caching entirely or use the
array cache driver:
π« Disable caching in tests:
// In your TestCase setUp() or phpunit.xml config(['laravel-model-caching.enabled' => false]);
β Use the array driver (useful for testing cache behavior itself):
config(['cache.stores.model-test' => ['driver' => 'array']]); config(['laravel-model-caching.store' => 'model-test']);
π· Queue Workers
The package has no special queue or Horizon integration. Cached queries inside queued jobs work the same as in HTTP requests. Cache invalidation triggered in a web request is immediately visible to queue workers (assuming a shared cache store like Redis). No additional configuration is needed.
π€ Contributing
Contributions are welcome! π Please review the Contribution Guidelines and observe the Code of Conduct before submitting a pull request.
β¬οΈ Upgrading
For breaking changes and upgrade instructions between versions, see the Releases page on GitHub.
π Security
Please review the Security Policy for information on supported versions and how to report vulnerabilities.
π Further Reading
The test suite serves as living documentation β browse it for detailed examples of every supported query type, relationship pattern, and edge case. π
Built with β€οΈ for the Laravel community using lots of βοΈ by Mike Bronner.
This is an MIT-licensed open-source project. Its continued development is made possible by the community. If you find it useful, please consider π becoming a sponsor and βing it on GitHub.
π Thank you to all contributors who have helped make this package better!