1

Memcached を透過的に使用するために、cache-money gem を使用しています。提供された構成ファイルを使用すると、すべてのモード (開発、テスト、本番) で有効になります。本番モードでのみキャッシュマネーを有効にする方法はありますか?

これを行う方法はすぐには明確ではなく、開発モードでキャッシュを扱うのは非常に面倒です。

4

3 に答える 3

5

Obie Fernandezが素晴らしいオフラインのヒントを提供してくれたことに感謝します。これにより、モデル内の #index ステートメントの場所が提供され、上記のエラーが停止します。

ここに私の完全な cache_money.rb ライブラリがあります:

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
于 2009-02-05T20:26:22.533 に答える
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
于 2009-05-16T20:15:57.073 に答える
0

初期化子で、開発モードで実行している場合は初期化をスキップします。

unless 'development' == RAILS_ENV
  require 'cache_money'
  ....
于 2009-02-03T21:59:30.883 に答える