我是Redis和Rails缓存的新手,我想要执行简单的模型缓存。我刚刚读了这两篇文章:
http://www.sitepoint.com/rails-model-caching-redis/
http://www.victorareba.com/tutorials/speed-your-rails-app-with-model-caching-using-redis
由于Redis模型缓存包括将JSON字符串存储在redis中,并使用以下代码检索它们
def fetch_snippets
snippets = $redis.get("snippets")
if snippets.nil?
snippets = Snippet.all.to_json
$redis.set("snippets", snippets)
end
@snippets = JSON.load snippets
end
我不明白为什么要用
gem 'redis-rails'
gem 'redis-rack-cache'
在这种例子中,我不知道缓存存储或其他缓存机制在哪里使用,因为它们只存在于对Redis的读/写中。
谢谢你的帮助。
发布于 2016-01-15 23:14:17
这是我的档案里有的东西
gem 'redis'
gem 'readthis'
gem 'hiredis'
gem 'redis-browser'
阅读这篇文章--最近实现了一个很好的特性,当Redis关闭如果Redis关机,禁用Rails缓存时,它不会让Rails崩溃。它还支持高级Redis数据类型(不仅仅是作为redis-rails的字符串)。
雇工-更快一点
redis-浏览器-允许我查看实际缓存的内容(比cli更容易)。
这是我的application.rb
config.cache_store = :readthis_store, { expires_in: 1.hour.to_i, namespace: 'foobar', redis: { host: config.redis_host, port: 6379, db: 0 }, driver: :hiredis }
然后在我的模特身上:
def my_method_name
Rails.cache.fetch("#{cache_key}/#{__method__}", expires_in: 1.hour) do
# put my code here
end
end
我使用https://github.com/MiniProfiler/rack-mini-profiler查看哪些查询触发了大量DB请求,并确定了应该缓存哪些查询。
发布于 2016-01-15 19:40:57
你贴出来的片段并不是很聪明。它假定整个代码段集合从未在本地更新,因为它没有为存储到Redis中的内容设置任何过期。
至于宝石,如果你的目标是你发布的例子,你根本不需要它们。
redis-rails
很可能是一个插件,可以连接到Rails中的Redis。但是,连接到Redis就像创建一个初始化程序文件和使用Redis打开一个新的具有正确Redis URL的Redis连接一样简单。
第二个gem似乎为机架缓存添加了一个基于Redis的存储。如果你不知道它是什么,它可能更好,如果你不使用它。
https://stackoverflow.com/questions/34816494
复制相似问题