-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Add initial value support to ActiveSupport::Cache #increment and #decrement calls #45068
Add initial value support to ActiveSupport::Cache #increment and #decrement calls #45068
Conversation
Thanks andrejbl! |
7aaaac2
to
7f815ad
Compare
I'm afraid I don't follow, is this meant to be a fix for #45069 or does it just happens to allow a workaround? Because by reading that issue, my reasoning is more to fix As for this specific PR, do you have a use case for setting an initial value? I guess it could be useful if you decrement until 0 ? But if we're going to add this feature, I think we should try to add it to all stores, otherwise their feature set will diverge even more. |
I had the same idea in #45077, though with a more explicit opt in behaviour to not make the change potentially breaking. There's also potentially confusing behaviour around decrement, as memcached only operates on unsigned integers, so the value cannot be < 0. I think defaulting the initial value there to 0 can work fine there (if we want to add a default initial value) |
Yeah, I misread this. I would prefer a solution for all stores and one set
at the store level instead of the method level. E.g.
MemoryStore.init_increments_to_zero = true
…On Thu, May 12, 2022, 6:02 AM Adam Renberg Tamm ***@***.***> wrote:
I had the same idea in #45077 <#45077>,
though with a more explicit opt in behaviour to not make the change
potentially breaking. There's also potentially confusing behaviour around
decrement, as memcached only operates on unsigned integers, so the value
cannot be < 0. I think defaulting the initial value there to 0 can work
fine there (if we want to add a default initial value)
—
Reply to this email directly, view it on GitHub
<#45068 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAXDJ3KPYNE2LMIDFCP4IXDVJT6PRANCNFSM5VVM22IA>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
To answer @byroot 's question - this PR is not related to #45069. It just happens to touch the problem there. The initial idea was just to simplify counter initialisation in The point about extending this to other stores makes sense ( To @tgwizard 's and @asavageiv comments - yes this might be a breaking change, in case someone made a strong dependency on |
I think so, let's start by making the stores converge a bit more by having them all default to 0 (well 1 if you increment). For memcached not supporting signed integers, I think it's ok to keep this as a difference, I don't want to gimp the other stores. Then once that's done, if someone come up with reasonable use cases for an |
Just to clarify. I think the behavior that makes the most sense is to assume that if the key doesn't exist, then it's value is Of course there is what we said about Memcached not supporting negative counters, so it doesn't apply here, but for the others we should do that. |
Just let Memcache be stuck at 0, it's fine. |
activesupport/test/cache/behaviors/cache_increment_decrement_behavior.rb
Outdated
Show resolved
Hide resolved
Some minor documentation things, please squash your commits though. |
Starting to look good, I'll try to find some time to do a final review. In the meantime please squash your commits, this way if there's no other concerns I can merge directly. |
429de6b
to
f48bf39
Compare
Thank you! |
Fix: rails#46301 When incrementing in Memcached or Redis, we use `raw` values, so only the backing store TTL counts, and is preserved. So for consistency we need to emulate this in MemoryStore by re-using the existing entry's `expires_at`. --- Some of the changes introduced in [the original PR](rails#46305) were discarded, such as the `normalize_` methods, as well as the change that originated in [that PR](rails#45068) that sets a value if none exists.
Summary
ActiveSupport::Cache
cache storesincrement
anddecrement
calls do not have support for setting an initial value on first call.This change sets the initial value to the amount provided in the call (or 0 for
decrement
calls onMemCacheStore
as it only supports unsigned integers).Before this change, to set and increment a counter one needed to do the following:
After the change, there is no need for an explicit write to set the initial value: