Ticket #8231: action_caching_speedup.2.patch
| File action_caching_speedup.2.patch, 6.0 kB (added by skaes, 1 year ago) |
|---|
-
actionpack/test/controller/caching_test.rb
old new 97 97 caches_action :index 98 98 99 99 def index 100 sleep 0.01 100 101 @cache_this = Time.now.to_f.to_s 101 102 render :text => @cache_this 102 103 end … … 195 196 def test_xml_version_of_resource_is_treated_as_different_cache 196 197 @mock_controller.mock_url_for = 'http://example.org/posts/' 197 198 @mock_controller.mock_path = '/posts/index.xml' 198 path_object = @path_class.new(@mock_controller )199 path_object = @path_class.new(@mock_controller, {}) 199 200 assert_equal 'xml', path_object.extension 200 201 assert_equal 'example.org/posts/index.xml', path_object.path 201 202 end … … 204 205 @mock_controller.mock_url_for = 'http://example.org/' 205 206 @mock_controller.mock_path = '/' 206 207 207 assert_equal 'example.org/index', @path_class.path_for(@mock_controller )208 assert_equal 'example.org/index', @path_class.path_for(@mock_controller, {}) 208 209 end 209 210 210 211 def test_file_extensions -
actionpack/lib/action_controller/caching.rb
old new 1 1 require 'fileutils' 2 2 require 'uri' 3 require 'set' 3 4 4 5 module ActionController #:nodoc: 5 6 # Caching is a cheap way of speeding up slow applications by keeping the result of calculations, renderings, and database calls … … 163 164 module Actions 164 165 def self.included(base) #:nodoc: 165 166 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 167 171 end 168 172 173 def protected_instance_variables_with_action_caching 174 protected_instance_variables_without_action_caching + %w(@action_cache_path) 175 end 176 169 177 module ClassMethods 170 178 # Declares that +actions+ should be cached. 171 179 # See ActionController::Caching::Actions for details. 172 180 def caches_action(*actions) 173 181 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 175 185 end 176 186 end 177 187 … … 187 197 end 188 198 189 199 class ActionCacheFilter #:nodoc: 190 def initialize(*actions , &block)191 @actions = actions200 def initialize(*actions) 201 @actions = Set.new actions 192 202 end 193 203 194 204 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) 198 208 controller.rendered_action_cache = true 199 set_content_type!( action_cache_path)209 set_content_type!(controller, cache_path.extension) 200 210 controller.send(:render_text, cache) 201 211 false 212 else 213 controller.action_cache_path = cache_path 202 214 end 203 215 end 204 216 205 217 def after(controller) 206 return if !@actions.include?(controller.action_name. intern) || controller.rendered_action_cache207 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) 208 220 end 209 221 210 222 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 217 225 end 218 226 219 227 end 220 228 221 229 class ActionCachePath 222 attr_reader : controller, :options230 attr_reader :path, :extension 223 231 224 232 class << self 225 def path_for( *args, &block)226 new( *args).path233 def path_for(controller, options) 234 new(controller, options).path 227 235 end 228 236 end 229 237 230 238 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) 233 244 end 234 245 235 def path236 return @path if @path237 @path = controller.url_for(options).split('://').last238 normalize!239 add_extension!240 URI.unescape(@path)241 end242 243 def extension244 @extension ||= extract_extension(controller.request.path)245 end246 247 246 private 248 def normalize! 249 @path << 'index' if @path.last == '/'247 def normalize!(path) 248 path << 'index' if path[-1] == ?/ 250 249 end 251 250 252 def add_extension! 253 @path << ".#{extension}" if extension251 def add_extension!(path, extension) 252 path << ".#{extension}" if extension 254 253 end 255 254 256 255 def extract_extension(file_path)