Module: SupportTableCache

Extended by:
ActiveSupport::Concern
Defined in:
lib/support_table_cache.rb

Overview

This concern can be added to a model for a support table to add the ability to lookup entries in these table using Rails.cache when calling find_by rather than hitting the database every time.

Defined Under Namespace

Modules: FindByOverride

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.cacheObject



70
71
72
73
74
75
76
# File 'lib/support_table_cache.rb', line 70

def cache
  if defined?(@cache)
    @cache
  elsif defined?(Rails.cache)
    Rails.cache
  end
end

Class Method Details

.cache_key(klass, attributes, key_attribute_names, case_sensitive) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generate a consistent cache key for a set of attributes. Returns nil if the attributes are not cacheable.

Parameters:

  • klass (Class)

    The class to



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/support_table_cache.rb', line 82

def cache_key(klass, attributes, key_attribute_names, case_sensitive)
  return nil if attributes.blank? || key_attribute_names.blank?

  sorted_names = attributes.keys.map(&:to_s).sort
  return nil unless sorted_names == key_attribute_names

  sorted_attributes = {}
  sorted_names.each do |attribute_name|
    value = (attributes[attribute_name] || attributes[attribute_name.to_sym])
    if !case_sensitive && (value.is_a?(String) || value.is_a?(Symbol))
      value = value.to_s.downcase
    end
    sorted_attributes[attribute_name] = value
  end

  [klass.name, sorted_attributes]
end

.disable(disabled = true, &block) ⇒ Object

Disable the caching behavior. If a block is specified, then caching is only disabled for that block. If no block is specified, then caching is disabled globally.

Parameters:

  • disabled (Boolean) (defaults to: true)

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



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

def disable(disabled = true, &block)
  if block
    save_val = Thread.current[:support_table_cache_disabled]
    begin
      Thread.current[:support_table_cache_disabled] = !!disabled
    ensure
      Thread.current[:support_table_cache_disabled] = save_val
    end
  else
    @disabled = !!disabled
  end
end

.disabled?Boolean

Return true if caching has been disabled.

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
# File 'lib/support_table_cache.rb', line 59

def disabled?
  block_value = Thread.current[:support_table_cache_disabled]
  if block_value.nil?
    !!(defined?(@disabled) && @disabled)
  else
    block_value
  end
end

.enable(&block) ⇒ Object



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

def enable(&block)
  disable(false, &block)
end

Instance Method Details

#uncachevoid

This method returns an undefined value.

Remove the cache entry for this record.



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/support_table_cache.rb', line 125

def uncache
  cache_by_attributes = self.class.support_table_cache_by_attributes
  return if cache_by_attributes.blank? || SupportTableCache.cache.nil?

  cache_by_attributes.each do |attribute_names, case_sensitive|
    attributes = {}
    attribute_names.each do |name|
      attributes[name] = self[name]
    end
    cache_key = SupportTableCache.cache_key(self.class, attributes, attribute_names, case_sensitive)
    SupportTableCache.cache.delete(cache_key)
  end
end