-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Cache] Add support for a tag lifetime in the RedisTagAwareAdapter #47509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
It looks like you unchecked the "Allow edits from maintainer" box. That is fine, but please note that if you have multiple commits, you'll need to squash your commits into one before this can be merged. Or, you can check the "Allow edits from maintainers" box and the maintainer can squash for you. Cheers! Carsonbot |
5d68852
to
94ec4be
Compare
Unless I missed something, this would be very unsafe. If a tag expires before the items that are bound to it, said items won't be deleted when the same tag will be invalidated later on. You're saying that tags remain in the storage while you deleted the users? How does that work? Reading the code, deleteItems calls sRem when deleting items, and Redis doesn't store empty sets. So I don't see how this can happen yet. |
@nicolas-grekas I added a fail-safe mechanism to the code for this, so that the tag for an item can not have a lower lifetime than the item itself. See https://github.com/rebuy-de/symfony/blob/expire-redis-tags/src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php#L341-L344
Sorry for not being more clear on this. Le me try to explain how our current workflow looks like:
Now there are two possibilities where the cache (and/or tag) is explicitly cleared.
But if none of these two scenarios happen (e.g. because the user just left the website and is never coming back), the cache item will just expire after 5 minutes without the tag ever being deleted/expiring. And thats why we want to be able to define a lifetime for those tags. |
Thanks that for the details. Here is another idea: we could make the class implement PrunableInterface. Would that make sense to you? Up to give it a try? |
That sounds like a good alternative. I'll give it a try. Should I create a new PR or work on the new solution in this one? |
A new PR would be fine, to keep this one for history. |
Looking forward to your PR on the topic. Closing meanwhile, thanks for raising this point. |
…angling symlinks (nicolas-grekas) This PR was merged into the 6.2 branch. Discussion ---------- [Cache] Make FilesystemTagAwareAdapter::prune() remove dangling symlinks | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - As spotted in #47509 (comment) This could be considered as a bugfix, but I suggest to be conservative and consider this as a minor improvement. Commits ------- cc333ba [Cache] Make FilesystemTagAwareAdapter::prune() remove dangling symlinks
…angling symlinks (nicolas-grekas) This PR was merged into the 6.2 branch. Discussion ---------- [Cache] Make FilesystemTagAwareAdapter::prune() remove dangling symlinks | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - As spotted in symfony/symfony#47509 (comment) This could be considered as a bugfix, but I suggest to be conservative and consider this as a minor improvement. Commits ------- cc333baae6 [Cache] Make FilesystemTagAwareAdapter::prune() remove dangling symlinks
The cache tag's expire time should depends on the longest expire time key.
|
We use the
RedisTagAwareAdapter
cache to tag cached auth tokens for a specific user. These cached tokens have a rather short lifetime of 5 minutes.By tagging those token caches, we can invalidate all token caches if the user (e.g.) is disabled/deleted. However, since the tag itself never expires (and some users don't come back for months), we see a constant increase of non-expiring keys in our redis database, which also increases the memory usage. Changing the eviction policy to
allkeys-lru
orallkeys-lfu
might fix this, but these policies are not allowed to be used with theRedisTagAwareAdapter
and thus are not an option.To fix this, I added a new parameter to the
RedisTagAwareAdapter
where you can specify a potential tag lifetime. This parameter can have the following values:null
(default) -> same behaviour as before, the tags will not expiretrue
-> use the lifetime from the item itselfnumeric
-> explicitly define the tag lifetimeFrom our point of view this is more of a bug fix and I think it would make sense to backport this parameter to the oldest supported version (in this case 4.4). But I also can see that this might be more of a new feature, so if you think it makes more sense to only add it to the
6.2
branch, I'll adjust the base branch (and code) accordingly.Based on how we agree on this, I'll update the changelog and add a proper documentation PR.