Module: Switchman::ActiveRecord::QueryCache

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

Overview

needs to be included in the same class as ::ActiveRecord::ConnectionAdapters::QueryCache after that module is included

Instance Method Summary collapse

Instance Method Details

#cacheObject



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

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



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

def clear_query_cache
  Thread.current[:query_cache].try(:clear)
end

#disable_query_cache!Object



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

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.



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

def enable_query_cache!
  self.query_cache_enabled = true
end

#query_cacheObject

thread local accessors to replace @query_cache_enabled



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

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

#query_cache_enabledObject



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

def query_cache_enabled
  Thread.current[:query_cache_enabled]
end

#query_cache_enabled=(value) ⇒ Object



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

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

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



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

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

#uncachedObject



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

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