class ArCache::WhereClause

Attributes

klass[R]
predicates[R]
table[R]

Public Class Methods

new(klass, predicates) click to toggle source
# File lib/ar_cache/where_clause.rb, line 7
def initialize(klass, predicates)
  @klass = klass
  @table = klass.ar_cache_table
  @predicates = predicates
end

Public Instance Methods

add_blank_primary_cache_key(key) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_hash() click to toggle source
# 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?() click to toggle source
# 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_keys() click to toggle source
# 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?() click to toggle source
# 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_keys() click to toggle source
# File lib/ar_cache/where_clause.rb, line 17
def invalid_keys
  @invalid_keys ||= []
end
missed_hash() click to toggle source
# 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_values() click to toggle source
# File lib/ar_cache/where_clause.rb, line 13
def missed_values
  @missed_values ||= []
end
primary_cache_keys() click to toggle source
# 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?() click to toggle source
# File lib/ar_cache/where_clause.rb, line 52
def primary_key_index?
  (@multi_values_key || @index.first) == table.primary_key
end
single?() click to toggle source
# File lib/ar_cache/where_clause.rb, line 48
def single?
  @multi_values_key.nil?
end