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

Changeset 4656

Show
Ignore:
Timestamp:
08/03/06 23:59:38 (2 years ago)
Author:
marcel
Message:

Make action caching aware of different formats for the same action so that, e.g. foo.xml is cached separately from foo.html. Implicitly set content type when reading in cached content with mime revealing extensions so the entire onous isn't on the webserver. PDI MORE CACHING TESTS [Marcel Molina Jr.]

Files:

Legend:

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

    r4644 r4656  
    11*SVN* 
     2 
     3* Make action caching aware of different formats for the same action so that, e.g.  foo.xml is cached separately from foo.html. Implicitly set content type when reading in cached content with mime revealing extensions so the entire onous isn't on the webserver. [Marcel Molina Jr.] 
    24 
    35* Restrict Request Method hacking with ?_method to POST requests.  [Rick Olson] 
  • trunk/actionpack/lib/action_controller/caching.rb

    r4310 r4656  
    156156    # "david.somewhere.com/lists/show/1". This allows the cacher to differentiate between "david.somewhere.com/lists/" and 
    157157    # "jamis.somewhere.com/lists/" -- which is a helpful way of assisting the subdomain-as-account-key pattern. 
     158    # 
     159    # Different representations of the same resource, e.g. <tt>http://david.somewhere.com/lists</tt> and <tt>http://david.somewhere.com/lists.xml</tt> 
     160    # are treated like separate requests and are so are cached separately. Keep in mine when expiring an action cache that <tt>:action => 'lists'</tt> is not the same 
     161    # as <tt>:action => 'list', :format => :xml</tt>. 
    158162    module Actions 
    159163      def self.included(base) #:nodoc: 
     
    173177        if options[:action].is_a?(Array) 
    174178          options[:action].dup.each do |action| 
    175             expire_fragment(url_for(options.merge({ :action => action })).split("://").last
     179            expire_fragment(ActionCachePath.path_for(self, options.merge({ :action => action }))
    176180          end 
    177181        else 
    178           expire_fragment(url_for(options).split("://").last
    179         end 
    180       end 
    181  
    182       class ActionCacheFilter #:nodoc: 
    183         def initialize(*actions
     182          expire_fragment(ActionCachePath.path_for(self, options)
     183        end 
     184      end 
     185 
     186      class ActionCacheFilter #:nodoc:         
     187        def initialize(*actions, &block
    184188          @actions = actions 
    185189        end 
     
    187191        def before(controller) 
    188192          return unless @actions.include?(controller.action_name.intern) 
    189           if cache = controller.read_fragment(controller.url_for.split("://").last) 
     193          action_cache_path = ActionCachePath.new(controller) 
     194          if cache = controller.read_fragment(action_cache_path.path) 
    190195            controller.rendered_action_cache = true 
     196            set_content_type!(action_cache_path) 
    191197            controller.send(:render_text, cache) 
    192198            false 
     
    196202        def after(controller) 
    197203          return if !@actions.include?(controller.action_name.intern) || controller.rendered_action_cache 
    198           controller.write_fragment(controller.url_for.split("://").last, controller.response.body) 
    199         end 
     204          controller.write_fragment(ActionCachePath.path_for(controller), controller.response.body) 
     205        end 
     206         
     207        private 
     208           
     209          def set_content_type!(action_cache_path) 
     210            if extention = action_cache_path.extension 
     211              content_type = Mime::EXTENSION_LOOKUP[extention] 
     212              action_cache_path.controller.headers['Content-Type'] = content_type.to_s 
     213            end 
     214          end 
     215           
     216      end 
     217       
     218      class ActionCachePath 
     219        attr_reader :controller, :options 
     220         
     221        class << self 
     222          def path_for(*args, &block) 
     223            new(*args).path 
     224          end 
     225        end 
     226         
     227        def initialize(controller, options = {}) 
     228          @controller = controller 
     229          @options    = options 
     230        end 
     231         
     232        def path 
     233          return @path if @path 
     234          @path = controller.url_for(options).split('://').last 
     235          normalize! 
     236          add_extension! 
     237          URI.unescape(@path) 
     238        end 
     239         
     240        def extension 
     241          @extension ||= extract_extension(controller.request.path) 
     242        end 
     243         
     244        private 
     245          def normalize! 
     246            @path << 'index' if @path.last == '/' 
     247          end 
     248         
     249          def add_extension! 
     250            @path << ".#{extension}" if extension 
     251          end 
     252           
     253          def extract_extension(file_path) 
     254            # Don't want just what comes after the last '.' to accomodate multi part extensions 
     255            # such as tar.gz. 
     256            file_path[/^[^.]+\.(.+)$/, 1] 
     257          end 
    200258      end 
    201259    end 
  • trunk/actionpack/test/controller/base_test.rb

    r3668 r4656  
    2424end 
    2525class EmptyController < ActionController::Base 
    26   include ActionController::Caching 
    2726end 
    2827class NonEmptyController < ActionController::Base