Changeset 6867
- Timestamp:
- 05/27/07 07:38:09 (1 year ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_controller/caching.rb (modified) (6 diffs)
- trunk/actionpack/test/controller/caching_test.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r6865 r6867 1 1 *SVN* 2 3 * Action Caching speedup. #8231 [skaes] 2 4 3 5 * Wordsmith resources documentation. #8484 [marclove] trunk/actionpack/lib/action_controller/caching.rb
r6787 r6867 1 1 require 'fileutils' 2 2 require 'uri' 3 require 'set' 3 4 4 5 module ActionController #:nodoc: … … 178 179 def self.included(base) #:nodoc: 179 180 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 181 185 end 182 186 … … 190 194 end 191 195 196 def protected_instance_variables_with_action_caching 197 protected_instance_variables_without_action_caching + %w(@action_cache_path) 198 end 199 192 200 def expire_action(options = {}) 193 201 return unless perform_caching … … 203 211 class ActionCacheFilter #:nodoc: 204 212 def initialize(*actions, &block) 205 @ actions = actions206 @ options = @actions.last.is_a?(Hash) ? @actions.pop : {}213 @options = actions.last.is_a?(Hash) ? actions.pop : {} 214 @actions = Set.new actions 207 215 end 208 216 209 217 def before(controller) 210 218 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) 213 221 controller.rendered_action_cache = true 214 set_content_type!( action_cache_path)222 set_content_type!(controller, cache_path) 215 223 controller.send(:render_text, cache) 216 224 false 225 else 226 controller.action_cache_path = cache_path 217 227 end 218 228 end … … 220 230 def after(controller) 221 231 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) 223 233 end 224 234 225 235 private 226 236 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 232 239 end 233 240 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) || {} 236 243 end 237 244 … … 239 246 240 247 class ActionCachePath 241 attr_reader : controller, :options248 attr_reader :path, :extension 242 249 243 250 class << self 244 def path_for( *args, &block)245 new( *args).path251 def path_for(controller, options) 252 new(controller, options).path 246 253 end 247 254 end 248 255 249 256 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) 264 262 end 265 263 266 264 private 267 def normalize! 268 @path << 'index' if @path.last == '/'265 def normalize!(path) 266 path << 'index' if path[-1] == ?/ 269 267 end 270 268 271 def add_extension! 272 @path << ".#{extension}" if extension269 def add_extension!(path, extension) 270 path << ".#{extension}" if extension 273 271 end 274 272 trunk/actionpack/test/controller/caching_test.rb
r6453 r6867 225 225 @mock_controller.mock_url_for = 'http://example.org/posts/' 226 226 @mock_controller.mock_path = '/posts/index.xml' 227 path_object = @path_class.new(@mock_controller )227 path_object = @path_class.new(@mock_controller, {}) 228 228 assert_equal 'xml', path_object.extension 229 229 assert_equal 'example.org/posts/index.xml', path_object.path … … 234 234 @mock_controller.mock_path = '/' 235 235 236 assert_equal 'example.org/index', @path_class.path_for(@mock_controller )236 assert_equal 'example.org/index', @path_class.path_for(@mock_controller, {}) 237 237 end 238 238