Class: ActiveRecord::ConnectionAdapters::QueryCache::Store

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#dirtiesObject Also known as: dirties?

:nodoc:



45
46
47
# File 'lib/active_record/connection_adapters/abstract/query_cache.rb', line 45

def dirties
  @dirties
end

#enabledObject 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

#clearObject



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

Returns:

  • (Boolean)


63
64
65
66
# File 'lib/active_record/connection_adapters/abstract/query_cache.rb', line 63

def empty?
  check_version
  @map.empty?
end

#sizeObject



58
59
60
61
# File 'lib/active_record/connection_adapters/abstract/query_cache.rb', line 58

def size
  check_version
  @map.size
end