Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Add initial value support to MemCacheStore increment and decrement calls#1

Closed
andrejbl wants to merge 22 commits intomainfrom
ab/add-mem-cache-store-incr-initial-support
Closed

Add initial value support to MemCacheStore increment and decrement calls#1
andrejbl wants to merge 22 commits intomainfrom
ab/add-mem-cache-store-incr-initial-support

Conversation

@andrejbl
Copy link
Copy Markdown

@andrejbl andrejbl commented May 3, 2022

Summary

ActiveSupport::Cache::MemCacheStore cache store increment and decrement calls do not have support for setting an initial value on first call even though that is supported by the Dalli client underneath: https://github.com/petergoldstein/dalli/blob/5588d98f79eb04a9abcaeeff3263e08f93468b30/lib/dalli/protocol/binary.rb#L111

This change adds the ability to use the :initial option to set one.

Before this change, to set and increment a counter one needed to do the following:

Rails.cache.write("bar", 1, raw: true)
=> 5321567009203290112
Rails.cache.increment("bar", 1)
=> 2
Rails.cache.write("foo", 1, raw: true)
=> 12022923254730588160
Rails.cache.decrement("foo", 1)
=> 0

After the change, you can combine the two:

Rails.cache.increment("bar", 1, initial: 1) # if key doesn't exist set it to initial value
=> 1
Rails.cache.decrement("foo", 1, initial: 2)
=> 2

or call the operations without doing the write at all and it will assume the initial value from the amount (for increment) or 0 (for decrement):

Rails.cache.increment("bar", 2)
=> 2
Rails.cache.decrement("foo", 1)
=> 0

@eugeneius
Copy link
Copy Markdown

This needs a changelog entry, since we're changing the default behaviour when the key is missing.

We should make the same change to decrement, although I think the default value for initial there should be 0 rather than amount, so that both methods behave as if the key was set to 0 before they were called:

https://github.com/memcached/memcached/wiki/BinaryProtocolRevamped#increment-decrement

Decrementing a counter will never result in a "negative value" (or cause the counter to "wrap"). instead the counter is set to 0.

@andrejbl andrejbl changed the title Add initial value support to MemCacheStore increment Add initial value support to MemCacheStore increment and decrement calls May 5, 2022
andrejbl and others added 4 commits May 5, 2022 16:56
Co-authored-by: Eugene Kenny <elkenny@gmail.com>
…ehavior.rb

Co-authored-by: Eugene Kenny <elkenny@gmail.com>
Co-authored-by: Eugene Kenny <elkenny@gmail.com>
andrejbl and others added 8 commits May 11, 2022 14:17
Co-authored-by: Eugene Kenny <elkenny@gmail.com>
Co-authored-by: Eugene Kenny <elkenny@gmail.com>
Co-authored-by: Eugene Kenny <elkenny@gmail.com>
Co-authored-by: Eugene Kenny <elkenny@gmail.com>
Co-authored-by: Eugene Kenny <elkenny@gmail.com>
Co-authored-by: Eugene Kenny <elkenny@gmail.com>
Co-authored-by: Eugene Kenny <elkenny@gmail.com>
@andrejbl andrejbl closed this May 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants