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.



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

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.



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

def relation
  @relation
end

#tableObject (readonly)

Returns the value of attribute table.



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

def table
  @table
end

#where_clauseObject (readonly)

Returns the value of attribute where_clause.



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

def where_clause
  @where_clause
end

Instance Method Details

#exec_queries(&block) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity



13
14
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
# File 'lib/ar_cache/query.rb', line 13

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

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

  missed_relation = if records.empty?
                      relation
                    elsif where_clause.missed_hash.any?
                      relation.rewhere(where_clause.missed_hash)
                    end

  if missed_relation
    records += relation.find_by_sql(missed_relation.arel, &block).tap do |rs|
      table.write(rs) if relation.select_values.empty?
    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)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ar_cache/query.rb', line 41

def exec_queries_cacheable? # rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
  return false if relation.klass.ar_cache_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.changed_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