繁体   English   中英

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

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

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

我的代码可以正常工作,但我需要你的帮助才能让它变得更好,请帮助我提高我的技能

谢谢您帮忙

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

我发现解决方案遵循这个原则如何在Ruby中将数组添加到另一个数组而不是最终得到多维结果?

概念是将两个数组中的所有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

希望这个帮助对任何人都有同样的问题:)

暂无
暂无

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

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