Class: ArCache::WhereClause

Inherits:
Object
  • Object
show all
Includes:
Raw
Defined in:
lib/ar_cache/where_clause.rb

Defined Under Namespace

Modules: Raw

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Raw

#where_values_hash

Constructor Details

#initialize(klass, predicates) ⇒ WhereClause

Returns a new instance of WhereClause.



7
8
9
10
11
# File 'lib/ar_cache/where_clause.rb', line 7

def initialize(klass, predicates)
  @klass = klass
  @table = klass.ar_cache_table
  @predicates = predicates
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



5
6
7
# File 'lib/ar_cache/where_clause.rb', line 5

def klass
  @klass
end

#predicatesObject (readonly)

Returns the value of attribute predicates.



5
6
7
# File 'lib/ar_cache/where_clause.rb', line 5

def predicates
  @predicates
end

#tableObject (readonly)

Returns the value of attribute table.



5
6
7
# File 'lib/ar_cache/where_clause.rb', line 5

def table
  @table
end

Instance Method Details

#add_blank_primary_cache_key(key) ⇒ Object



94
95
96
# File 'lib/ar_cache/where_clause.rb', line 94

def add_blank_primary_cache_key(key)
  invalid_keys << key
end

#add_invalid_second_cache_key(key) ⇒ Object



98
99
100
101
# File 'lib/ar_cache/where_clause.rb', line 98

def add_invalid_second_cache_key(key)
  # invalid_keys << key # The primary key index is reliable.
  invalid_keys << cache_hash[key] unless primary_key_index?
end

#add_missed_values(key) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/ar_cache/where_clause.rb', line 86

def add_missed_values(key)
  if primary_key_index?
    missed_values << cache_hash[key]
  else
    missed_values << @original_cache_hash[cache_hash[key]]
  end
end

#cache_hashObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ar_cache/where_clause.rb', line 56

def cache_hash
  return @cache_hash if defined?(@cache_hash)

  @cache_hash = {}
  multi_values_key = @multi_values_key || @index.first

  Array.wrap(where_values_hash[multi_values_key]).each do |v|
    @cache_hash[table.cache_key(where_values_hash, @index, multi_values_key, v)] = v
  end

  return @cache_hash if primary_key_index?

  @original_cache_hash = @cache_hash
  @cache_hash = ArCache.read_multi(*@cache_hash.keys, raw: true)
  @original_cache_hash.each { |k, v| missed_values << v unless @cache_hash.key?(k) }
  @cache_hash = @cache_hash.invert

  @cache_hash
end

#cacheable?Boolean

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/ar_cache/where_clause.rb', line 21

def cacheable?
  return @cacheable if defined?(@cacheable)

  @cacheable = predicates.any? && where_values_hash.length == predicates.length && hit_unique_index?
end

#delete_invalid_keysObject



103
104
105
# File 'lib/ar_cache/where_clause.rb', line 103

def delete_invalid_keys
  ArCache.delete_multi(invalid_keys) if invalid_keys.any?
end

#hit_unique_index?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ar_cache/where_clause.rb', line 27

def hit_unique_index?
  table.unique_indexes.each do |index|
    @index = index
    @multi_values_key = nil
    count = 0

    bool = index.all? do |column|
      (Thread.current[:ar_cache_reflection] ? where_values_hash.key?(column) : where_values_hash[column]).tap do
        if where_values_hash[column].is_a?(Array)
          @multi_values_key = column
          count += 1
        end
      end
    end

    return true if bool && count < 2
  end

  false
end

#invalid_keysObject



17
18
19
# File 'lib/ar_cache/where_clause.rb', line 17

def invalid_keys
  @invalid_keys ||= []
end

#missed_hashObject



82
83
84
# File 'lib/ar_cache/where_clause.rb', line 82

def missed_hash
  @missed_hash ||= missed_values.empty? ? {} : { (@multi_values_key || @index.first) => missed_values }
end

#missed_valuesObject



13
14
15
# File 'lib/ar_cache/where_clause.rb', line 13

def missed_values
  @missed_values ||= []
end

#primary_cache_keysObject



76
77
78
79
80
# File 'lib/ar_cache/where_clause.rb', line 76

def primary_cache_keys
  raise 'Does not detect primary key index' unless primary_key_index?

  @primary_cache_keys ||= Array(where_values_hash[table.primary_key]).map { |v| table.primary_cache_key(v) }
end

#primary_key_index?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/ar_cache/where_clause.rb', line 52

def primary_key_index?
  (@multi_values_key || @index.first) == table.primary_key
end

#single?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/ar_cache/where_clause.rb', line 48

def single?
  @multi_values_key.nil?
end