Last active
October 25, 2022 11:33
-
-
Save BrekiTomasson/70315b2492a5aaf5a74c5509d6381596 to your computer and use it in GitHub Desktop.
Laravel Trait to use in your Model classes to offer a $this->taggedCache() shortcut to your Cache
Note that you can also define the property $cacheTagIdentifier
in your model to ensure that the cache tags it generates are distinct enough for that model. If $cacheTagIdentifier
is not defined, it uses the name of the database table for the given model by default, using Str::singular()
to ensure that your users
table can result in a user:4
tag.
This has now been much improved and released as a package which can be found here: https://packagist.org/packages/brekitomasson/laravel-tagged-cache
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you include this file in your project and
use HasCacheTagsById;
you will be able to do things like:The
taggedCache()
method will ensure that yourdisplayname
cache key is tagged withusers
anduser:4
(assuming the user in question has user ID 4), so that there is no risk of conflict or overlap. You can then use your Event Listeners in order to$user->taggedCache()->flush()
whenever a user logs in or whenever specific settings are changed. To flush caches for all users, justCache::tags('users')->flush()
.