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

Inherits:
Object
  • Object
show all
Defined in:
activerecord/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.



51
52
53
54
55
56
57
58
# File 'activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb', line 51

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:



47
48
49
# File 'activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb', line 47

def dirties
  @dirties
end

#enabledObject Also known as: enabled?

:nodoc:



47
48
49
# File 'activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb', line 47

def enabled
  @enabled
end

Instance Method Details

#[](key) ⇒ Object



70
71
72
73
74
75
76
77
# File 'activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb', line 70

def [](key)
  check_version
  return unless @enabled

  if entry = @map.delete(key)
    @map[key] = entry
  end
end

#clearObject



95
96
97
98
# File 'activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb', line 95

def clear
  @map.clear
  self
end

#compute_if_absent(key) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb', line 79

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)


65
66
67
68
# File 'activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb', line 65

def empty?
  check_version
  @map.empty?
end

#sizeObject



60
61
62
63
# File 'activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb', line 60

def size
  check_version
  @map.size
end