Skip to content

Instantly share code, notes, and snippets.

@madsheep
Created May 13, 2011 07:30
Show Gist options
  • Save madsheep/970143 to your computer and use it in GitHub Desktop.
Save madsheep/970143 to your computer and use it in GitHub Desktop.
Random values on passenger.
#production.rb
if defined?(PhusionPassenger)
PhusionPassenger.on_event(:starting_worker_process) do |forked|
# Only works with DalliStore
Rails.cache.reset if forked
end
end
#config/initializers/i18n.rb
I18n.backend = I18n::Backend::ActiveRecord.new
I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Memoize)
I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Flatten)
#this implements cache store inside active record store - we cache only the values that AR retrives.
#v important - DO NOT USE i18n cache implementation! its broken by design
module I18n
module Backend
class ActiveRecord
protected
def lookup_with_cache(locale, key, scope = [], options = {})
return unless key
separator = options[:separator] || I18n.default_separator
somekey = resolve_link(locale, key)
somekey = (Array(scope) + Array(somekey)).join(separator)
ret = Rails.cache.fetch("i18n-#{[locale, somekey].join(separator)}".gsub(/\s/, "_")) do
[lookup_without_cache(locale, key, scope, options)].to_json
end
JSON.parse(ret).first unless ret.nil?
end
alias_method_chain :lookup, :cache
#since we are using JSON to parse cached data hashes will come back like so:
#>> {"a" => "a"}
#but i18n needs:
#>> {:a => 'a'}
def pluralize_with_symbolized_keys(locale, entry, count)
if entry.is_a?(Hash)
pluralize_without_symbolized_keys(locale, entry.symbolize_keys, count)
else
pluralize_without_symbolized_keys(locale, entry, count)
end
end
alias_method_chain :pluralize, :symbolized_keys
end
end
end
I18n::Backend::Simple.send(:include, I18n::Backend::Memoize)
I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
I18n.backend = I18n::Backend::Chain.new(I18n.backend, I18n::Backend::Simple.new)
@madsheep
Copy link
Author

This is simple I18n.cache implementation specific for I18n AR backend. Rails.cache is used to cache only values stored in the database. This applies only if you are using i18n backend.

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