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] Inconsistent behavior in namespaced_key #8615

Closed
wants to merge 1 commit into from

Conversation

bbenezech
Copy link

fix for #8614

>  Rails.cache.send(:namespaced_key, User.first, {})
 => "users/508fefdb54ca455644000003-20121226093034" 
> Rails.cache.send(:namespaced_key, [User.first, User.last], {})
 => "users/508fefdb54ca455644000003-20121226093034/users/50b7832354ca45f043000011-20121129154539" 
> Rails.cache.send(:namespaced_key, [User.first], {})
 => "508fefdb54ca455644000003" 

# yurk, should probably be "users/508fefdb54ca455644000003-20121226093034" instead

Also this is inconsistent with the (internally used) ActiveSupport::Cache.expand_cache_key

> ActiveSupport::Cache.expand_cache_key User.first
 => "users/508fefdb54ca455644000003-20121226093034" 
> ActiveSupport::Cache.expand_cache_key [User.first, User.last]
 => "users/508fefdb54ca455644000003-20121226093034/users/50b7832354ca45f043000011-20121129154539" 
>   ActiveSupport::Cache.expand_cache_key [User.first]
 => "users/508fefdb54ca455644000003-20121226093034" 

@rafaelfranca
Copy link
Member

I'm not sure what is the intended behavior, but this seems to be not backward compatible.

cc @jeremy

@bbenezech
Copy link
Author

@rafaelfranca I did not see any doc/test indicating this is the intended behaviour. Code simply stayed as Brian wrote it 3 years ago. Maybe he can tell us more about it.

cc @bdurand

@bdurand
Copy link
Contributor

bdurand commented Dec 27, 2012

The implementation should generate unique, consistent keys for distinct objects. I'm not sure if the previous implementation was correct (it doesn't look like it is from the output listed above), but I don't think the proposed fix is correct either.

Consider:

id = 1
Rails.cache.fetch(id){ User.find(id) }

will not give the same result as

id = [1]
Rails.cache.fetch{ User.find(id) }

The first case will return a single User record, the second will return an array containing one User record, but both would be stored with the same cache key with the proposed fix.

@bbenezech
Copy link
Author

This is a whole different issue. It wasn't designed that way, was it ? The possible way that i can think of to differentiate both cases is to add a trailing slash at the end for the array with one element.

@bbenezech
Copy link
Author

Bump. This behavior needs to be fixed somehow...

@JonRowe
Copy link
Contributor

JonRowe commented Mar 12, 2013

Any more feedback for @bbenezech core team? (cc @rafaelfranca @jeremy @bdurand)

arthurnn added a commit to arthurnn/rails that referenced this pull request Mar 4, 2014
`cache.fetch(['foo'])` and `cache.fetch('foo')` should generate
different cache keys as they are not equivalents.

[related rails#8615]
[related rails#8614]
@arthurnn
Copy link
Member

arthurnn commented Mar 4, 2014

👍 this is good to be merged into master... this will break backwards compatible, so it should not be backported.

@arthurnn
Copy link
Member

arthurnn commented Mar 4, 2014

Actually, I think we are missing some tests. The tests you added are all for cache_key. Maybe add tests that pass a string: e.g. @cache.write(['foo'], "bar")

@arthurnn
Copy link
Member

arthurnn commented Mar 5, 2014

From the same note on #14269 (comment), I dont think we should go forward with this, as we cannot change the cache_key structure.
Thanks anyways for the patch
cc @chancancode

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

Successfully merging this pull request may close these issues.

None yet

5 participants