我想分别为每个用户/访问者缓存搜索块,以便当用户/访问者搜索或排序任何内容时,搜索块会缓存所有CSS和视觉更改。因此,当用户在5.minutes中重新访问Advertisement #index页面时,Rails缓存会呈现与他离开时相同的搜索块。
以前,我尝试计算排序链接上的点击次数,然后将其保存为Cookie,然后在加载广告#index页面时,进行所有点击。但这种方法非常讨厌,有时候也没有用。
到目前为止我已成功缓存搜索结果并根据session.id检索它们。
我在Advertisement_controller中的代码:index :
if params[:q].present? #if search params is present
@cache_search = true #this is for fragment cache in view
Rails.cache.delete("q_#{request.session.id}")
Rails.cache.write("q_#{request.session.id}",params[:q],:expires_in => 5.minutes)
@search = @adver.search(params[:q])
else
params[:q] = Rails.cache.read("q_#{request.session.id}")
@search = @adver.search(params[:q])
end
@search.sorts = ['height asc','age asc','votes_for.size asc'] if @search.sorts.empty?
@advertisements = @search.result(distinct: true)
#delete cache is search resulted in 0 results
if @advertisements.present?
else
Rails.cache.delete("q_#{request.session.id}")
end
此代码生成此缓存:q_925feaae9c1cd1f6948899e672838c0a。
我的搜索区块:
<% cache_if @cache_search == true, request.session.id do %>
<%= search_form_for @search, :class=>"search",:id=>"search-menio",:remote=>"true", url: girls_path, :method => :get do |f| %>
<%= f.text_field :name_or_phone_number_or_identifier_cont, :id=>"search-field-keyup",:class=>"form-control-s" %>
<%end%>
<%= sort_link(@search, :height,t('height'),{hide_indicator: true},{ :remote => true, :method => :get }) %>
<%= sort_link(@search, :age,t('age'),{hide_indicator: true},{ :remote => true, :method => :get }) %>
<%= sort_link(@search, 'votes_for.size',t('rating'), {hide_indicator: true},{ :remote => true, :method => :get }) %>
<%end%>
<%end%>
问题:Rails在搜索发生时不会缓存搜索块,但是当我第一次访问Advertisement #index page时。如何编辑我的代码,以便每次搜索时更新搜索块缓存并为此缓存搜索块提供5.分钟或新搜索?
我搜索过similair问题,但没有一个像我的一样。
感谢您的帮助。
答案 0 :(得分:0)
我知道这个答案对你的目的来说可能为时已晚,但我会回答未来的搜索者。一些想法:
[@search, request.session.id]
。您可能还希望在五分钟内使片段缓存失效。params[:q]
存储在会话中而不是缓存中。这样肯定会在那里呆上五分钟。如果缓存填满,缓存项可能会提前到期。您需要在会话中添加时间戳,以便在5分钟后使查询字符串过期。我建议将其编码如下:
<强>控制器强>
def index
if params[:q].present? #if search params is present
@cache_search = true #this is for fragment cache in view
session[:search_string] = params[:q]
session[:q_created_at] = Time.now.to_i
elsif session[:q_created_at].present? && session[:q_created_at] > 5.minutes.ago.to_i
params[:q] = session[:q]
end
@search = @adver.search(params[:q]) if params[:q].present?
@search.sorts = ['height asc','age asc','votes_for.size asc'] if @search.sorts.empty?
@advertisements = @search.result(distinct: true)
#delete query string session parameters if search resulted in 0 results
unless @advertisements.present?
session.delete(:q)
session.delete(:q_created_at)
end
end
查看强>
<% cache_if @cache_search, [@search, request.session.id] do %>
<%= search_form_for @search, :class=>"search",:id=>"search-menio",:remote=>"true", url: girls_path, :method => :get do |f| %>
<%= f.text_field :name_or_phone_number_or_identifier_cont, :id=>"search-field-keyup",:class=>"form-control-s" %>
<%end%>
<%= sort_link(@search, :height,t('height'),{hide_indicator: true},{ :remote => true, :method => :get }) %>
<%= sort_link(@search, :age,t('age'),{hide_indicator: true},{ :remote => true, :method => :get }) %>
<%= sort_link(@search, 'votes_for.size',t('rating'), {hide_indicator: true},{ :remote => true, :method => :get }) %>
<%end%>
<%end%>
此代码尚未经过测试。