Module: SupportTableCache::ClassMethods

Defined in:
lib/support_table_cache.rb

Instance Method Summary collapse

Instance Method Details

#disable_cache(disabled = true, &block) ⇒ Object

Disable the caching behavior for this class within the block. The disabled setting for a class will always take precedence over the global setting.

Parameters:

  • disabled (Boolean) (defaults to: true)

    Caching will be disabled if this is true and enabled if false.

Yield Returns:

  • The return value of the block.


43
44
45
46
47
48
49
50
51
52
# File 'lib/support_table_cache.rb', line 43

def disable_cache(disabled = true, &block)
  varname = "support_table_cache_disabled:#{name}"
  save_val = Thread.current.thread_variable_get(varname)
  begin
    Thread.current.thread_variable_set(varname, !!disabled)
    yield
  ensure
    Thread.current.thread_variable_set(varname, save_val)
  end
end

#enable_cache(&block) ⇒ Object

Enable the caching behavior for this class within the block. The enabled setting for a class will always take precedence over the global setting.

Yield Returns:

  • The return value of the block.


58
59
60
# File 'lib/support_table_cache.rb', line 58

def enable_cache(&block)
  disable_cache(false, &block)
end

#load_cachevoid

This method returns an undefined value.

Load all records into the cache. You should only call this method on small tables with a few dozen rows at most because it will load each row one at a time.


66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/support_table_cache.rb', line 66

def load_cache
  cache = current_support_table_cache
  return super if cache.nil?

  find_each do |record|
    support_table_cache_by_attributes.each do |attribute_names, case_sensitive|
      attributes = record.attributes.select { |name, value| attribute_names.include?(name) }
      cache_key = SupportTableCache.cache_key(self, attributes, attribute_names, case_sensitive)
      cache.fetch(cache_key, expires_in: support_table_cache_ttl) { record }
    end
  end
end

#support_table_cache=(cache) ⇒ void

This method returns an undefined value.

Set a class-specific cache to use in lieu of the global cache.

Parameters:

  • cache (ActiveSupport::Cache::Store, Symbol)

    The cache instance to use. You can also specify the value :memory to use an optimized in-memory cache.


84
85
86
87
# File 'lib/support_table_cache.rb', line 84

def support_table_cache=(cache)
  cache = MemoryCache.new if cache == :memory
  self.support_table_cache_impl = cache
end