Class: ActiveRecord::ConnectionAdapters::QueryCache::Store
- Inherits:
-
Object
- Object
- ActiveRecord::ConnectionAdapters::QueryCache::Store
- Defined in:
- lib/active_record/connection_adapters/abstract/query_cache.rb
Overview
This is the actual query cache store.
It has an internal hash whose keys are either SQL strings, or arrays of two elements [SQL string, binds], if there are binds. The hash values are their corresponding ActiveRecord::Result objects.
Keeping the hash size under max size is achieved with LRU eviction.
The store gets passed a version object, which is shared among the query cache stores of a given connection pool (see ConnectionPoolConfiguration down below). The version value may be externally changed as a way to signal cache invalidation, that is why all methods have a guard for it.
Instance Attribute Summary collapse
-
#dirties ⇒ Object
(also: #dirties?)
:nodoc:.
-
#enabled ⇒ Object
(also: #enabled?)
:nodoc:.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #clear ⇒ Object
- #compute_if_absent(key) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(version, max_size) ⇒ Store
constructor
A new instance of Store.
- #size ⇒ Object
Constructor Details
#initialize(version, max_size) ⇒ Store
Returns a new instance of Store.
49 50 51 52 53 54 55 56 |
# File 'lib/active_record/connection_adapters/abstract/query_cache.rb', line 49 def initialize(version, max_size) @version = version @current_version = version.value @map = {} @max_size = max_size @enabled = false @dirties = true end |
Instance Attribute Details
#dirties ⇒ Object Also known as: dirties?
:nodoc:
45 46 47 |
# File 'lib/active_record/connection_adapters/abstract/query_cache.rb', line 45 def dirties @dirties end |
#enabled ⇒ Object Also known as: enabled?
:nodoc:
45 46 47 |
# File 'lib/active_record/connection_adapters/abstract/query_cache.rb', line 45 def enabled @enabled end |
Instance Method Details
#[](key) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/active_record/connection_adapters/abstract/query_cache.rb', line 68 def [](key) check_version return unless @enabled if entry = @map.delete(key) @map[key] = entry end end |
#clear ⇒ Object
93 94 95 96 |
# File 'lib/active_record/connection_adapters/abstract/query_cache.rb', line 93 def clear @map.clear self end |
#compute_if_absent(key) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/active_record/connection_adapters/abstract/query_cache.rb', line 77 def compute_if_absent(key) check_version return yield unless @enabled if entry = @map.delete(key) return @map[key] = entry end if @max_size && @map.size >= @max_size @map.shift # evict the oldest entry end @map[key] ||= yield end |
#empty? ⇒ Boolean
63 64 65 66 |
# File 'lib/active_record/connection_adapters/abstract/query_cache.rb', line 63 def empty? check_version @map.empty? end |
#size ⇒ Object
58 59 60 61 |
# File 'lib/active_record/connection_adapters/abstract/query_cache.rb', line 58 def size check_version @map.size end |