public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Make query-cache thread-local

Signed-off-by: Joshua Peek <josh@joshpeek.com>
Nick Sieger (author)
Fri Aug 29 08:19:26 -0700 2008
josh (committer)
Fri Aug 29 08:21:39 -0700 2008
commit  a9086b3daa0a5174ba2118a2131eb5121f32d41b
tree    e06db5b9612b7b91ed33e691865761aca60c872c
parent  6769d824f9c20812a0e6cd6e0c19742984b2450e
...
4
5
6
7
8
9
10
...
16
17
18
19
 
20
21
22
...
26
27
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
30
31
32
 
 
33
34
35
36
 
37
38
39
40
41
 
42
43
44
 
45
46
47
...
51
52
53
54
 
55
56
57
58
 
59
60
61
...
63
64
65
66
67
 
 
68
69
70
...
73
74
75
76
 
77
78
 
79
80
 
81
82
83
...
4
5
6
 
7
8
9
...
15
16
17
 
18
19
20
21
...
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 
 
46
47
48
49
50
 
51
52
53
54
55
 
56
57
58
 
59
60
61
62
...
66
67
68
 
69
70
71
72
 
73
74
75
76
...
78
79
80
 
 
81
82
83
84
85
...
88
89
90
 
91
92
 
93
94
 
95
96
97
98
0
@@ -4,7 +4,6 @@ module ActiveRecord
0
       class << self
0
         def included(base)
0
           base.class_eval do
0
-            attr_accessor :query_cache_enabled
0
             alias_method_chain :columns, :query_cache
0
             alias_method_chain :select_all, :query_cache
0
           end
0
@@ -16,7 +15,7 @@ module ActiveRecord
0
           method_names.each do |method_name|
0
             base.class_eval <<-end_code, __FILE__, __LINE__
0
               def #{method_name}_with_query_dirty(*args)
0
-                clear_query_cache if @query_cache_enabled
0
+                clear_query_cache if query_cache_enabled
0
                 #{method_name}_without_query_dirty(*args)
0
               end
0
 
0
@@ -26,22 +25,38 @@ module ActiveRecord
0
         end
0
       end
0
 
0
+      def query_cache_enabled
0
+        Thread.current['query_cache_enabled']
0
+      end
0
+
0
+      def query_cache_enabled=(flag)
0
+        Thread.current['query_cache_enabled'] = flag
0
+      end
0
+
0
+      def query_cache
0
+        Thread.current['query_cache']
0
+      end
0
+
0
+      def query_cache=(cache)
0
+        Thread.current['query_cache'] = cache
0
+      end
0
+
0
       # Enable the query cache within the block.
0
       def cache
0
-        old, @query_cache_enabled = @query_cache_enabled, true
0
-        @query_cache ||= {}
0
+        old, self.query_cache_enabled = query_cache_enabled, true
0
+        self.query_cache ||= {}
0
         yield
0
       ensure
0
         clear_query_cache
0
-        @query_cache_enabled = old
0
+        self.query_cache_enabled = old
0
       end
0
 
0
       # Disable the query cache within the block.
0
       def uncached
0
-        old, @query_cache_enabled = @query_cache_enabled, false
0
+        old, self.query_cache_enabled = query_cache_enabled, false
0
         yield
0
       ensure
0
-        @query_cache_enabled = old
0
+        self.query_cache_enabled = old
0
       end
0
 
0
       # Clears the query cache.
0
@@ -51,11 +66,11 @@ module ActiveRecord
0
       # the same SQL query and repeatedly return the same result each time, silently
0
       # undermining the randomness you were expecting.
0
       def clear_query_cache
0
-        @query_cache.clear if @query_cache
0
+        query_cache.clear if query_cache
0
       end
0
 
0
       def select_all_with_query_cache(*args)
0
-        if @query_cache_enabled
0
+        if query_cache_enabled
0
           cache_sql(args.first) { select_all_without_query_cache(*args) }
0
         else
0
           select_all_without_query_cache(*args)
0
@@ -63,8 +78,8 @@ module ActiveRecord
0
       end
0
 
0
       def columns_with_query_cache(*args)
0
-        if @query_cache_enabled
0
-          @query_cache["SHOW FIELDS FROM #{args.first}"] ||= columns_without_query_cache(*args)
0
+        if query_cache_enabled
0
+          query_cache["SHOW FIELDS FROM #{args.first}"] ||= columns_without_query_cache(*args)
0
         else
0
           columns_without_query_cache(*args)
0
         end
0
@@ -73,11 +88,11 @@ module ActiveRecord
0
       private
0
         def cache_sql(sql)
0
           result =
0
-            if @query_cache.has_key?(sql)
0
+            if query_cache.has_key?(sql)
0
               log_info(sql, "CACHE", 0.0)
0
-              @query_cache[sql]
0
+              query_cache[sql]
0
             else
0
-              @query_cache[sql] = yield
0
+              query_cache[sql] = yield
0
             end
0
 
0
           if Array === result

Comments