找到在Rails上使用Redis缓存的最佳方法

时间:2019-04-18 11:15:53

标签: ruby-on-rails ruby

我尝试使用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"}

我的代码可以正常工作,但是我需要您的帮助以使其变得更好,请帮助我提高技能

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

您可以使用内置的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

答案 1 :(得分:0)

我通过关注这些东西How do you add an array to another array in Ruby and not end up with a multi-dimensional result?

找到了解决方案

将概念分成两个数组,然后再次将其哈希到新数组中

=

希望对任何有同样问题的人都可以提供帮助:)