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.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/support_table_cache.rb', line 103

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 attributes
    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