|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 | 3 | require "concurrent/map"
|
| 4 | +require "concurrent/atomic/atomic_fixnum" |
4 | 5 |
|
5 | 6 | module ActiveRecord
|
6 | 7 | module ConnectionAdapters # :nodoc:
|
@@ -35,9 +36,9 @@ class Store # :nodoc:
|
35 | 36 | alias_method :enabled?, :enabled
|
36 | 37 | alias_method :dirties?, :dirties
|
37 | 38 |
|
38 |
| - def initialize(pool, max_size) |
39 |
| - @pool = pool |
40 |
| - @version = pool.query_cache_version |
| 39 | + def initialize(version, max_size) |
| 40 | + @version = version |
| 41 | + @current_version = version.value |
41 | 42 | @map = {}
|
42 | 43 | @max_size = max_size
|
43 | 44 | @enabled = false
|
@@ -85,18 +86,17 @@ def clear
|
85 | 86 |
|
86 | 87 | private
|
87 | 88 | def check_version
|
88 |
| - version = @pool.query_cache_version |
89 |
| - if version != @version |
| 89 | + if @current_version != @version.value |
90 | 90 | @map.clear
|
91 |
| - @version = version |
| 91 | + @current_version = @version.value |
92 | 92 | end
|
93 | 93 | end
|
94 | 94 | end
|
95 | 95 |
|
96 | 96 | module ConnectionPoolConfiguration # :nodoc:
|
97 | 97 | def initialize(...)
|
98 | 98 | super
|
99 |
| - @query_cache_version = 0 |
| 99 | + @query_cache_version = Concurrent::AtomicFixnum.new |
100 | 100 | @query_cache_max_size = \
|
101 | 101 | case query_cache = db_config&.query_cache
|
102 | 102 | when 0, false
|
@@ -160,16 +160,14 @@ def clear_query_cache
|
160 | 160 | # With transactional fixtures, and especially systems test
|
161 | 161 | # another thread may use the same connection, but with a different
|
162 | 162 | # query cache. So we must clear them all.
|
163 |
| - synchronize do |
164 |
| - @query_cache_version += 1 |
165 |
| - end |
| 163 | + @query_cache_version.increment |
166 | 164 | end
|
167 | 165 | query_cache.clear
|
168 | 166 | end
|
169 | 167 |
|
170 | 168 | def query_cache
|
171 | 169 | key = :"active_record_query_cache_#{object_id}"
|
172 |
| - ActiveSupport::IsolatedExecutionState[key] ||= Store.new(self, @query_cache_max_size) |
| 170 | + ActiveSupport::IsolatedExecutionState[key] ||= Store.new(@query_cache_version, @query_cache_max_size) |
173 | 171 | end
|
174 | 172 | end
|
175 | 173 |
|
|
0 commit comments