require 'fileutils' require 'nitro/caching/fragments' module Nitro # Adds support for caching. module Caching # Action caching is a set of helper to allow the caching # generated by an action. In the caching hierarchy it lies # between Output caching (top action, full page) and fragment # caching (fine-grained caching). module Actions def self.included(base) # :nodoc: base.extend(ClassMethods) end module ClassMethods # Cache the given actions. def cache_action(*actions) return unless caching_enabled? before( %{ fragment_name = "\#\{@action_name\}\#{@request.query_string}" if fragment = Fragments.get(fragment_name) @out = fragment return end }, :only => actions ) after( %{ fragment_name = "\#\{@action_name\}\#{@request.query_string}" Fragments.put(fragment_name, @out) }, :only => actions ) end end private # Expire the cached fragment of the given actions. #-- # FIXME: not very good implementation? #++ def expire_action(*actions) return unless caching_enabled? for action in [actions].flatten expire_fragment(action) end end end end end # * George Moschovitis