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

Changeset 8415

Show
Ignore:
Timestamp:
12/16/07 16:56:42 (9 months ago)
Author:
david
Message:

Introduce cache_key management and expansion

Files:

Legend:

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

    r8414 r8415  
    5757      def cache(key, options = nil, &block) 
    5858        if cache_configured? 
    59           cache_store.fetch(key.to_param, options, &block) 
     59          cache_store.fetch(ActiveSupport::Cache.expand_cache_key(key, :controller), options, &block) 
    6060        else 
    6161          yield 
     
    6464 
    6565 
    66     private 
     66    private     
    6767      def cache_configured? 
    6868        self.class.cache_configured? 
  • branches/2-1-caching/actionpack/lib/action_controller/caching/fragments.rb

    r8414 r8415  
    5858        # value of url_for on that hash (without the protocol). 
    5959        def fragment_cache_key(key) 
    60           key.is_a?(Hash) ? url_for(key).split("://").last : key.to_param 
     60          ActiveSupport::Cache.expand_cache_key(key.is_a?(Hash) ? url_for(key).split("://").last : key, :fragments) 
    6161        end 
    6262 
     
    8282          key = fragment_cache_key(key) 
    8383 
    84           self.class.benchmark "Cached fragment: #{key}" do 
     84          self.class.benchmark "Cached fragment miss: #{key}" do 
    8585            cache_store.write(key, content, options) 
    8686          end 
     
    9595          key = fragment_cache_key(key) 
    9696 
    97           self.class.benchmark "Fragment read: #{key}" do 
     97          self.class.benchmark "Cached fragment hit: #{key}" do 
    9898            cache_store.read(key, options) 
    9999          end 
  • branches/2-1-caching/activerecord/lib/active_record/base.rb

    r8381 r8415  
    19481948        (id = self.id) ? id.to_s : nil # Be sure to stringify the id for routes 
    19491949      end 
     1950       
     1951      # Returns a cache key that can be used to identify this record. Examples: 
     1952      # 
     1953      #   Product.new.cache_key     # => "products/new" 
     1954      #   Product.find(5).cache_key # => "products/5" (updated_at not available) 
     1955      #   Person.find(5).cache_key  # => "people/5-20071224150000" (updated_at available) 
     1956      def cache_key 
     1957        case  
     1958        when new_record? 
     1959          "#{self.class.name.tableize}/new" 
     1960        when self[:updated_at] 
     1961          "#{self.class.name.tableize}/#{id}-#{updated_at.to_s(:number)}" 
     1962        else 
     1963          "#{self.class.name.tableize}/#{id}" 
     1964        end 
     1965      end 
    19501966 
    19511967      def id_before_type_cast #:nodoc: 
  • branches/2-1-caching/activesupport/lib/active_support/cache.rb

    r8394 r8415  
    1414        store 
    1515      end 
     16    end 
     17 
     18    def self.expand_cache_key(key, namespace = nil) 
     19      expanded_cache_key = namespace ? "#{namespace}/" : "" 
     20       
     21      if ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"] 
     22        expanded_cache_key << "#{ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"]}/"  
     23      end 
     24 
     25      expanded_cache_key << case 
     26      when key.respond_to?(:cache_key) 
     27        key.cache_key 
     28      when key.is_a?(Array) 
     29        key.collect { |element| expand_cache_key(element) }.to_param 
     30      when key.respond_to?(:to_param) 
     31        key.to_param 
     32      end 
     33 
     34      expanded_cache_key 
    1635    end 
    1736 
  • branches/2-1-caching/activesupport/lib/active_support/core_ext/date/conversions.rb

    r8279 r8415  
    88          :long         => "%B %e, %Y", 
    99          :db           => "%Y-%m-%d", 
     10          :number       => "%Y%m%d", 
    1011          :long_ordinal => lambda { |date| date.strftime("%B #{date.day.ordinalize}, %Y") }, # => "April 25th, 2007" 
    1112          :rfc822       => "%e %b %Y" 
  • branches/2-1-caching/activesupport/lib/active_support/core_ext/time/conversions.rb

    r8278 r8415  
    66        DATE_FORMATS = { 
    77          :db           => "%Y-%m-%d %H:%M:%S", 
     8          :number       => "%Y%m%d%H%M%S", 
    89          :time         => "%H:%M", 
    910          :short        => "%d %b %H:%M",