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

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/connection_adapters/abstract/query_cache.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size) ⇒ Store

Returns a new instance of Store.



38
39
40
41
42
43
# File 'lib/active_record/connection_adapters/abstract/query_cache.rb', line 38

def initialize(max_size)
  @map = {}
  @max_size = max_size
  @enabled = false
  @dirties = true
end

Instance Attribute Details

#dirtiesObject Also known as: dirties?

Returns the value of attribute dirties.



34
35
36
# File 'lib/active_record/connection_adapters/abstract/query_cache.rb', line 34

def dirties
  @dirties
end

#enabledObject Also known as: enabled?

Returns the value of attribute enabled.



34
35
36
# File 'lib/active_record/connection_adapters/abstract/query_cache.rb', line 34

def enabled
  @enabled
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  return unless @enabled

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

#clearObject



75
76
77
78
# File 'lib/active_record/connection_adapters/abstract/query_cache.rb', line 75

def clear
  @map.clear
  self
end

#compute_if_absent(key) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/active_record/connection_adapters/abstract/query_cache.rb', line 61

def compute_if_absent(key)
  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)


49
50
51
# File 'lib/active_record/connection_adapters/abstract/query_cache.rb', line 49

def empty?
  @map.empty?
end

#sizeObject



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

def size
  @map.size
end