1212
1313![ Model Caching for Laravel masthead image] ( https://repository-images.githubusercontent.com/103836049/b0d89480-f1b1-11e9-8e13-a7055f008fe6 )
1414
15+ ## 🗂️ Table of Contents
16+ - [ 📖 Summary] ( #-summary )
17+ - [ 📦 Installation] ( #-installation )
18+ - [ 🚀 Getting Started] ( #-getting-started )
19+ - [ ⚙️ Configuration] ( #️-configuration )
20+ - [ 🤝 Contributing] ( #-contributing )
21+ - [ ⬆️ Upgrading] ( #️-upgrading )
22+ - [ 🔐 Security] ( #-security )
23+ - [ 📚 Further Reading] ( #-further-reading )
24+
1525## 📖 Summary
1626Automatic, self-invalidating Eloquent model and relationship caching. Add a
1727trait to your models and all query results are cached automatically — no manual
@@ -46,15 +56,15 @@ $posts = Post::where('active', true)->with('comments')->paginate();
4656```
4757
4858### ✅ What Gets Cached
49- - 🔍 Model queries (` get ` , ` first ` , ` find ` , ` all ` , ` paginate ` , ` pluck ` , ` value ` , ` exists ` )
50- - 🔢 Aggregations (` count ` , ` sum ` , ` avg ` , ` min ` , ` max ` )
51- - 🔗 Eager-loaded relationships (via ` with() ` )
59+ - Model queries (` get ` , ` first ` , ` find ` , ` all ` , ` paginate ` , ` pluck ` , ` value ` , ` exists ` )
60+ - Aggregations (` count ` , ` sum ` , ` avg ` , ` min ` , ` max ` )
61+ - Eager-loaded relationships (via ` with() ` )
5262
5363### 🚫 What Does Not Get Cached
54- - 🦥 Lazy-loaded relationships — only eager-loaded (` with() ` ) relationships are cached. Use ` with() ` to benefit from caching.
55- - 📋 Queries using ` select() ` clauses — custom column selections bypass the cache.
56- - 🔒 Queries inside transactions — cache is not automatically flushed when a transaction commits; call ` flushCache() ` manually if needed.
57- - 🎲 ` inRandomOrder() ` queries — caching is automatically disabled since results should differ each time.
64+ - Lazy-loaded relationships — only eager-loaded (` with() ` ) relationships are cached. Use ` with() ` to benefit from caching.
65+ - Queries using ` select() ` clauses — custom column selections bypass the cache.
66+ - Queries inside transactions — cache is not automatically flushed when a transaction commits; call ` flushCache() ` manually if needed.
67+ - ` inRandomOrder() ` queries — caching is automatically disabled since results should differ each time.
5868
5969### 💾 Cache Drivers
6070
@@ -63,24 +73,14 @@ $posts = Post::where('active', true)->with('comments')->paginate();
6373| Redis | ✅ (recommended) |
6474| Memcached | ✅ |
6575| APC | ✅ |
66- | Array | ✅ (for testing) |
76+ | Array | ❌ |
6777| File | ❌ |
6878| Database | ❌ |
6979| DynamoDB | ❌ |
7080
7181### 📋 Requirements
72- - 🐘 PHP 8.2+
73- - 🔺 Laravel 11, 12, or 13
74-
75- ## 🗂️ Table of Contents
76- - [ 📖 Summary] ( #-summary )
77- - [ 📦 Installation] ( #-installation )
78- - [ 🚀 Getting Started] ( #-getting-started )
79- - [ ⚙️ Configuration] ( #️-configuration )
80- - [ 🤝 Contributing] ( #-contributing )
81- - [ ⬆️ Upgrading] ( #️-upgrading )
82- - [ 🔐 Security] ( #-security )
83- - [ 📚 Further Reading] ( #-further-reading )
82+ - PHP 8.2+
83+ - Laravel 11, 12, or 13
8484
8585## 📦 Installation
8686```
@@ -129,9 +129,10 @@ class Post extends CachedModel
129129🎉 That's it — all Eloquent queries and eager-loaded relationships on these
130130models are now cached and automatically invalidated.
131131
132- > ** ⚠️ Note:** Avoid adding ` Cachable ` to the ` User ` model. It extends
133- > ` Illuminate\Foundation\Auth\User ` , and overriding that can break
134- > authentication. User data should generally be fresh anyway.
132+ > ** ⚠️ Note:** You can cache the ` User ` model — the ` Cachable ` trait does not
133+ > conflict with Laravel's authentication. Just avoid using cache cool-down
134+ > periods on it, and ensure user updates always go through Eloquent (not raw
135+ > ` DB::table() ` queries) so cache invalidation fires correctly.
135136
136137### 🌍 Real-World Example
137138Consider a blog with posts, comments, and tags:
@@ -230,17 +231,17 @@ across connections without any extra configuration.
230231### 🚫 Disabling Cache
231232There are three ways to bypass caching:
232233
233- ** 1️⃣ Per-query** (only affects this query chain, not subsequent queries):
234+ ** 1. Per-query** (only affects this query chain, not subsequent queries):
234235``` php
235236$results = MyModel::disableCache()->where('active', true)->get();
236237```
237238
238- ** 2️⃣ Globally via environment:**
239+ ** 2. Globally via environment:**
239240```
240241MODEL_CACHE_ENABLED=false
241242```
242243
243- ** 3️⃣ For a block of code:**
244+ ** 3. For a block of code:**
244245``` php
245246$result = app('model-cache')->runDisabled(function () {
246247 return MyModel::get();
@@ -262,7 +263,7 @@ In high-traffic scenarios (e.g. frequent comment submissions) you may want to
262263prevent every write from immediately flushing the cache. Cool-down requires two
263264steps:
264265
265- ** 1️⃣ Declare the default duration** on the model (this alone does nothing — it
266+ ** Declare the default duration** on the model (this alone does nothing — it
266267just sets the value):
267268
268269``` php
@@ -281,7 +282,7 @@ class Comment extends Model
281282}
282283```
283284
284- ** 2️⃣ Activate the cool-down** by calling ` withCacheCooldownSeconds() ` in your
285+ ** Activate the cool-down** by calling ` withCacheCooldownSeconds() ` in your
285286query. This writes the cool-down window into the cache store:
286287
287288``` php
@@ -310,14 +311,14 @@ Cache is automatically flushed when:
310311
311312| Trigger | Behavior |
312313| ---------| ----------|
313- | ➕ Model created | Flush model cache |
314- | ✏️ Model updated/saved | Flush model cache |
315- | 🗑️ Model deleted | Flush only if rows were actually deleted |
316- | 💥 Model force-deleted | Flush only if rows were actually deleted |
317- | 🔗 Pivot ` attach ` / ` detach ` / ` sync ` / ` updateExistingPivot ` | Flush relationship cache |
318- | 🔢 ` increment ` / ` decrement ` | Flush model cache |
319- | 📝 ` insert ` / ` update ` (builder) | Flush model cache |
320- | 🧨 ` truncate ` | Flush model cache |
314+ | Model created | Flush model cache |
315+ | Model updated/saved | Flush model cache |
316+ | Model deleted | Flush only if rows were actually deleted |
317+ | Model force-deleted | Flush only if rows were actually deleted |
318+ | Pivot ` attach ` / ` detach ` / ` sync ` / ` updateExistingPivot ` | Flush relationship cache |
319+ | ` increment ` / ` decrement ` | Flush model cache |
320+ | ` insert ` / ` update ` (builder) | Flush model cache |
321+ | ` truncate ` | Flush model cache |
321322
322323Cache tags are generated for the primary model, each eager-loaded relationship,
323324joined tables, and morph-to target types, so only the relevant entries are
@@ -330,12 +331,12 @@ related model uses the `Cachable` trait.
330331
331332### 🧹 Manual Cache Flushing
332333
333- ** 🖥️ Artisan command — single model:**
334+ ** Artisan command — single model:**
334335``` sh
335336php artisan modelCache:clear --model=' App\Models\Post'
336337```
337338
338- ** 🖥️ Artisan command — all models:**
339+ ** Artisan command — all models:**
339340``` sh
340341php artisan modelCache:clear
341342```
0 commit comments