在我的Rails 3.1.2項目中,我使用ActiveRecord作爲I18n.backend。我想緩存MemCache中的值。但是我的開發和測試環境之間存在奇怪的行爲差異。Rails 3.1 i18n和memcache
我的 '配置/初始化/ i18n.rb'
# https://github.com/svenfuchs/i18n-active_record/blob/master/lib/i18n/backend/active_record/translation.rb
require 'i18n/backend/active_record'
require 'i18n_cache_backend'
I18n.default_locale = :en
I18n.backend.class.send(:include, I18n::Backend::Fallbacks)
active_record_backend = I18n::Backend::ActiveRecord.new
active_record_backend.class.send(:include, I18n::Backend::Fallbacks)
I18n::Backend::ActiveRecord.send :include, I18n::Backend::Cache
I18n.cache_store = Rails.application.config.i18n_cache_store
I18n.backend = I18n::Backend::Chain.new(active_record_backend, I18n.backend)
我 '的lib/i18n_cache_backend',改寫:store_translation方法來清理緩存。
module I18n::Backend::Cache
def store_translations(locale, data, options = {})
flatten_keys(data, true) do |key, value|
c_key = cache_key(locale, key.to_s, options)
I18n.cache_store.delete c_key
end
super
end
end
在 '配置/環境/ development.rb' 我有:
config.i18n_cache_store = ActiveSupport::Cache.lookup_store(:mem_cache_store,
'localhost', :namespace => "development")
在 '配置/環境/ beta.rb' 我有:
config.i18n_cache_store = ActiveSupport::Cache.lookup_store(:mem_cache_store,
'memcache.v.l', :namespace => "beta")
在發展軌道控制檯:
ruby-1.8.7-p352 :008 > I18n.cache_store.clear
=> [<MemCache::Server: localhost:11211 [1] (CONNECTED)>]
ruby-1.8.7-p352 :009 > helper.t 'main.wire'
[SQL QUERY GOES HERE]
=> "Wire"
ruby-1.8.7-p352 :010 > helper.t 'main.wire'
=> "Wire"
ruby-1.8.7-p352 :011 > I18n.backend.store_translations(:en, {:main => {:wire => "foo bar baz"}}, {:rescue_format=>:html})
[SQL UPDATES GOES HERE]
=> {:"main.wire"=>"foo bar baz"}
ruby-1.8.7-p352 :012 > helper.t 'main.wire'
[SQL QUERY GOES HERE]
=> "foo bar baz"
ruby-1.8.7-p352 :013 > helper.t 'main.wire'
=> "foo bar baz"
在公測ENV軌道控制檯:
ruby-1.8.7-p352 :001 > I18n.cache_store.clear
=> [<MemCache::Server: memcache.v.l:11211 [1] (CONNECTED)>]
ruby-1.8.7-p352 :002 > helper.t 'main.wire'
[SQL QUERY GOES HERE]
=> "Wire"
ruby-1.8.7-p352 :003 > helper.t 'main.wire'
=> "Wire"
ruby-1.8.7-p352 :004 > I18n.backend.store_translations(:en, {:main => {:wire => "foo bar baz"}}, {:rescue_format=>:html})
[SQL UPDATES GOES HERE]
=> {:"main.wire"=>"foo bar baz"}
ruby-1.8.7-p352 :005 > helper.t 'main.wire'
=> "Wire"
ruby-1.8.7-p352 :006 > helper.t 'main.wire'
=> "Wire"
ruby-1.8.7-p352 :007 > I18n.cache_store.clear
=> [<MemCache::Server: memcache.v.l:11211 [1] (CONNECTED)>]
ruby-1.8.7-p352 :008 > helper.t 'main.wire'
[SQL QUERY GOES HERE]
=> "foo bar baz"
ruby-1.8.7-p352 :009 > helper.t 'main.wire'
=> "foo bar baz"
有人有想法嗎?在發展高速緩存翻譯是clrear後每次:store_translation,在測試緩衝值不刷新,除非我做I18n.cache_store.clear