找到在 Rails 上使用 redis 缓存的最佳方法
Posted
技术标签:
【中文标题】找到在 Rails 上使用 redis 缓存的最佳方法【英文标题】:Find a best way to work with redis cache on rails 【发布时间】:2019-09-08 17:18:37 【问题描述】:我尝试使用 Redis 在 Rails 上进行缓存,但在尝试缓存多语言时遇到了挑战。因为我的Redis需要用table_translations缓存
我尝试了一些代码,但我认为这不是最好的方法
我有使用 Erb 模板的实例变量
def index
@posts = fetch_posts
@translations = fetch_translations
puts @posts
puts @translations
end
Redis 像这样抓取
private
def fetch_posts
begin
posts = $redis.get "posts"
if posts.nil?
posts = []
Post.all.order("id ASC").each do |post|
posts << post
end
posts = posts.to_json
$redis.set "posts", posts
end
posts = JSON.load posts
rescue => error
puts error.inspect
posts = Post.all
end
posts
end
def fetch_translations
begin
translations = $redis.get "translations"
if translations.nil?
translations = []
Post.all.order("id ASC").each do |post|
post.translations.order("locale ASC").each do |translation|
translations << translation
end
end
translations = translations.to_json
$redis.set "translations", translations
end
translations = JSON.load translations
rescue => error
puts error.inspect
translations = Post.all
end
translations
end
我这样做是因为我需要获取帖子的所有语言版本,所以我为翻译制作了一个 Redis 密钥
和我的输出:
"id":1,"slug":"hello-world","thumb_url":"thumbs/null","thumb_file_name":null,"thumb_content_type":null,"thumb_file_size":null,"thumb_updated_at":null,"featured":false,"hidden":false,"created_at":"2019-04-18T07:05:09.117Z","updated_at":"2019-04-18T07:27:55.830Z"
"title":"Xin chao","description":"Day la bai viet dau tien, duoc viet tu rails CMS","body":"xin chao cac ban"
"title":"Hello World","description":"This is first post from rails CMS","body":"Hello every body"
我找到了将输出变成密钥的最佳解决方案,如下所示:
"id":1,"slug":"hello-world","thumb_url":"thumbs/null","thumb_file_name":null,"thumb_content_type":null,"thumb_file_size":null,"thumb_updated_at":null,"featured":false,"hidden":false,"created_at":"2019-04-18T07:05:09.117Z","updated_at":"2019-04-18T07:27:55.830Z","title":"Xin chao","description":"Đay la bai viet đau tien, đuoc viet tu rails CMS","body":"xin chao cac ban"
"id":1,"slug":"hello-world","thumb_url":"thumbs/null","thumb_file_name":null,"thumb_content_type":null,"thumb_file_size":null,"thumb_updated_at":null,"featured":false,"hidden":false,"created_at":"2019-04-18T07:05:09.117Z","updated_at":"2019-04-18T07:27:55.830Z",title":"Hello World","description":"This is first post from rails CMS","body":"Hello every body"
我的代码可以正常工作,但我需要你的帮助才能让它变得更好,请帮助我提高我的技能
感谢您的帮助
【问题讨论】:
【参考方案1】:您可以使用内置的 Rails 缓存处理程序,这样您就不需要处理 .nil?
对缓存键的调用。
private
def fetch_posts
posts = Rails.cache.fetch("posts") do
begin
Post.all.order("id ASC").as_json
rescue => error
puts error.inspect
Post.all
end
end
posts
end
def fetch_translations
translations = Rails.cache.fetch("translations") do
begin
Post.all.order("id ASC").map do |post|
post.translations.order("locale ASC").as_json
end.flatten
rescue => error
puts error.inspect
Post.all
end
end
translations
end
【讨论】:
谢谢你的东西@ErvalhouS :) 我会在其他情况下尝试 Rails 缓存【参考方案2】:我通过关注这个东西How do you add an array to another array in Ruby and not end up with a multi-dimensional result?找到了解决方案
概念是将两个数组中的所有属性展平,然后再次将其散列到新数组中
def fetch_posts
posts = []
Post.all.order("id ASC").each do |post|
post.translations.each do |translation|
posts << [*post.attributes.slice('slug','thumb_url'),*JSON.parse(translation.to_json)].to_h
end
end
end
希望对任何对我有同样问题的人有所帮助:)
【讨论】:
以上是关于找到在 Rails 上使用 redis 缓存的最佳方法的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Amazon OpsWorks 上为 Rails 应用程序运行 Redis?
为 memcached 和 Rails 组合片段和对象缓存的最佳方式
Redis 缓存:当多个应用程序写入/读取数据时选择的最佳模式