Odd and potentially unintuitive behavior when using Cache::tags()
#44055
Unanswered
BrekiTomasson
asked this question in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Say I have something like the following in my
User
model:If I then do something like:
Under the hood, this will store the
$name
property of User 36 using the cache keyname
, tagging it with bothusers
anduser.36
. While this is cached, I will be able to get this value by doing something likeCache::tags(['users', 'user.36'])->get('name')
, just as one would expect.If I want to forget everything having to do with User 36, I can
Cache::tags('user.36')->flush()
, which would meanCache::tags(['users', 'user.36'])->get('name')
would returnnull
butCache::tags(['users', 'user.23'])->get('name')
would return the name of User 23 (assuming i've stored the name of User 23 previously, of course). I can alsoCache::tags('users')->flush()
and neither theuser.36
or theuser.23
versions of that query would return a value other thannull
.So far, so good. Everything is working as expected and we can define our keys with much simpler names by moving some of the responsibilities into the tags rather than the more verbose
Cache::tags('users')->put('36:name', 'Kevin')
orCache::put('user:36:name', 'Kevin')
, for example.However, there are three use cases where the way cache tags works becomes somewhat unintuitive, one of which feels entirely broken. They are:
1. Accessing a key using a subset of the tags:
When you define a cache key with two or more tags, you cannot access the key using a subset of those tags, even if the result would be unique.
I can kind of understand this one, since there is no guarantee that the key you used will be unique for any one of the tags you included. Changing this to return
'Kevin'
would requireCache::tags('users')->get('name')
to throw an exception of some kind as there are multiple keys calledname
that use the same underlying tag and your request needs to be made more specific. This would most likely be a breaking change and, as such, I'd say this is working as intended.Still; it's somewhat unexpected behavior and might need better documentation.
2. Changing the order of your cache tags:
For some reason, when you
get()
a value using a Cache that you've tagged with an array, you need to name the cache tags in the exact same order that they were defined when youput()
the name there.This is entirely unexpected and - in my opinion - broken. Laravel should, under the hood, sort the array passed into
tags()
so that it doesn't matter in which order the cache tags are provided.3. Changing all values of a tag Subset
You cannot 'bulk replace' tags by providing a new value for one or more of its tags.
Again, this most likely has to do with how Laravel converts the contents of
tags()
to something else under the hood, but again, this is somewhat unexpected behavior, especially when you think of how you canflush()
any one of the tags to remove the value:Beta Was this translation helpful? Give feedback.
All reactions