简体   繁体   中英

How to clear cache for a particular webpage in a Rails Application

I have a Rails application running and there are several webpages and I am using default Rails caching. All the cache is stored in tmp/cache directory. I want to delete cache only for particular webpage and I want to do it on terminal something like rm -rf

rake tmp:cache:clear is your command.

You can also clear cache with Rails.cache.clear

If you are referring to clearing a fragment cache , one common pattern is to append a version string so that when you change the HTML of a view template, the cache for that template is busted. For example instead of:

<% cache do %>
  All available products:
  <% Product.all.each do |p| %>
    <%= link_to p.name, product_url(p) %>
  <% end %>
<% end %>

Do the following:

<% cache 'v1' do %>
  All available products:
  <% Product.all.each do |p| %>
    <%= link_to p.name, product_url(p) %>
  <% end %>
<% end %>

That way when you want to change the HTML inside of the cache block, you just change the string to 'v2'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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