Sha256: 23a6cd733639a0c3b31b19a5731f6e133f8c0814735b85d29806274440aa597e

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

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  <gm@navel.gr>

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nitro-0.30.0 lib/nitro/caching/actions.rb
nitro-0.31.0 lib/nitro/caching/actions.rb