Module: ActiveRecord::ConnectionAdapters::QueryCache

Defined in:
lib/query_memcached.rb

Instance Method Summary collapse

Instance Method Details

#cacheObject

Enable the query cache within the block



110
111
112
113
114
115
116
117
118
# File 'lib/query_memcached.rb', line 110

def cache
  old, @query_cache_enabled = @query_cache_enabled, true
  @query_cache ||= {}
  @cache_version ||= {}
  yield
ensure
  @query_cache_enabled = old
  clear_query_cache
end

#clear_query_cacheObject

Clears the query cache.

One reason you may wish to call this method explicitly is between queries that ask the database to randomize results. Otherwise the cache would see the same SQL query and repeatedly return the same result each time, silently undermining the randomness you were expecting.



126
127
128
129
# File 'lib/query_memcached.rb', line 126

def clear_query_cache
  @query_cache.clear if @query_cache
  @cache_version.clear if @cache_version
end