简体   繁体   English

找到在rails上使用redis缓存的最佳方法

[英]Find a best way to work with redis cache on rails

I try to use Redis to cache on rails, but I get a challenge when trying to cache multi-language. 我尝试使用Redis缓存在rails上,但在尝试缓存多语言时遇到了挑战。 Because my Redis needs to be cached with table_translations 因为我的Redis需要使用table_translations进行缓存

I try with some code, but I don't think this is the best way 我尝试了一些代码,但我不认为这是最好的方法

I have the instance variable to work with Erb template 我有实例变量来使用Erb模板

def index
  @posts = fetch_posts
  @translations = fetch_translations

  puts @posts
  puts @translations
end

and Redis fetch like this 和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

I do that because I need to get all language version of a post, so I make a Redis key for translate 我这样做是因为我需要获得帖子的所有语言版本,所以我为翻译制作了Redis密钥

and my output: 和我的输出:

{"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"}

I find the best solution to make my output into a key, like this: 我找到了将输出变为密钥的最佳解决方案,如下所示:

{"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"}

My code can work correctly, but I need your help to make it better, please help me to improve my skills 我的代码可以正常工作,但我需要你的帮助才能让它变得更好,请帮助我提高我的技能

Thank for your help 谢谢您帮忙

You can use the built in Rails cache handler, this way you won't need to handle .nil? 您可以使用内置的Rails缓存处理程序,这样您就不需要处理.nil? calls to cache keys. 调用缓存键。

  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

I found the solution by follow this stuff How do you add an array to another array in Ruby and not end up with a multi-dimensional result? 我发现解决方案遵循这个原则如何在Ruby中将数组添加到另一个数组而不是最终得到多维结果?

concept is flatten all attr in two array and then Hash again this into new array 概念是将两个数组中的所有attr展平,然后将其再次哈希到新数组中

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

Hope this help to anyone have question same to me :) 希望这个帮助对任何人都有同样的问题:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM