Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

list keys of all cached fragments

I have a lot of cached fragments on my website because I use fragment caching with action_suffix to use caching in my application template. Im using this for the navigatin for example,... like this:

<% cache(:action => params[:action], :action_suffix => "navigation_#{request.path}") %>
  <%= render 'navigation_entries/navigation' %>
<% end %>

Its working pretty well but now I have a slight problem expiring the cached Fragments. What I need is a list of all keys for the cached fragments. I had more than just a look at the documentation but I didnt find anything.

Is there a way to list all fragment keys?

like image 717
davidb Avatar asked Jul 23 '12 13:07

davidb


2 Answers

You can pass a Regexp to expire_fragment:

expire_fragment(%r{navigation_})

According to the docs

Regexp expiration is only supported on caches that can iterate over all keys (unlike memcached).

like image 135
Stefan Avatar answered Sep 29 '22 12:09

Stefan


There's no method that I can find on the rails cache to list all keys, however if you're using the default file cache, you can list them by looking in the file system. When in the default rails dir on unix/linux you can use something like this to see all cache files (named after the keys url encoded):

find ./tmp/cache -type f

Or this to unencode and strip dirs as well:

find ./tmp/cache -type f | xargs ruby -e 'require "cgi";puts CGI::unescape(ARGV.sort.join("\n")).gsub(/.*tmp\/cache\/\w*\/\w*\//,"")'

Using a regex to delete will let you delete most keys, but for debugging it's nice sometimes to see the actual keys you're storing if you're using complex keys.

like image 29
Kenny Grant Avatar answered Sep 29 '22 12:09

Kenny Grant