Module: SupportTableCache::RelationOverride

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.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/support_table_cache.rb', line 134

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

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

  # Apply any attributes from the current relation chain
  if 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(klass, 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