Wisdom of Rails Wizards Study on Best Cache Store

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

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