2009-01-17 65 views
1

我使用cache-money gem來透明地使用Memcached。使用提供的配置文件可以在所有模式(開發,測試,生產)上啓用它。有沒有辦法只激活生產模式下的緩存資金?Cache-Money:僅用於生產?

目前尚不清楚如何做到這一點,而且這是一種在開發模式下處理緩存的總體痛苦。

回答

5

感謝Obie Fernandez爲一個偉大的離線提示:捻熄緩存錢的#INDEX方法,什麼也不做。這爲模型中的#index語句提供了一個位置,並停止了上述錯誤。

這裏是我的全部cache_money.rb lib目錄下:

if RAILS_ENV != 'development' 
    require 'cache_money' 

    config = YAML.load(IO.read(File.join(RAILS_ROOT, "config", "memcached.yml")))[RAILS_ENV] 
    $memcache = MemCache.new(config) 
    $memcache.servers = config['servers'] 

    $local = Cash::Local.new($memcache) 
    $lock = Cash::Lock.new($memcache) 
    $cache = Cash::Transactional.new($local, $lock) 

    class ActiveRecord::Base 
    is_cached :repository => $cache 
    end 
else 
    # If we're in development mode, we don't want to 
    # deal with cacheing oddities, so let's overrite 
    # cache-money's #index method to do nothing... 
    class ActiveRecord::Base 
    def self.index(*args) 
    end 
    end 
end 
0

在你的初始化,初始化跳過如果你在開發模式下運行:

unless 'development' == RAILS_ENV 
    require 'cache_money' 
    .... 
+0

我希望它是那麼容易。當緩存資金未加載時,我的索引模型無法加載: /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.0/lib/active_record/base.rb:1963:in `method_missing_without_paginate':未定義的方法`index'爲#(NoMethodError) – 2009-02-04 14:07:26

1

通過測試關閉緩存錢,你可以不知道它與您的代碼干擾。

我這樣做,而不是:

require 'cache_money' 
require 'memcache' 

if RAILS_ENV == 'test' 
    $memcache = Cash::Mock.new 
else 
    config = YAML.load(IO.read(File.join(RAILS_ROOT, "config", "memcached.yml")))[RAILS_ENV] 
    $memcache = MemCache.new(config) 
    $memcache.servers = config['servers'] 
end 

$local = Cash::Local.new($memcache) 
$lock = Cash::Lock.new($memcache) 
$cache = Cash::Transactional.new($local, $lock) 

class ActiveRecord::Base 
    is_cached :repository => $cache 
end