Module: SupportTableCache::FindByOverride

Defined in:
lib/support_table_cache.rb

Instance Method Summary collapse

Instance Method Details

#find_by(*args) ⇒ Object

Override for the find_by method that looks in the cache first.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/support_table_cache.rb', line 107

def find_by(*args)
  return super if SupportTableCache.cache.nil? || SupportTableCache.disabled?

  cache_key = nil
  attributes = args.first if args.size == 1 && args.first.is_a?(Hash)

  if respond_to?(:scope_attributes) && scope_attributes.present?
    attributes = scope_attributes.merge(attributes || {})
  end

  if attributes.present?
    support_table_cache_by_attributes.each do |attribute_names, case_sensitive|
      cache_key = SupportTableCache.cache_key(self, attributes, attribute_names, case_sensitive)
      break if cache_key
    end
  end

  if cache_key
    SupportTableCache.cache.fetch(cache_key, expires_in: support_table_cache_ttl) { super }
  else
    super
  end
end