Wisdom of Rails Wizards Study on Best Cache Store

Hey all,

I had a few questions about Rails performance related to caching (at the
ActiveRecord and fragment levels), so I picked the brains of a few of
the finest Rails experts I know, who come from a more server
administration / performance background.

I’d also like to get as much feedback as possible, from anyone else that
would like to weigh in!

The main post is here:
http://onwebapps.com/wisdom-of-rails-wizards-study-in-best-activerecord-and-fragment-caching-store/

In case the above link gets truncated, should be the first post here for
a while:
http://onwebapps.com/

If you reply to this thread with your own take, or would like your reply
linked up from the original post, please let me know.

I’d also love to get some feedback on how you’d go about benchmarking
something like this. (or, if someone has already done this and
explained how they did it, please point me in the right direction!)

Cheers,

Shanti B.

shantibraford @ gmail

http://onwebapps.com/

ps - apologies if this post comes across as self-serving or promoting in
anyway. just wanted to share the knowledge I’ve gained from these great
responses!

Hi Shanti, I’m no rails expert. However I do quite a bit of fragment
caching on my site. Just recently I had to deal with performance
problems related to expiring fragment cache. If you use a regular
expression to expire cache it will stat every file in your cache
directory looking for matches. The more files you have the slower it
gets. I had to write a trivial plugin to the fragment cache module to
take a controller or path as an option to restrict this.

Cheers,
Dallas

Dallas wrote:

Hi Shanti, I’m no rails expert. However I do quite a bit of fragment
caching on my site. Just recently I had to deal with performance
problems related to expiring fragment cache. If you use a regular
expression to expire cache it will stat every file in your cache
directory looking for matches. The more files you have the slower it
gets.

Ouch! That sounds nasty. I was unaware of this. Do you have any plans
to release your plugin?

I’m not using any regexes for expiring fragments at the moment, but I
may do so at some point.

Do you use that to do things like, expire all fragments matching /page/
for example?

Thanks!

Shanti

Like I said I’m no rails expert, but if anyone else has this problem
this is what I did for my plugin.

Cheers,
Dallas

<% cache(:host => ‘sportspyder.com’, :action => ‘team_articles’,
:action_suffix => “articles_#{@team.id}#{@filter}#{@page}”) do -%>

expire_fragment(/articles_#{@team.id}_all/, :host =>
sportspyder.com’, :controller => ‘team’)

expire_fragment(/headline/, :host => ‘sportspyder.com’, :path=>
‘team/mlb/’, :recursive=>false)

module ActionController
module Caching
module Fragments
class UnthreadedFileStore #:nodoc:
def delete_matched(matcher, options) #:nodoc:
path = @cache_path
if options[:controller] && options[:host]
path = “#{path}/#{options[:host]}/#{options[:controller]}”
elsif options[:path] && options[:host]
path = “#{path}/#{options[:host]}/#{options[:path]}”

      end

      return unless File.directory?(path)
      recursive = [true, false].include?(options[:recursive]) ?

options[:recursive] : true

      if path == @cache_path && recursive
        ActionController::Base.logger.error("You are statting every

file in the cache directory. You should specify a host and controller
or path when using a regex for expiration. Regex [#{matcher}]")
end

      search_dir(path, recursive) do |f|

        if f =~ matcher
          begin
            File.delete(f)
          rescue Object => e
            # If there's no cache, then there's nothing to complain

about
end
end
end
end

    def search_dir(dir, recursive=true, &callback)
      Dir.foreach(dir) do |d|
        next if d == "." || d == ".."
        name = File.join(dir, d)
        if File.directory?(name) && recursive
          search_dir(name, recursive, &callback)
        else
          callback.call name
        end
      end
    end
  end
end

end
end