Module: SupportTableCache::ClassMethods

Defined in:
lib/support_table_cache.rb

Instance Method Summary collapse

Instance Method Details

#disable_cache(disabled = true) { ... } ⇒ 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.

Yields:

  • Executes the provided block with caching disabled or enabled.

Returns:

  • (Object)

    The return value of the block.



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

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 { ... } ⇒ 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.

Yields:

  • Executes the provided block with caching enabled.

Returns:

  • (Object)

    The return value of the block.



60
61
62
# File 'lib/support_table_cache.rb', line 60

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.



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

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.slice(*attribute_names)
      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.



86
87
88
89
# File 'lib/support_table_cache.rb', line 86

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