Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6867

Show
Ignore:
Timestamp:
05/27/07 07:38:09 (1 year ago)
Author:
rick
Message:

Action Caching speedup. #8231 [skaes]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r6865 r6867  
    11*SVN* 
     2 
     3* Action Caching speedup.  #8231 [skaes] 
    24 
    35* Wordsmith resources documentation.  #8484 [marclove] 
  • trunk/actionpack/lib/action_controller/caching.rb

    r6787 r6867  
    11require 'fileutils' 
    22require 'uri' 
     3require 'set' 
    34 
    45module ActionController #:nodoc: 
     
    178179      def self.included(base) #:nodoc: 
    179180        base.extend(ClassMethods) 
    180         base.send(:attr_accessor, :rendered_action_cache) 
     181          base.class_eval do 
     182            attr_accessor :rendered_action_cache, :action_cache_path 
     183            alias_method_chain :protected_instance_variables, :action_caching 
     184          end 
    181185      end 
    182186 
     
    190194      end 
    191195 
     196      def protected_instance_variables_with_action_caching 
     197        protected_instance_variables_without_action_caching + %w(@action_cache_path) 
     198      end 
     199 
    192200      def expire_action(options = {}) 
    193201        return unless perform_caching 
     
    203211      class ActionCacheFilter #:nodoc: 
    204212        def initialize(*actions, &block) 
    205           @actions = actions 
    206           @options = @actions.last.is_a?(Hash) ? @actions.pop : {} 
     213          @options = actions.last.is_a?(Hash) ? actions.pop : {} 
     214          @actions = Set.new actions 
    207215        end 
    208216 
    209217        def before(controller) 
    210218          return unless @actions.include?(controller.action_name.intern) 
    211           action_cache_path = ActionCachePath.new(controller, path_options_for(controller)) 
    212           if cache = controller.read_fragment(action_cache_path.path) 
     219          cache_path = ActionCachePath.new(controller, path_options_for(controller, @options)) 
     220          if cache = controller.read_fragment(cache_path.path) 
    213221            controller.rendered_action_cache = true 
    214             set_content_type!(action_cache_path) 
     222            set_content_type!(controller, cache_path) 
    215223            controller.send(:render_text, cache) 
    216224            false 
     225          else 
     226            controller.action_cache_path = cache_path 
    217227          end 
    218228        end 
     
    220230        def after(controller) 
    221231          return if !@actions.include?(controller.action_name.intern) || controller.rendered_action_cache 
    222           controller.write_fragment(ActionCachePath.path_for(controller, path_options_for(controller)), controller.response.body) 
     232          controller.write_fragment(controller.action_cache_path.path, controller.response.body) 
    223233        end 
    224234         
    225235        private 
    226236           
    227           def set_content_type!(action_cache_path) 
    228             if extention = action_cache_path.extension 
    229               content_type = Mime::EXTENSION_LOOKUP[extention] 
    230               action_cache_path.controller.response.content_type = content_type.to_s 
    231             end 
     237          def set_content_type!(controller, extension) 
     238            controller.response.content_type = Mime::EXTENSION_LOOKUP[extension].to_s if extension 
    232239          end 
    233240           
    234           def path_options_for(controller
    235             (@options[:cache_path].respond_to?(:call) ? @options[:cache_path].call(controller) : @options[:cache_path]) || {} 
     241          def path_options_for(controller, options
     242            ((path_options = options[:cache_path]).respond_to?(:call) ? path_options.call(controller) : path_options) || {} 
    236243          end 
    237244           
     
    239246       
    240247      class ActionCachePath 
    241         attr_reader :controller, :options 
     248        attr_reader :path, :extension 
    242249         
    243250        class << self 
    244           def path_for(*args, &block
    245             new(*args).path 
     251          def path_for(controller, options
     252            new(controller, options).path 
    246253          end 
    247254        end 
    248255         
    249256        def initialize(controller, options = {}) 
    250           @controller = controller 
    251           @options    = options 
    252         end 
    253          
    254         def path 
    255           return @path if @path 
    256           @path = controller.url_for(options).split('://').last 
    257           normalize! 
    258           add_extension! 
    259           URI.unescape(@path) 
    260         end 
    261          
    262         def extension 
    263           @extension ||= extract_extension(controller.request.path) 
     257          @extension = extract_extension(controller.request.path) 
     258          path = controller.url_for(options).split('://').last 
     259          normalize!(path) 
     260          add_extension!(path, @extension) 
     261          @path = URI.unescape(path) 
    264262        end 
    265263         
    266264        private 
    267           def normalize! 
    268             @path << 'index' if @path.last == '/' 
     265          def normalize!(path) 
     266            path << 'index' if path[-1] == ?/ 
    269267          end 
    270268         
    271           def add_extension! 
    272             @path << ".#{extension}" if extension 
     269          def add_extension!(path, extension) 
     270            path << ".#{extension}" if extension 
    273271          end 
    274272           
  • trunk/actionpack/test/controller/caching_test.rb

    r6453 r6867  
    225225    @mock_controller.mock_url_for = 'http://example.org/posts/' 
    226226    @mock_controller.mock_path    = '/posts/index.xml' 
    227     path_object = @path_class.new(@mock_controller
     227    path_object = @path_class.new(@mock_controller, {}
    228228    assert_equal 'xml', path_object.extension 
    229229    assert_equal 'example.org/posts/index.xml', path_object.path 
     
    234234    @mock_controller.mock_path    = '/' 
    235235 
    236     assert_equal 'example.org/index', @path_class.path_for(@mock_controller
     236    assert_equal 'example.org/index', @path_class.path_for(@mock_controller, {}
    237237  end 
    238238