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

Allow for the configuration of a cache_key_digest method #285

Merged
merged 3 commits into from
Nov 3, 2016

Conversation

evanmiller67
Copy link
Contributor

This will allow someone to use something other than #hash on an object (which will change per VM startup).
Documented in issues #146 and #52; as well as here. Demonstrated here on 2.1.2:

$ irb
~> Console extensions: wirble hirb ap rails2 rails3 pm interactive_editor
>> 'foo'.hash
=> -2698411427522052147
>> exit
$ irb
~> Console extensions: wirble hirb ap rails2 rails3 pm interactive_editor
>> 'foo'.hash
=> 3357245547237632663

This is especially handy when using something like Redis to cache values which should persist across server restarts and allow for multiple application instances to talk to the same Redis server, or multiple applications sharing the same translation store.

Using something like Digest::MD5 takes double the time as Object#hash. However, the digest value remains consistent for the same raw value across multiple VMs.

I've done some benchmarks using different hashing algorithms and have uploaded the results here.

Props to @yellow5 for his help digging through with me!

allow someone to use something other than #hash on an object (which will
change per VM startup).

Using something like Digest::MD5 takes double the time as Object#hash,
however, it remains the same for the same value across multiple VMs.

This is especially handy when using something like Redis to cache values
which should persist across server restarts and allow for multiple
instances to talk to the same Redis server.

Update existing specs and add new spec for configurable
cache_key_digest
Copy link
Collaborator

@radar radar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the solution. It's flexible and doesn't dictate a digest algo to use. Please undo the change to USE_INSPECT_HASH and then I think this is good to go.

@@ -84,13 +97,13 @@ def _fetch(cache_key, &block)

def cache_key(locale, key, options)
# This assumes that only simple, native Ruby values are passed to I18n.translate.
"i18n/#{I18n.cache_namespace}/#{locale}/#{key.hash}/#{USE_INSPECT_HASH ? options.inspect.hash : options.hash}"
"i18n/#{I18n.cache_namespace}/#{locale}/#{digest_item(key)}/#{digest_item(options.inspect)}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This'll break earlier Ruby versions which do not have Hash ordering, I think.

@radar radar added this to the 0.8.0 milestone Nov 3, 2016
@yellow5
Copy link
Contributor

yellow5 commented Nov 3, 2016

I restored use of the USE_INSPECT_HASH constant on behalf of @evanmiller67.

The specs needed a minor tweak to be stabilized and should be good to go.

@radar
Copy link
Collaborator

radar commented Nov 3, 2016

LGTM. Thanks @yellow5 for getting this across the line :)

@radar radar merged commit 4b3725d into ruby-i18n:master Nov 3, 2016
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

Successfully merging this pull request may close these issues.

3 participants