How do I expire a view cached fragment from console?
Cache fragment entries are created with a slightly different key than what you access with Rails.cache.
Use expire_fragment
instead (you can send it to a controller): http://api.rubyonrails.org/classes/ActionController/Caching/Fragments.html#M000438
Rails 4 Fragment Cache Entire Page. How to Expire Cache In Model
I believe your issue is the digest, so if you change your cache to this it should work:
<% cache 'main-page', skip_digest: true do %>
# html here
<% end %>
If you want to use this kind of style, where the cache doesn't expire and relies on detecting model changes to invalidate, you may want to consider using an Observer or Sweeper, which were removed from Rails 4, but are useful for this pattern:
https://github.com/rails/rails-observers
Not the answer you are looking for perhaps, but another way:
Make the cache key based on the max updated_at from the Post model.
Whenever any post changes, the cache key will automatically miss and go retrieve the latest posts to re-cache that part of the view.
module HomeHelper
def cache_key_for_posts
count = Post.count
max_updated_at = Post.maximum(:updated_at).try(:utc).try(:to_s, :number)
"posts/all-#{count}-#{max_updated_at}"
end
end
And then in your view:
<% cache cache_key_for_posts, skip_digest: true do %>
# html here
<% end %>
Manually Clear Fragment Cache in Rails
I ended up manually clearing the entire cache by going into the rails console and using the command:
Rails.cache.clear
Rails 4.0 expire_fragment/cache expiration not working
I believe the issue is that when you cache the fragment in your view, a cache digest is being added to the cache key (views/all_available_releases/41cb0a928326986f35f41c52bb3d8352), but expire_fragment is not using the digest (views/all_available_releases).
If you add skip_digest: true
to the cache call in the view it should prevent the digest from being used.
<% cache "all_available_releases", skip_digest: true do %>
<% @releases.each do |release| %>
<% cache(release) do %>
<html code with>
<%ruby code @release.name blah blah blah%>
<%end%>
<%end%>
<%end%>
Cache digests are only intended to be used with automatic cache expiration. If you need to manually expire cache keys then you can't use cache digests.
Rails - fragment cache not expiring
You might try this:
cache("news") do
%h2 News
- etc
end
and...
def expire_home_cache
puts "expire_home_cache"
expire_fragment("news")
end
...or try this ...
- cache({:key=>"news"}) do
%h2 News
- etc
I am thinking the issue may be that ruby or rails is having a hard time determining what the key is exactly and so the cache method and expire_fragment are generating two different cache keys.
How to get the proper key to use expire_fragment?
I'm not sure if this is by design, or whether there is a bug in Rails. But what is happening is the suffix /dca9add42d461d4c76103c08d12a6571
that's added to the end of the cache key when I use the cache command in my views, is a digest, which you can skip by calling cache like so:
<% cache("my-key", { :skip_digest: true }) %>
html
<% end %>
That way the suffix (digest) gets left off the key.
Now where I think a possible bug (?) is coming in, is that the expire_fragment
command doesn't add the digest to the end of the key - so I never actually end up expiring my fragments.
So my answer for now - is to use :skip_digest: true
if I want to manually expire them.
Rails fragment caching strategy
Action caching is also technically type of fragment caching which wraps your fragment. So your fragment might expire but this will not be visible untill the action cache expires.
So if want your fragment to expire you need to expire the action as well. But the fragment caching is not without purpose. If the action expires, the cached fragment will be used.
Related Topics
How to View a Sample of the Call Stack in Ruby
Rails 4 User Roles and Permissions
Normalizing Line Endings in Ruby
Escape Double and Single Backslashes in a String in Ruby
Can a Ruby Script Tell What Directory It's In
How to Get the Current Route in Rails
How to Do Advanced String Comparison in Ruby
How to Set a Custom User Agent in Ruby
Including/Extending the Kernel Doesn't Add Those Methods on Main:Object
Rspec Stubbing Method for Only Specific Arguments
How to Expire a View Cached Fragment from Console
How to Find Where a Ruby Method Is Declared
Ruby: Creating a Hash Key and Value from a Variable in Ruby
How to Check for Stdin Input in a Ruby Script