You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To speed up the scaffolding of invalidation within your app, you can specify the model to auto-flush the cache upon any model gets created, updated or deleted.
156
+
157
+
```php
158
+
class Page extends Model
159
+
{
160
+
use QueryCacheable;
161
+
162
+
/**
163
+
* Invalidate the cache automatically
164
+
* upon update in the database.
165
+
*/
166
+
protected static $flushCacheOnUpdate = true;
167
+
}
168
+
```
169
+
170
+
When you set up the `$flushCacheOnUpdate` variable, the package attaches an observer to your model, and any `created`, `updated`, `deleted`, `forceDeleted` or `restored` event will trigger the cache invalidation.
171
+
172
+
> In order for auto-flush to work, you will need at least one **base tag**. Out-of-the-box, the model has a base tag set. In some cases, if you have overwritten the `getCacheBaseTags()` with an empty array, it might not work.
173
+
174
+
## Partial Automatic Invalidation
175
+
176
+
In some cases, you might not want to invalidate the whole cache of a specific model. Perhaps you got two queries that run individually and want to invalidate the cache only for one of them.
177
+
178
+
To do this, overwrite your `getCacheTagsToInvalidateOnUpdate()` method in your model:
179
+
180
+
```php
181
+
class Page extends Model
182
+
{
183
+
use QueryCacheable;
184
+
185
+
/**
186
+
* Invalidate the cache automatically
187
+
* upon update in the database.
188
+
*/
189
+
protected static $flushCacheOnUpdate = true;
190
+
191
+
/**
192
+
* When invalidating automatically on update, you can specify
193
+
* which tags to invalidate.
194
+
*
195
+
* @return array
196
+
*/
197
+
public function getCacheTagsToInvalidateOnUpdate(): array
198
+
{
199
+
return [
200
+
'query1',
201
+
];
202
+
}
203
+
}
204
+
205
+
$query1 = Page::cacheFor(60)
206
+
->cacheTags(['query1'])
207
+
->get();
208
+
209
+
$query2 = Page::cacheFor(60)
210
+
->cacheTags(['query2'])
211
+
->get();
212
+
213
+
// The $query1 gets invalidated
214
+
// but $query2 will still hit from cache if re-called.
215
+
216
+
$page = Page::first();
217
+
218
+
$page->update([
219
+
'name' => 'Reddit',
220
+
]);
221
+
```
222
+
223
+
**Please keep in mind: Setting `$flushCacheOnUpdate` to `true` and not specifying individual tags to invalidate will lead to [Full Automatic Invalidation](#full-automatic-invalidation) since the default tags to invalidate are the base tags and you need at least one tag to invalidate.**
224
+
225
+
**Not specifying a tag to invalidate fallbacks to the set of base tags, thus leading to Full Automatic Invalidation.**
226
+
153
227
## Cache Keys
154
228
155
229
The package automatically generate the keys needed to store the data in the cache store. However, prefixing them might be useful if the cache store is used by other applications and/or models and you want to manage the keys better to avoid collisions.
0 commit comments