lib/interlock/action_controller.rb in interlock-1.3 vs lib/interlock/action_controller.rb in interlock-1.4

- old
+ new

@@ -14,11 +14,11 @@ def caching_key(ignore = nil, tag = nil) ignore = Array(ignore) ignore = Interlock::SCOPE_KEYS if ignore.include? :all if (Interlock::SCOPE_KEYS - ignore).empty? and !tag - raise UsageError, "You must specify a :tag if you are ignoring the entire default scope." + raise Interlock::UsageError, "You must specify a :tag if you are ignoring the entire default scope." end if tag.is_a? Array and tag.all? {|x| x.is_a? Symbol} tag = tag.sort_by do |key| key.to_s @@ -119,15 +119,17 @@ def behavior_cache(*args) conventional_class = begin; controller_name.classify.constantize; rescue NameError; end options, dependencies = Interlock.extract_options_and_dependencies(args, conventional_class) - raise UsageError, ":ttl has no effect in a behavior_cache block" if options[:ttl] + raise Interlock::UsageError, ":ttl has no effect in a behavior_cache block" if options[:ttl] + Interlock.say "key", "yo: #{options.inspect} -- #{dependencies.inspect}" + key = caching_key(options.value_for_indifferent_key(:ignore), options.value_for_indifferent_key(:tag)) - if options[:perform] == false + if options[:perform] == false || Interlock.config[:disabled] Interlock.say key, "is not cached" yield else Interlock.register_dependencies(dependencies, key) @@ -147,12 +149,12 @@ # # Callback to reset the local cache. # def clear_interlock_local_cache - Interlock.local_cache = ::ActionController::Base::MemoryStore.new - RAILS_DEFAULT_LOGGER.warn "** cleared interlock local cache" + Interlock.local_cache = ::ActiveSupport::Cache::MemoryStore.new + Interlock.log "** cleared interlock local cache" end # Should be registered first in the chain prepend_before_filter :clear_interlock_local_cache @@ -170,11 +172,11 @@ def write_fragment(key, block_content, options = nil) return unless perform_caching content = [block_content, @template.cached_content_for] - fragment_cache_store.write(key, content, options) + cache_store.write(key, content, options) Interlock.local_cache.write(key, content, options) Interlock.say key, "wrote" block_content @@ -191,11 +193,11 @@ return unless perform_caching begin if content = Interlock.local_cache.read(key, options) # Interlock.say key, "read from local cache" - elsif content = fragment_cache_store.read(key, options) + elsif content = cache_store.read(key, options) raise Interlock::FragmentConsistencyError, "#{key} expected Array but got #{content.class}" unless content.is_a? Array Interlock.say key, "read from memcached" Interlock.local_cache.write(key, content, options) else # Not found @@ -223,7 +225,29 @@ raise e end end end - end + + # With Rails 2.1 action caching, we need to slip in our :expire param into the ActionCacheFilter options, so that when we + # write_fragment we can pass that in and allow shane's #{key}_expiry to take effect + # (see def write in interlock/config.rb) + module Actions + + module ClassMethods + def caches_action(*actions) + return unless cache_configured? + options = actions.extract_options! + around_filter(ActionCacheFilter.new(:cache_path => options.delete(:cache_path), :expire => options.delete(:expire)), {:only => actions}.merge(options)) + end + end + + class ActionCacheFilter #:nodoc: + def after(controller) + return if controller.rendered_action_cache || !caching_allowed(controller) + controller.write_fragment(controller.action_cache_path.path, controller.response.body, :expire => @options[:expire]) # pass in our :expire + end + end + end + + end end