Class: ArCache::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/ar_cache/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation) ⇒ Query

Returns a new instance of Query.



9
10
11
12
13
# File 'lib/ar_cache/query.rb', line 9

def initialize(relation)
  @relation = relation
  @table = @relation.klass.ar_cache_table
  @where_clause = ArCache::WhereClause.new(@relation.klass, @relation.where_clause.send(:predicates))
end

Instance Attribute Details

#relationObject (readonly)

Returns the value of attribute relation.



7
8
9
# File 'lib/ar_cache/query.rb', line 7

def relation
  @relation
end

#tableObject (readonly)

Returns the value of attribute table.



7
8
9
# File 'lib/ar_cache/query.rb', line 7

def table
  @table
end

#where_clauseObject (readonly)

Returns the value of attribute where_clause.



7
8
9
# File 'lib/ar_cache/query.rb', line 7

def where_clause
  @where_clause
end

Instance Method Details

#exec_queries(&block) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ar_cache/query.rb', line 15

def exec_queries(&block) # rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
  return [] if relation.where_clause.contradiction?
  return ArCache.skip_cache { relation.send(:exec_queries, &block) } unless exec_queries_cacheable?

  records = table.read(where_clause, @select_values, &block)

  if where_clause.missed_hash.any?
    missed_relation = relation.rewhere(where_clause.missed_hash).reselect('*')
    missed_relation = missed_relation.lock(lock_statement) unless cache_lock?
    missed_relation.arel.singleton_class.attr_accessor(:klass_and_select_values)
    missed_relation.arel.klass_and_select_values = [relation.klass, @select_values]
    if cache_lock?
      records += missed_relation.find_by_sql(missed_relation.arel, &block)
    else
      missed_relation.connection.transaction do
        records += missed_relation.find_by_sql(missed_relation.arel, &block)
      end
    end
  end

  records_order(records)

  relation.preload_associations(records) unless relation.skip_preloading_value

  records.each(&:readonly!) if relation.readonly_value
  records.each(&:strict_loading!) if relation.strict_loading_value

  records
end

#exec_queries_cacheable?Boolean

rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ar_cache/query.rb', line 45

def exec_queries_cacheable? # rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
  return false if table.disabled?
  return false if relation.skip_query_cache_value
  return false if relation.lock_value
  return false if relation.distinct_value
  return false if relation.group_values.any?
  return false if relation.joins_values.any?
  return false if relation.left_outer_joins_values.any?
  return false if relation.offset_value
  return false if relation.eager_loading?
  return false if relation.connection.transaction_manager.transaction_table?(table.name)
  return false unless relation.from_clause.empty?
  return false unless where_clause.cacheable?
  return false unless select_values_cacheable?
  return false unless order_values_cacheable?
  return false unless limit_value_cacheable?

  true
end