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, RelationOverride
Class Attribute Summary collapse
Class Method Summary collapse
-
.cache_key(klass, attributes, key_attribute_names, case_sensitive) ⇒ Object
private
Generate a consistent cache key for a set of attributes.
-
.disable(disabled = true, &block) ⇒ Object
Disable the caching behavior.
-
.disabled? ⇒ Boolean
Return true if caching has been disabled.
- .enable(&block) ⇒ Object
Instance Method Summary collapse
-
#uncache ⇒ void
Remove the cache entry for this record.
Class Attribute Details
.cache ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/support_table_cache.rb', line 74 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.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/support_table_cache.rb', line 86 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.
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/support_table_cache.rb', line 44 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.
63 64 65 66 67 68 69 70 |
# File 'lib/support_table_cache.rb', line 63 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
57 58 59 |
# File 'lib/support_table_cache.rb', line 57 def enable(&block) disable(false, &block) end |
Instance Method Details
#uncache ⇒ void
This method returns an undefined value.
Remove the cache entry for this record.
163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/support_table_cache.rb', line 163 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 |