Module: SupportTableCache
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/support_table_cache.rb,
lib/support_table_cache/associations.rb,
lib/support_table_cache/memory_cache.rb,
lib/support_table_cache/find_by_override.rb,
lib/support_table_cache/relation_override.rb
Overview
This concern can be added to a model to add the ability to look up entries in the table using Rails.cache when calling find_by rather than hitting the database every time.
Defined Under Namespace
Modules: Associations, ClassMethods, FindByOverride, RelationOverride Classes: MemoryCache
Class Method Summary collapse
-
.cache ⇒ ActiveSupport::Cache::Store
Get the global cache (will default to ‘Rails.cache` if running in a Rails environment).
-
.cache=(value) ⇒ void
Set the global cache to use.
-
.cache_key(klass, attributes, key_attribute_names, case_sensitive) ⇒ String
private
Generate a consistent cache key for a set of attributes.
-
.disable(disabled = true, &block) ⇒ Object
Disable the caching behavior for all classes.
-
.disabled? ⇒ Boolean
Return true if caching has been disabled.
-
.enable(&block) ⇒ Object
Enable the caching behavior for all classes.
-
.testing!(&block) ⇒ void
Enter test mode for a block.
-
.testing_cache ⇒ SupportTableCache::MemoryCache
private
Get the current test mode cache.
Instance Method Summary collapse
-
#uncache ⇒ void
Remove the cache entry for this record.
Class Method Details
.cache ⇒ ActiveSupport::Cache::Store
Get the global cache (will default to ‘Rails.cache` if running in a Rails environment).
193 194 195 196 197 198 199 200 201 |
# File 'lib/support_table_cache.rb', line 193 def cache if testing_cache testing_cache elsif defined?(@cache) @cache elsif defined?(Rails.cache) Rails.cache end end |
.cache=(value) ⇒ void
This method returns an undefined value.
Set the global cache to use.
185 186 187 188 |
# File 'lib/support_table_cache.rb', line 185 def cache=(value) value = MemoryCache.new if value == :memory @cache = value end |
.cache_key(klass, attributes, key_attribute_names, case_sensitive) ⇒ String
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. It will return nil if the attributes are not cacheable.
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/support_table_cache.rb', line 239 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 for all classes. If a block is specified, then caching is only disabled for that block. If no block is specified, then caching is disabled globally.
148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/support_table_cache.rb', line 148 def disable(disabled = true, &block) if block save_val = Thread.current.thread_variable_get(:support_table_cache_disabled) begin Thread.current.thread_variable_set(:support_table_cache_disabled, !!disabled) yield ensure Thread.current.thread_variable_set(:support_table_cache_disabled, save_val) end else @disabled = !!disabled end end |
.disabled? ⇒ Boolean
Return true if caching has been disabled.
172 173 174 175 176 177 178 179 |
# File 'lib/support_table_cache.rb', line 172 def disabled? block_value = Thread.current.thread_variable_get(:support_table_cache_disabled) if block_value.nil? !!(defined?(@disabled) && @disabled) else block_value end end |
.enable(&block) ⇒ Object
Enable the caching behavior for all classes. If a block is specified, then caching is only enabled for that block. If no block is specified, then caching is enabled globally.
166 167 168 |
# File 'lib/support_table_cache.rb', line 166 def enable(&block) disable(false, &block) end |
.testing!(&block) ⇒ void
This method returns an undefined value.
Enter test mode for a block. New caches will be used within each test mode block. You can use this to wrap your test methods so that cached values from one test don’t show up in subsequent tests.
208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/support_table_cache.rb', line 208 def testing!(&block) save_val = Thread.current.thread_variable_get(:support_table_cache_test_cache) if save_val.nil? Thread.current.thread_variable_set(:support_table_cache_test_cache, MemoryCache.new) end begin yield ensure Thread.current.thread_variable_set(:support_table_cache_test_cache, save_val) end end |
.testing_cache ⇒ SupportTableCache::MemoryCache
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.
Get the current test mode cache. This will only return a value inside of a ‘testing!` block.
224 225 226 227 228 |
# File 'lib/support_table_cache.rb', line 224 def testing_cache unless defined?(@cache) && @cache.nil? Thread.current.thread_variable_get(:support_table_cache_test_cache) end end |
Instance Method Details
#uncache ⇒ void
This method returns an undefined value.
Remove the cache entry for this record.
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/support_table_cache.rb', line 261 def uncache cache_by_attributes = self.class.support_table_cache_by_attributes return if cache_by_attributes.blank? cache = self.class.send(:current_support_table_cache) return if 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) cache.delete(cache_key) end end |