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

Changeset 8414

Show
Ignore:
Timestamp:
12/16/07 00:12:27 (10 months ago)
Author:
david
Message:

Working improvements

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/2-1-caching/actionpack/lib/action_controller/caching.rb

    r8393 r8414  
    5353    end 
    5454 
    55     # Convenience accessor 
    56     def cache(key, options = nil, &block) 
    57       if cache_configured? 
    58         cache_store.fetch(key, options, &block) 
    59       else 
    60         yield 
     55    protected 
     56      # Convenience accessor 
     57      def cache(key, options = nil, &block) 
     58        if cache_configured? 
     59          cache_store.fetch(key.to_param, options, &block) 
     60        else 
     61          yield 
     62        end 
    6163      end 
    62     end 
    6364 
    64     def cache_configured? 
    65       self.class.cache_configured? 
    66     end 
     65 
     66    private 
     67      def cache_configured? 
     68        self.class.cache_configured? 
     69      end 
    6770  end 
    6871end 
  • branches/2-1-caching/actionpack/lib/action_controller/caching/actions.rb

    r8393 r8414  
    5555      end 
    5656 
    57       def protected_instance_variables_with_action_caching 
    58         protected_instance_variables_without_action_caching + %w(@action_cache_path) 
    59       end 
     57      protected 
     58        def protected_instance_variables_with_action_caching 
     59          protected_instance_variables_without_action_caching + %w(@action_cache_path) 
     60        end 
    6061 
    61       def expire_action(options = {}) 
    62         return unless cache_configured? 
     62        def expire_action(options = {}) 
     63          return unless cache_configured? 
    6364 
    64         if options[:action].is_a?(Array) 
    65           options[:action].dup.each do |action| 
    66             expire_fragment(ActionCachePath.path_for(self, options.merge({ :action => action }))) 
     65          if options[:action].is_a?(Array) 
     66            options[:action].dup.each do |action| 
     67              expire_fragment(ActionCachePath.path_for(self, options.merge({ :action => action }))) 
     68            end 
     69          else 
     70            expire_fragment(ActionCachePath.path_for(self, options)) 
    6771          end 
    68         else 
    69           expire_fragment(ActionCachePath.path_for(self, options)) 
    7072        end 
    71       end 
    7273 
    7374      class ActionCacheFilter #:nodoc: 
  • branches/2-1-caching/actionpack/lib/action_controller/caching/fragments.rb

    r8393 r8414  
    4040            end 
    4141          end 
     42 
     43          def fragment_cache_store=(store_option) #:nodoc: 
     44            ActiveSupport::Deprecation.warn('The fragment_cache_store= method is now use cache_store=') 
     45            self.cache_store = store_option 
     46          end 
     47         
     48          def fragment_cache_store #:nodoc: 
     49            ActiveSupport::Deprecation.warn('The fragment_cache_store method is now use cache_store') 
     50            cache_store 
     51          end 
    4252        end 
    4353      end 
    4454 
    45       # Given a name (as described in <tt>expire_fragment</tt>), returns a key suitable for use in reading,  
    46       # writing, or expiring a cached fragment. If the name is a hash, the generated name is the return 
    47       # value of url_for on that hash (without the protocol). 
    48       def fragment_cache_key(name) 
    49         name.is_a?(Hash) ? url_for(name).split("://").last : name 
    50       end 
    51  
    52       # Called by CacheHelper#cache 
    53       def cache_erb_fragment(block, name = {}, options = nil) 
    54         unless cache_configured? then block.call; return end 
    55  
    56         buffer = eval(ActionView::Base.erb_variable, block.binding) 
    57  
    58         if cache = read_fragment(name, options) 
    59           buffer.concat(cache) 
    60         else 
    61           pos = buffer.length 
    62           block.call 
    63           write_fragment(name, buffer[pos..-1], options) 
    64         end 
    65       end 
    66  
    67       # Writes <tt>content</tt> to the location signified by <tt>name</tt> (see <tt>expire_fragment</tt> for acceptable formats) 
    68       def write_fragment(name, content, options = nil) 
    69         return unless cache_configured? 
    70  
    71         key = fragment_cache_key(name) 
    72  
    73         self.class.benchmark "Cached fragment: #{key}" do 
    74           fragment_cache_store.write(key, content, options) 
     55      protected 
     56        # Given a key (as described in <tt>expire_fragment</tt>), returns a key suitable for use in reading,  
     57        # writing, or expiring a cached fragment. If the key is a hash, the generated key is the return 
     58        # value of url_for on that hash (without the protocol). 
     59        def fragment_cache_key(key) 
     60          key.is_a?(Hash) ? url_for(key).split("://").last : key.to_param 
    7561        end 
    7662 
    77         content 
    78       end 
     63        # Called by CacheHelper#cache 
     64        def cache_erb_fragment(block, key = {}, options = nil) 
     65          unless cache_configured? then block.call; return end 
    7966 
    80       # Reads a cached fragment from the location signified by <tt>name</tt> (see <tt>expire_fragment</tt> for acceptable formats) 
    81       def read_fragment(name, options = nil) 
    82         return unless cache_configured? 
     67          buffer = eval(ActionView::Base.erb_variable, block.binding) 
    8368 
    84         key = fragment_cache_key(name) 
    85  
    86         self.class.benchmark "Fragment read: #{key}" do 
    87           fragment_cache_store.read(key, options) 
    88         end 
    89       end 
    90  
    91       # Name can take one of three forms: 
    92       # * String: This would normally take the form of a path like "pages/45/notes" 
    93       # * Hash: Is treated as an implicit call to url_for, like { :controller => "pages", :action => "notes", :id => 45 } 
    94       # * Regexp: Will destroy all the matched fragments, example: 
    95       #     %r{pages/\d*/notes} 
    96       #   Ensure you do not specify start and finish in the regex (^$) because 
    97       #   the actual filename matched looks like ./cache/filename/path.cache 
    98       #   Regexp expiration is only supported on caches that can iterate over 
    99       #   all keys (unlike memcached). 
    100       def expire_fragment(name, options = nil) 
    101         return unless cache_configured? 
    102  
    103         key = fragment_cache_key(name) 
    104  
    105         if key.is_a?(Regexp) 
    106           self.class.benchmark "Expired fragments matching: #{key.source}" do 
    107             fragment_cache_store.delete_matched(key, options) 
    108           end 
    109         else 
    110           self.class.benchmark "Expired fragment: #{key}" do 
    111             fragment_cache_store.delete(key, options) 
     69          if cache = read_fragment(key, options) 
     70            buffer.concat(cache) 
     71          else 
     72            pos = buffer.length 
     73            block.call 
     74            write_fragment(key, buffer[pos..-1], options) 
    11275          end 
    11376        end 
    114       end 
     77 
     78        # Writes <tt>content</tt> to the location signified by <tt>key</tt> (see <tt>expire_fragment</tt> for acceptable formats) 
     79        def write_fragment(key, content, options = nil) 
     80          return unless cache_configured? 
     81 
     82          key = fragment_cache_key(key) 
     83 
     84          self.class.benchmark "Cached fragment: #{key}" do 
     85            cache_store.write(key, content, options) 
     86          end 
     87 
     88          content 
     89        end 
     90 
     91        # Reads a cached fragment from the location signified by <tt>key</tt> (see <tt>expire_fragment</tt> for acceptable formats) 
     92        def read_fragment(key, options = nil) 
     93          return unless cache_configured? 
     94 
     95          key = fragment_cache_key(key) 
     96 
     97          self.class.benchmark "Fragment read: #{key}" do 
     98            cache_store.read(key, options) 
     99          end 
     100        end 
     101 
     102        # Name can take one of three forms: 
     103        # * String: This would normally take the form of a path like "pages/45/notes" 
     104        # * Hash: Is treated as an implicit call to url_for, like { :controller => "pages", :action => "notes", :id => 45 } 
     105        # * Regexp: Will destroy all the matched fragments, example: 
     106        #     %r{pages/\d*/notes} 
     107        #   Ensure you do not specify start and finish in the regex (^$) because 
     108        #   the actual filename matched looks like ./cache/filename/path.cache 
     109        #   Regexp expiration is only supported on caches that can iterate over 
     110        #   all keys (unlike memcached). 
     111        def expire_fragment(key, options = nil) 
     112          return unless cache_configured? 
     113 
     114          key = fragment_cache_key(key) 
     115 
     116          if key.is_a?(Regexp) 
     117            self.class.benchmark "Expired fragments matching: #{key.source}" do 
     118              cache_store.delete_matched(key, options) 
     119            end 
     120          else 
     121            self.class.benchmark "Expired fragment: #{key}" do 
     122              cache_store.delete(key, options) 
     123            end 
     124          end 
     125        end 
    115126    end 
    116127  end 
  • branches/2-1-caching/actionpack/lib/action_controller/caching/pages.rb

    r8393 r8414  
    9797      end 
    9898 
    99       # Expires the page that was cached with the +options+ as a key. Example: 
    100       #   expire_page :controller => "lists", :action => "show" 
    101       def expire_page(options = {}) 
    102         return unless perform_caching 
     99      protected 
     100        # Expires the page that was cached with the +options+ as a key. Example: 
     101        #   expire_page :controller => "lists", :action => "show" 
     102        def expire_page(options = {}) 
     103          return unless perform_caching 
    103104 
    104         if options.is_a?(Hash) 
    105           if options[:action].is_a?(Array) 
    106             options[:action].dup.each do |action| 
    107               self.class.expire_page(url_for(options.merge(:only_path => true, :skip_relative_url_root => true, :action => action))) 
     105          if options.is_a?(Hash) 
     106            if options[:action].is_a?(Array) 
     107              options[:action].dup.each do |action| 
     108                self.class.expire_page(url_for(options.merge(:only_path => true, :skip_relative_url_root => true, :action => action))) 
     109              end 
     110            else 
     111              self.class.expire_page(url_for(options.merge(:only_path => true, :skip_relative_url_root => true))) 
    108112            end 
    109113          else 
    110             self.class.expire_page(url_for(options.merge(:only_path => true, :skip_relative_url_root => true))
     114            self.class.expire_page(options
    111115          end 
    112         else 
    113           self.class.expire_page(options) 
    114         end 
    115       end 
    116  
    117       # Manually cache the +content+ in the key determined by +options+. If no content is provided, the contents of response.body is used 
    118       # If no options are provided, the requested url is used. Example: 
    119       #   cache_page "I'm the cached content", :controller => "lists", :action => "show" 
    120       def cache_page(content = nil, options = nil) 
    121         return unless perform_caching && caching_allowed 
    122  
    123         path = case options 
    124           when Hash 
    125             url_for(options.merge(:only_path => true, :skip_relative_url_root => true, :format => params[:format])) 
    126           when String 
    127             options 
    128           else 
    129             request.path 
    130116        end 
    131117 
    132         self.class.cache_page(content || response.body, path) 
    133       end 
     118        # Manually cache the +content+ in the key determined by +options+. If no content is provided, the contents of response.body is used 
     119        # If no options are provided, the requested url is used. Example: 
     120        #   cache_page "I'm the cached content", :controller => "lists", :action => "show" 
     121        def cache_page(content = nil, options = nil) 
     122          return unless perform_caching && caching_allowed 
     123 
     124          path = case options 
     125            when Hash 
     126              url_for(options.merge(:only_path => true, :skip_relative_url_root => true, :format => params[:format])) 
     127            when String 
     128              options 
     129            else 
     130              request.path 
     131          end 
     132 
     133          self.class.cache_page(content || response.body, path) 
     134        end 
    134135 
    135136      private 
  • branches/2-1-caching/actionpack/lib/action_controller/caching/sql_cache.rb

    r8393 r8414  
    88      end 
    99 
    10       def perform_action_with_caching 
    11         ActiveRecord::Base.cache do 
    12           perform_action_without_caching 
     10      protected 
     11        def perform_action_with_caching 
     12          ActiveRecord::Base.cache do 
     13            perform_action_without_caching 
     14          end 
    1315        end 
    14       end 
    1516    end 
    1617  end 
  • branches/2-1-caching/actionpack/lib/action_view/helpers/cache_helper.rb

    r8393 r8414  
    3333      #    <% end %> 
    3434      def cache(name = {}, options = nil, &block) 
    35         @controller.cache_erb_fragment(block, name, options) 
     35        @controller.send!(:cache_erb_fragment, block, name, options) 
    3636      end 
    3737    end 
  • branches/2-1-caching/activesupport/lib/active_support/core_ext/hash/conversions.rb

    r8343 r8414  
    9696          end.sort * '&' 
    9797        end 
     98         
     99        alias_method :to_param, :to_query 
    98100 
    99101        def to_xml(options = {})