Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails缓存所有查询_Ruby On Rails_Will Paginate - Fatal编程技术网

Ruby on rails Rails缓存所有查询

Ruby on rails Rails缓存所有查询,ruby-on-rails,will-paginate,Ruby On Rails,Will Paginate,我有那种剧本 搜索\u controller.rb: def results @users = @search.cached_users_count.page(params[:page]).per_page(10) end Search.rb模型: users = users.order('current_sign_in_at DESC') def cached_users_count Rails.cache.fetch(["user_count", expires_i

我有那种剧本

搜索\u controller.rb:

def results
   @users = @search.cached_users_count.page(params[:page]).per_page(10)
end
Search.rb模型:

users = users.order('current_sign_in_at DESC')  


def cached_users_count

    Rails.cache.fetch(["user_count", expires_in: 5.minutes]) { users }

end
我想做的是缓存所有查询,因为当一些用户在执行搜索结果后登录时,将在下一页上对替换结果进行分页-因为刚刚登录的用户进入第一个位置,结果将按当前登录的顺序进行更改


有人能帮我吗?现在没有缓存,对于每个搜索结果都有sql响应。

在当前实现中有一些东西需要更改,以获得所需的行为

  • 您开始使用的
    users
    对象正在返回一个关系,但是 只有作用域,这意味着它没有像您期望的那样存储结果。 要存储查询结果,需要使用 查询方法。如果在Rails 3中,则使用
    all
    ;如果在Rails 4中,则使用
    load

    users.order('current_sign_in_at DESC').all  #Rails 3
    users.order('current_sign_in_at DESC').load #Rails 4, all is deprecated
    
  • 如果您真的希望Rails缓存的语法是不正确的 到期。您将在中传递
    expires\u作为密钥的一部分,该密钥是
    在当前代码中数组
    []
    。你想通过普通考试吗 参数,并将
    中的
    expires\u作为选项

    Rails.cache.fetch("user_count", expires_in: 5.minutes) { users }
    
  • 有关SO的类似问题,请参见以下内容: