Module: SupportTableCache::FindByOverride Private

Defined in:
lib/support_table_cache/find_by_override.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Instance Method Details

#fetch_by(attributes) ⇒ ActiveRecord::Base?

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.

Same as find_by, but performs a safety check to confirm the query will hit the cache.

Parameters:

  • attributes (Hash)

    Attributes to find the record by.

Returns:

  • (ActiveRecord::Base, nil)

    The found record or nil if not found.

Raises:

  • (ArgumentError)

    if the query cannot use the cache.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/support_table_cache/find_by_override.rb', line 36

def fetch_by(attributes)
  attributes = (attributes || {}).stringify_keys
  query_attributes = support_table_query_attributes(attributes)
  unless SupportTableCache.cacheable_query?(self, query_attributes)
    raise ArgumentError.new("#{name} does not cache queries by #{(query_attributes || attributes).keys.sort.to_sentence}")
  end
  unless all.send(:support_table_cacheable_scope?)
    raise ArgumentError.new("#{name} cannot use the support table cache on an uncacheable scope")
  end
  find_by(attributes)
end

#fetch_by!(attributes) ⇒ ActiveRecord::Base

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.

Same as find_by!, but performs a safety check to confirm the query will hit the cache.

Parameters:

  • attributes (Hash)

    Attributes to find the record by.

Returns:

  • (ActiveRecord::Base)

    The found record.

Raises:

  • (ArgumentError)

    if the query cannot use the cache.

  • (ActiveRecord::RecordNotFound)

    if no record is found.



54
55
56
57
58
59
60
# File 'lib/support_table_cache/find_by_override.rb', line 54

def fetch_by!(attributes)
  value = fetch_by(attributes)
  if value.nil?
    raise ActiveRecord::RecordNotFound.new("Couldn't find #{name}", name)
  end
  value
end

#find_by(*args) ⇒ 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.

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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/support_table_cache/find_by_override.rb', line 7

def find_by(*args)
  cache = current_support_table_cache
  return super unless cache

  # Only queries by simple attribute equality can be matched against cache keys.
  return super unless args.size == 1 && args.first.is_a?(Hash)

  # If the class has any scope applied (a default scope or a scoping block), defer to
  # the relation override, which checks whether the scoped query can be cached.
  return super if all.values.present?

  # Queries inside a transaction could see uncommitted data that would be invalid
  # if the transaction is rolled back, so they cannot be cached.
  return super if SupportTableCache.open_transaction?(self)

  cache_key = SupportTableCache.cache_key_for_query(self, args.first.stringify_keys)

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