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) ⇒ 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.

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.

Raises:

  • ArgumentError if the query cannot use the cache.



43
44
45
46
47
48
49
# File 'lib/support_table_cache/find_by_override.rb', line 43

def fetch_by(attributes)
  find_by_attribute_names = support_table_find_by_attribute_names(attributes)
  unless support_table_cache_by_attributes.any? { |attribute_names, _ci, _where| attribute_names == find_by_attribute_names }
    raise ArgumentError.new("#{name} does not cache queries by #{find_by_attribute_names.to_sentence}")
  end
  find_by(attributes)
end

#fetch_by!(attributes) ⇒ 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.

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.

Raises:

  • ArgumentError if the query cannot use the cache.

  • ActiveRecord::RecordNotFound if no record is found.



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

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
30
31
32
33
34
35
36
37
# File 'lib/support_table_cache/find_by_override.rb', line 7

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

  cache_key = nil
  attributes = ((args.size == 1 && args.first.is_a?(Hash)) ? args.first.stringify_keys : {})

  if respond_to?(:scope_attributes) && scope_attributes.present?
    attributes = scope_attributes.stringify_keys.merge(attributes)
  end

  if attributes.present?
    support_table_cache_by_attributes.each do |attribute_names, case_sensitive, where|
      where&.each do |name, value|
        if attributes.include?(name) && attributes[name] == value
          attributes.delete(name)
        else
          return super
        end
      end
      cache_key = SupportTableCache.cache_key(self, attributes, attribute_names, case_sensitive)
      break if cache_key
    end
  end

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