From eb62ad214483022e5332da258f04c6480bce86cb Mon Sep 17 00:00:00 2001 From: Karel Minarik Date: Thu, 14 Aug 2008 18:46:33 +0200 Subject: [PATCH] Added expire_fragment_by_glob() method to ActionController::Caching::Fragments --- .../lib/action_controller/caching/fragments.rb | 14 ++++++++++++++ actionpack/test/controller/caching_test.rb | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+), 0 deletions(-) diff --git a/actionpack/lib/action_controller/caching/fragments.rb b/actionpack/lib/action_controller/caching/fragments.rb index e9b434d..6fb4601 100644 --- a/actionpack/lib/action_controller/caching/fragments.rb +++ b/actionpack/lib/action_controller/caching/fragments.rb @@ -133,6 +133,20 @@ module ActionController #:nodoc: end end end + + # Expire fragment in ActiveSupport::Cache::FileStore by 'glob' value as argument, eg. 'some/deep/**/*/path' + # === Examples: + # expire_fragment_by_glob('events/{theatre,movie}') + # expire_fragment_by_glob('program/**/*') + # Motivation was to expire fragment for every hostname you are running your application on (application.com, application.org) + def expire_fragment_by_glob(pattern) + raise NoMethodError, "Not supported by #{cache_store.class}" unless cache_store.is_a?(ActiveSupport::Cache::FileStore) + # TODO: Would be nice to log here, but I keep getting 'uninitialized constant RAILS_DEFAULT_LOGGER' in tests + self.class.benchmark "Expired fragments by pattern: #{pattern}" do + Dir[ File.join(cache_store.cache_path, '**', "#{pattern}.cache") ].each { |path| FileUtils.rm_rf(path) } + end + end + end end end diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index b6cdd11..f062093 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -572,6 +572,25 @@ class FragmentCachingTest < Test::Unit::TestCase end end +class FileStoreFragmentCachingTest < FragmentCachingTest # Inherit setup method, but... + self.public_instance_methods.select { |m| m =~ /^test_/ }.each { |m| undef_method(m.to_sym) } # ... undefine test methods from parent + def setup + super + @store = ActiveSupport::Cache::FileStore.new(Dir.pwd) + ActionController::Base.cache_store = @store + end + + def test_expire_fragment_with_glob + @store.write('views/name/other/name', 'value') + + @controller.expire_fragment_by_glob 'views/name/*' # Do not expire with this glob... + assert_equal 'value', @store.read('views/name/other/name') + + @controller.expire_fragment_by_glob 'views/name/**/*' # ...but do expire with this one. + assert_nil @store.read('views/name/other/name') + end +end + class FunctionalCachingController < ActionController::Base def fragment_cached end -- 1.5.5