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

Ticket #8231: action_caching_speedup.2.patch

File action_caching_speedup.2.patch, 6.0 kB (added by skaes, 1 year ago)

patched version of stable patch

  • actionpack/test/controller/caching_test.rb

    old new  
    9797  caches_action :index 
    9898 
    9999  def index 
     100    sleep 0.01 
    100101    @cache_this = Time.now.to_f.to_s 
    101102    render :text => @cache_this 
    102103  end 
     
    195196  def test_xml_version_of_resource_is_treated_as_different_cache 
    196197    @mock_controller.mock_url_for = 'http://example.org/posts/' 
    197198    @mock_controller.mock_path    = '/posts/index.xml' 
    198     path_object = @path_class.new(@mock_controller
     199    path_object = @path_class.new(@mock_controller, {}
    199200    assert_equal 'xml', path_object.extension 
    200201    assert_equal 'example.org/posts/index.xml', path_object.path 
    201202  end 
     
    204205    @mock_controller.mock_url_for = 'http://example.org/' 
    205206    @mock_controller.mock_path    = '/' 
    206207 
    207     assert_equal 'example.org/index', @path_class.path_for(@mock_controller
     208    assert_equal 'example.org/index', @path_class.path_for(@mock_controller, {}
    208209  end 
    209210 
    210211  def test_file_extensions 
  • actionpack/lib/action_controller/caching.rb

    old new  
    11require 'fileutils' 
    22require 'uri' 
     3require 'set' 
    34 
    45module ActionController #:nodoc: 
    56  # Caching is a cheap way of speeding up slow applications by keeping the result of calculations, renderings, and database calls 
     
    163164    module Actions 
    164165      def self.included(base) #:nodoc: 
    165166        base.extend(ClassMethods) 
    166         base.send(:attr_accessor, :rendered_action_cache) 
     167        base.class_eval do 
     168          attr_accessor :rendered_action_cache, :action_cache_path 
     169          alias_method_chain :protected_instance_variables, :action_caching 
     170        end 
    167171      end 
    168172 
     173      def protected_instance_variables_with_action_caching 
     174        protected_instance_variables_without_action_caching + %w(@action_cache_path) 
     175      end 
     176 
    169177      module ClassMethods 
    170178        # Declares that +actions+ should be cached. 
    171179        # See ActionController::Caching::Actions for details. 
    172180        def caches_action(*actions) 
    173181          return unless perform_caching 
    174           around_filter(ActionCacheFilter.new(*actions)) 
     182          action_cache_filter = ActionCacheFilter.new(*actions) 
     183          before_filter action_cache_filter 
     184          after_filter action_cache_filter 
    175185        end 
    176186      end 
    177187 
     
    187197      end 
    188198 
    189199      class ActionCacheFilter #:nodoc: 
    190         def initialize(*actions, &block
    191           @actions = actions 
     200        def initialize(*actions
     201          @actions = Set.new actions 
    192202        end 
    193203 
    194204        def before(controller) 
    195           return unless @actions.include?(controller.action_name.intern
    196           action_cache_path = ActionCachePath.new(controller
    197           if cache = controller.read_fragment(action_cache_path.path) 
     205          return unless @actions.include?(controller.action_name.to_sym
     206          cache_path = ActionCachePath.new(controller, {}
     207          if cache = controller.read_fragment(cache_path.path) 
    198208            controller.rendered_action_cache = true 
    199             set_content_type!(action_cache_path
     209            set_content_type!(controller, cache_path.extension
    200210            controller.send(:render_text, cache) 
    201211            false 
     212          else 
     213            controller.action_cache_path = cache_path 
    202214          end 
    203215        end 
    204216 
    205217        def after(controller) 
    206           return if !@actions.include?(controller.action_name.intern) || controller.rendered_action_cache 
    207           controller.write_fragment(ActionCachePath.path_for(controller), controller.response.body) 
     218          return if !@actions.include?(controller.action_name.to_sym) || controller.rendered_action_cache 
     219          controller.write_fragment(controller.action_cache_path.path, controller.response.body) 
    208220        end 
    209221         
    210222        private 
    211            
    212           def set_content_type!(action_cache_path) 
    213             if extention = action_cache_path.extension 
    214               content_type = Mime::EXTENSION_LOOKUP[extention] 
    215               action_cache_path.controller.response.content_type = content_type.to_s 
    216             end 
     223          def set_content_type!(controller, extension) 
     224            controller.response.content_type = Mime::EXTENSION_LOOKUP[extension].to_s if extension 
    217225          end 
    218226           
    219227      end 
    220228       
    221229      class ActionCachePath 
    222         attr_reader :controller, :options 
     230        attr_reader :path, :extension 
    223231         
    224232        class << self 
    225           def path_for(*args, &block
    226             new(*args).path 
     233          def path_for(controller, options
     234            new(controller, options).path 
    227235          end 
    228236        end 
    229237         
    230238        def initialize(controller, options = {}) 
    231           @controller = controller 
    232           @options    = options 
     239          @extension = extract_extension(controller.request.path) 
     240          path = controller.url_for(options).split('://').last 
     241          normalize!(path) 
     242          add_extension!(path, @extension) 
     243          @path = URI.unescape(path) 
    233244        end 
    234245         
    235         def path 
    236           return @path if @path 
    237           @path = controller.url_for(options).split('://').last 
    238           normalize! 
    239           add_extension! 
    240           URI.unescape(@path) 
    241         end 
    242          
    243         def extension 
    244           @extension ||= extract_extension(controller.request.path) 
    245         end 
    246          
    247246        private 
    248           def normalize! 
    249             @path << 'index' if @path.last == '/' 
     247          def normalize!(path) 
     248            path << 'index' if path[-1] == ?/ 
    250249          end 
    251250         
    252           def add_extension! 
    253             @path << ".#{extension}" if extension 
     251          def add_extension!(path, extension) 
     252            path << ".#{extension}" if extension 
    254253          end 
    255254           
    256255          def extract_extension(file_path)