Skip to content
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

ActiveSupport::Cache::DalliStore: increment() against a non-existent key sets the key to "1\000\000..." and returns 1 #68

Closed
knu opened this issue Feb 14, 2011 · 2 comments

Comments

@knu
Copy link

knu commented Feb 14, 2011

Which is incompatible with MemCacheStore, which leaves the key unset and returns 0.

@mperham
Copy link
Collaborator

mperham commented Feb 14, 2011

This behavior is by design so you don't have to check for existence before each increment (which would double the network round trips). Yep, it sets the value (not key) to that string because of a marshalling incompatibility between ActiveSupport and Dalli. Just call to_i on the result or use the Dalli incr() method directly.

@lawrencepit
Copy link
Contributor

In issue #112 I'm providing a workaround allowing Rails.cache.increment(key, 1, :initial => nil) to return nil regardless of the underlying store.

Regarding the justification given above: the double network round trip happens only once; it's hardly a cost. And that is by design for us, because we only want to determine the initial value once as that usually is a very resource intensive operation compared to the one time extra call to memcached. For Dalli to assume the initial value is the same as the increment value is incorrect or at least arbitrary. The correct behaviour would be follow the API of the ActiveSupport::Cache store, i.e. return nil on a missing key.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants