ruby on rails - Clearing Fragment Caching from Console - Stack Overflow

I am new to Rails, going through the caching mechanism and facing a few issues in Fragment caching and

I am new to Rails, going through the caching mechanism and facing a few issues in Fragment caching and not being able to get the solution

In development.rb

config.action_controller.perform_caching = true
config.cache_store = :memory_store

In posts/index.html.rb

<p style="color: green"><%= notice %></p>

<h1>Posts</h1>

<div id="posts">
  <% @posts.each do |post| %>
  <% cache post do %>
    <%= logger.info "Rendering post #{post.id}" %>
    <%= render post %>
  <% end %>
    <p>
      <%= link_to "Show this post", post %>
    </p>
  <% end %>
</div>

<%= link_to "New post", new_post_path %>

The caching is working as expected and on update, the caching is expiring. According to the doc trying to delete the cache from the console

rails c

post = Post.first
Rails.cache.delete("views/#{post.cache_key}")

Returning false

On debugging, I found

cache_key = post.cache_key_with_version
Rails.cache.delete("views/#{cache_key}")

This also did not work Rails.cache.clear is also not working. It would be great help to understand this.

I am new to Rails, going through the caching mechanism and facing a few issues in Fragment caching and not being able to get the solution

In development.rb

config.action_controller.perform_caching = true
config.cache_store = :memory_store

In posts/index.html.rb

<p style="color: green"><%= notice %></p>

<h1>Posts</h1>

<div id="posts">
  <% @posts.each do |post| %>
  <% cache post do %>
    <%= logger.info "Rendering post #{post.id}" %>
    <%= render post %>
  <% end %>
    <p>
      <%= link_to "Show this post", post %>
    </p>
  <% end %>
</div>

<%= link_to "New post", new_post_path %>

The caching is working as expected and on update, the caching is expiring. According to the doc trying to delete the cache from the console

rails c

post = Post.first
Rails.cache.delete("views/#{post.cache_key}")

Returning false

On debugging, I found

cache_key = post.cache_key_with_version
Rails.cache.delete("views/#{cache_key}")

This also did not work Rails.cache.clear is also not working. It would be great help to understand this.

Share Improve this question edited Mar 11 at 23:26 dbugger 16.5k10 gold badges33 silver badges35 bronze badges asked Mar 11 at 20:07 GoJoGoJo 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

From the official guide

When your application receives its first request to this page, Rails will write a new cache entry with a unique key. A key looks something like this:

views/products/index:bea67108094918eeba42cd4a6e786901/products/1

The string of characters in the middle is a template tree digest. It is a hash digest computed based on the contents of the view fragment you are caching. If you change the view fragment (e.g., the HTML changes), the digest will change, expiring the existing file.

Also pay attention here to the view name (views/products/index). You didn't use it when tried to clear cache

If you want to delete such cache, you can invoke delete_matched method

Rails.cache.delete_matched("views/posts/index*/posts/#{post.id}*")

You can manually check stored keys using

Rails.cache.instance_variable_get(:@data).keys

Also pay attention to cache store. If you use memory store, it is process-dependent. So cache in the rails s and rails c are different. To check server cache use some debugger like this in your view

<% binding.irb %>

or same in the controller

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744773051a4592878.html

相关推荐

  • ruby on rails - Clearing Fragment Caching from Console - Stack Overflow

    I am new to Rails, going through the caching mechanism and facing a few issues in Fragment caching and

    15小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信