如何在测试环境中设置dalli缓存?

5

我将使用Dalli缓存作为键值存储。

通常在生产和开发环境中,我们有一个命令行

config.cache_store = :dalli_store

那么我们可以使用Rails.cache结构从缓存中读取和写入内容。

但通常在测试环境中,我们没有这个配置项。

在测试环境中设置缓存的正确方法是什么,以便测试我的存储逻辑?

P.S. 我正在使用Linux(Ubuntu)。


你可以在本地设置一个memcached实例,但具体步骤取决于你使用的操作系统。你是在Windows/Mac/Linux上吗? - Mark Stratmann
@MarkStratmann,我在使用Linux。 - megas
2个回答

5

dalli是用于缓存服务(memcached)的客户端,可以在任何环境下全局设置,例如在config/application.rb中。

config.cache_store = :dalli_store

在测试环境中停用缓存是一种常见的方法,请检查config/environments/test.rb文件。

config.action_controller.perform_caching = false

因此,您可以在测试环境中启用它,但这可能会导致一些奇怪的冲突。最好的方法可能是仅对特定规格进行动态启用:

before do # enable caching
  @caching_state = ActionController::Base.perform_caching
  ActionController::Base.perform_caching = true
end

after do # disable caching
  ActionController::Base.perform_caching = @caching_state
end

-1

我假设你正在使用Ubuntu,并在Google上搜索了“ubuntu install memcached rails”,找到了几个详细页面。以下是关键点。

安装memecache

sudo apt-get install memcached

重启 memcache
/etc/init.d/memcached restart

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接