From df7a4d498c542a57d99622e160b3ede3d8b688a9 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 26 Mar 2005 13:09:56 +0000 Subject: Added expire_matched_fragments(regular_expression) to clear out a lot of fragment caches at once #927 [technoweenie@gmail.com] Fixed the problems with : and ? in file names for fragment caches on Windows #927 [technoweenie@gmail.com] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@996 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 4 ++++ actionpack/lib/action_controller/caching.rb | 35 +++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index f4be0319e0..d0eeb05914 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,9 @@ *SVN* +* Added expire_matched_fragments(regular_expression) to clear out a lot of fragment caches at once #927 [technoweenie@gmail.com] + +* Fixed the problems with : and ? in file names for fragment caches on Windows #927 [technoweenie@gmail.com] + * Added TextHelper#human_size for formatting file sizes, like human_size(1234567) => 1.2 MB #943 [thomas@fesch.at] * Fixed link_to :confirm #936 [Nicholas Seckar] diff --git a/actionpack/lib/action_controller/caching.rb b/actionpack/lib/action_controller/caching.rb index 4c11bc99d8..d071447c81 100644 --- a/actionpack/lib/action_controller/caching.rb +++ b/actionpack/lib/action_controller/caching.rb @@ -285,7 +285,12 @@ module ActionController #:nodoc: fragment_cache_store.delete(name, options) logger.info "Expired fragment: #{name}" unless logger.nil? end - + + def expire_matched_fragments(re=Regexp.new('/*/'), options = {}) + fragment_cache_store.delete_matched(re, { :root_path => url_for.split('://').last.split('/').first }) + logger.info "Expired all fragments matching: #{re} " unless logger.nil? + end + class MemoryStore #:nodoc: def initialize @data, @mutex = { }, Mutex.new @@ -302,6 +307,10 @@ module ActionController #:nodoc: def delete(name, options = {}) #:nodoc: @mutex.synchronize { @data.delete(name) } end + + def delete_matched(re, options) #:nodoc: + @mutex.synchronize { @data.delete_if {|k,v| k.index(options[:root_path]) == 0 and k =~ re} } + end end class DRbStore < MemoryStore #:nodoc: @@ -335,15 +344,37 @@ module ActionController #:nodoc: def delete(name, options) #:nodoc: File.delete(real_file_path(name)) if File.exist?(real_file_path(name)) end + + def delete_matched(re, options) #:nodoc: + rootPath = real_file_path(options[:root_path]) + search_dir(@cache_path).each do |f| + File.delete(f) if f.index(rootPath) == 0 and f =~ re and File.exist?(f) + end + end private def real_file_path(name) - "#{@cache_path}/#{name}" + '%s/%s' % [@cache_path, name.gsub('?', '.').gsub(':', '.')] end def ensure_cache_path(path) FileUtils.makedirs(path) unless File.exists?(path) end + + def search_dir(dir) + require 'pathname' + files = [] + dir = Dir.new(dir) + dir.each do |d| + unless d == '.' or d == '..' + d = File.join(dir.path, d) + p = Pathname.new(d) + files << p.to_s if p.file? + files += search_dir(d) if p.directory? + end + end + files + end end end -- cgit v1.2.3