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

Changeset 6189

Show
Ignore:
Timestamp:
02/21/07 18:08:39 (2 years ago)
Author:
xal
Message:

Enable active record cache automatically for all actions

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/lib/action_controller/caching.rb

    r5838 r6189  
    1111  module Caching 
    1212    def self.included(base) #:nodoc: 
    13       base.send(:include, Pages, Actions, Fragments, Sweeping) 
     13      base.send(:include, Pages, Actions, Fragments) 
     14      base.send(:include, Sweeping, SqlCache) if defined?(ActiveRecord) 
    1415 
    1516      base.class_eval do 
     
    613614      end 
    614615    end 
     616     
     617    if defined?(ActiveRecord)      
     618      module SqlCache 
     619        def self.included(base) #:nodoc: 
     620          base.alias_method_chain :perform_action, :caching 
     621        end 
     622         
     623        def perform_action_with_caching 
     624          ActiveRecord::Base.cache do 
     625            perform_action_without_caching 
     626          end 
     627        end 
     628      end 
     629    end 
     630     
    615631  end 
    616632end 
  • trunk/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

    r5955 r6189  
    102102        @connection 
    103103      end 
     104       
     105      def log_info(sql, name, runtime) 
     106        return unless @logger or !@logger.debug? 
     107 
     108        @logger.debug( 
     109          format_log_entry( 
     110            "#{name.nil? ? "SQL" : name} (#{sprintf("%f", runtime)})", 
     111            sql.gsub(/ +/, " ") 
     112          ) 
     113        ) 
     114      end       
    104115 
    105116      protected 
     
    129140        end 
    130141 
    131         def log_info(sql, name, runtime) 
    132           return unless @logger 
    133  
    134           @logger.debug( 
    135             format_log_entry( 
    136               "#{name.nil? ? "SQL" : name} (#{sprintf("%f", runtime)})", 
    137               sql.gsub(/ +/, " ") 
    138             ) 
    139           ) 
    140         end 
    141  
    142142        def format_log_entry(message, dump = nil) 
    143143          if ActiveRecord::Base.colorize_logging 
  • trunk/activerecord/lib/active_record/query_cache.rb

    r6179 r6189  
    1111 
    1212    def select_all(sql, name = nil) 
    13       (@query_cache[sql] ||= @connection.select_all(sql, name)).dup 
     13      cache(sql) { @connection.select_all(sql, name) } 
    1414    end 
    1515 
    1616    def select_one(sql, name = nil) 
    17       @query_cache[sql] ||= @connection.select_one(sql, name) 
     17      cache(sql) { @connection.select_one(sql, name) } 
    1818    end 
    1919     
    2020    def select_values(sql, name = nil) 
    21       (@query_cache[sql] ||= @connection.select_values(sql, name)).dup 
     21      cache(sql) { @connection.select_values(sql, name) } 
    2222    end 
    2323 
    2424    def select_value(sql, name = nil) 
    25       @query_cache[sql] ||= @connection.select_value(sql, name) 
     25      cache(sql) { @connection.select_value(sql, name) } 
    2626    end 
    2727     
     
    5151     
    5252    private 
     53     
     54      def cache(sql) 
     55        result = if @query_cache.has_key?(sql) 
     56          log_info(sql, "CACHE", 0.0) 
     57          @query_cache[sql] 
     58        else 
     59          @query_cache[sql] = yield 
     60        end 
     61         
     62        result ? result.dup : nil 
     63      end 
     64     
    5365      def method_missing(method, *arguments, &proc) 
    5466        @connection.send(method, *arguments, &proc)