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

Changeset 6179

Show
Ignore:
Timestamp:
02/20/07 23:42:04 (2 years ago)
Author:
xal
Message:

You can now use cache in instance hierachies. This allows ActiveRecord::Base.cache { } usage to cache everything

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/lib/active_record/query_cache.rb

    r6138 r6179  
    6464        (Thread.current[:query_cache] ||= {}) 
    6565      end 
     66       
     67      def query_cache 
     68        if query_caches[self] 
     69          query_caches[self] 
     70        elsif superclass.respond_to?(:query_cache) 
     71          superclass.query_cache 
     72        end 
     73      end 
     74       
     75      def query_cache=(cache) 
     76        query_caches[self] = cache 
     77      end 
    6678             
    6779      def cache         
    68         query_caches[self] = QueryCache.new(connection
     80        self.query_cache = QueryCache.new(connection_without_query_cache
    6981        yield 
    7082      ensure  
    71         query_caches[self] = nil 
     83        self.query_cache = nil 
    7284      end         
    7385       
    7486      def connection 
    75         query_caches[self] || connection_without_query_cache 
     87        query_cache || connection_without_query_cache 
    7688      end 
    7789    end 
  • trunk/activerecord/test/query_cache_test.rb

    r6138 r6179  
    44require 'fixtures/task' 
    55 
    6  
    76class QueryCacheTest < Test::Unit::TestCase 
    87  fixtures :tasks 
    9  
    108   
    119  def test_find_queries 
     
    2422    end 
    2523  end 
     24   
     25  def test_query_cache_returned         
     26    assert_not_equal ActiveRecord::QueryCache, Task.connection.class 
     27    Task.cache do  
     28      assert_equal ActiveRecord::QueryCache, Task.connection.class       
     29    end     
     30  end 
     31   
    2632 
    2733  def test_cache_is_scoped_on_actual_class_only 
     
    3036    end 
    3137  end 
     38   
     39   
     40  def test_cache_is_scoped_on_all_descending_classes 
     41    ActiveRecord::Base.cache do 
     42      assert_queries(1) {  Task.find(1); Task.find(1) }     
     43    end 
     44  end 
     45   
     46   
    3247end 
     48 
    3349 
    3450uses_mocha('QueryCacheExpiryTest') do