Module: Switchman::ActiveRecord::QueryCache

Defined in:
lib/switchman/active_record/query_cache.rb

Instance Method Summary collapse

Instance Method Details

#cacheObject



32
33
34
35
36
37
38
# File 'lib/switchman/active_record/query_cache.rb', line 32

def cache
  old, self.query_cache_enabled = query_cache_enabled, true
  yield
ensure
  self.query_cache_enabled = old
  clear_query_cache unless self.query_cache_enabled
end

#clear_query_cacheObject



47
48
49
# File 'lib/switchman/active_record/query_cache.rb', line 47

def clear_query_cache
  Thread.current[:query_cache]&.clear
end

#disable_query_cache!Object



28
29
30
# File 'lib/switchman/active_record/query_cache.rb', line 28

def disable_query_cache!
  self.query_cache_enabled = false
end

#enable_query_cache!Object

basically wholesale repeat of the methods from the original (see github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb), but with self.query_cache_enabled and self.query_cache_enabled= instead of @query_cache_enabled.



24
25
26
# File 'lib/switchman/active_record/query_cache.rb', line 24

def enable_query_cache!
  self.query_cache_enabled = true
end

#query_cacheObject

thread local accessors to replace @query_cache_enabled



6
7
8
9
# File 'lib/switchman/active_record/query_cache.rb', line 6

def query_cache
  thread_cache = Thread.current[:query_cache] ||= {}
  thread_cache[self.object_id] ||= Hash.new { |h,sql| h[sql] = {} }
end

#query_cache_enabledObject



11
12
13
# File 'lib/switchman/active_record/query_cache.rb', line 11

def query_cache_enabled
  Thread.current[:query_cache_enabled]
end

#query_cache_enabled=(value) ⇒ Object



15
16
17
# File 'lib/switchman/active_record/query_cache.rb', line 15

def query_cache_enabled=(value)
  Thread.current[:query_cache_enabled] = value
end

#select_all(arel, name = nil, binds = [], preparable: nil) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/switchman/active_record/query_cache.rb', line 51

def select_all(arel, name = nil, binds = [], preparable: nil)
  if self.query_cache_enabled && !locked?(arel)
    arel, binds = binds_from_relation(arel, binds)
    sql = to_sql(arel, binds)
    cache_sql(sql, binds) { super(sql, name, binds, preparable: preparable) }
  else
    super
  end
end

#uncachedObject



40
41
42
43
44
45
# File 'lib/switchman/active_record/query_cache.rb', line 40

def uncached
  old, self.query_cache_enabled = query_cache_enabled, false
  yield
ensure
  self.query_cache_enabled = old
end