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.



54
55
56
# File 'lib/support_table_cache.rb', line 54

def disable_cache(disabled = true, &block)
  SupportTableCache.with_fiber_local("support_table_cache_disabled:#{name}", !!disabled, &block)
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.



63
64
65
# File 'lib/support_table_cache.rb', line 63

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.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/support_table_cache.rb', line 71

def load_cache
  cache = current_support_table_cache
  return if cache.nil?

  find_each do |record|
    support_table_cache_by_attributes.each do |attribute_names, case_sensitive, where|
      next unless where.nil? || where.all? { |name, value| record[name] == type_for_attribute(name).cast(value) }

      attributes = record.attributes.slice(*attribute_names)
      cache_key = SupportTableCache.cache_key(self, attributes, attribute_names, case_sensitive)
      next if cache_key.nil?

      cache.write(cache_key, record, expires_in: support_table_cache_ttl)
    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 or true to use an optimized in-memory cache.



93
94
95
96
# File 'lib/support_table_cache.rb', line 93

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