Class: ActiveRecord::Base

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

Class Method Summary collapse

Class Method Details

.cache_version_key(table_name = nil) ⇒ Object



41
42
43
# File 'lib/query_memcached.rb', line 41

def cache_version_key(table_name = nil)
  "#{global_cache_version_key}/#{table_name || self.table_name}"
end

.connection_with_memcache_query_cacheObject



33
34
35
36
37
# File 'lib/query_memcached.rb', line 33

def connection_with_memcache_query_cache
  conn = connection_without_memcache_query_cache
  conn.memcache_query_cache_options = self.enableMemcacheQueryForModels[self.to_s]
  conn
end

.enable_memcache_querycache(options = {}) ⇒ Object

put this class method at the top of your AR model to enable memcache for the queryCache, otherwise it will use standard query cache



23
24
25
26
27
28
29
30
31
# File 'lib/query_memcached.rb', line 23

def enable_memcache_querycache(options = {})
  if ActionController::Base.perform_caching && defined?(::Rails.cache) && ::Rails.cache.is_a?(ActiveSupport::Cache::MemCacheStore)
    options[:expires_in] ||= 90.minutes
    self.enableMemcacheQueryForModels[ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s] = options
  else
    warning = "[Query memcached WARNING] Disabled for #{ActiveRecord::Base.send(:class_name_of_active_record_descendant, self)} -- Memcache for QueryCache is not enabled for this model because caching is not turned on, Rails.cache is not defined, or cache engine is not mem_cache_store"
    ActiveRecord::Base.logger.error warning
  end
end

.extract_table_names(sql) ⇒ Object

Given a sql query this method extract all the table names of the database affected by the query thanks to the regular expression we have generated on the load of the plugin



59
60
61
# File 'lib/query_memcached.rb', line 59

def extract_table_names(sql)
  sql.gsub(/`/,'').scan(self.table_names).map {|t| t.strip}.uniq
end

.global_cache_version_keyObject



45
# File 'lib/query_memcached.rb', line 45

def global_cache_version_key; 'version' end

.increase_version!(table_name = nil) ⇒ Object

Increment the class version key number



48
49
50
51
52
53
54
55
# File 'lib/query_memcached.rb', line 48

def increase_version!(table_name = nil)
  key = cache_version_key(table_name)
  if r = ::Rails.cache.read(key)
    ::Rails.cache.write(key, r.to_i + 1)
  else
    ::Rails.cache.write(key, 1)
  end
end