Module: SupportTableCache::RelationOverride Private

Defined in:
lib/support_table_cache/relation_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.



59
60
61
62
63
64
65
# File 'lib/support_table_cache/relation_override.rb', line 59

def fetch_by(attributes)
  find_by_attribute_names = support_table_find_by_attribute_names(attributes)
  unless klass.support_table_cache_by_attributes.any? { |attribute_names, _ci| 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.



72
73
74
75
76
77
78
# File 'lib/support_table_cache/relation_override.rb', line 72

def fetch_by!(attributes)
  value = fetch_by(attributes)
  if value.nil?
    raise ActiveRecord::RecordNotFound.new("Couldn't find #{klass.name}", klass.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.



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
38
39
40
41
42
43
# File 'lib/support_table_cache/relation_override.rb', line 8

def find_by(*args)
  return super unless klass.include?(SupportTableCache)

  cache = klass.send(:current_support_table_cache)
  return super unless cache

  return super if select_values.present?

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

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

#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.

Raises:

  • ActiveRecord::RecordNotFound if no record is found.



47
48
49
50
51
52
53
# File 'lib/support_table_cache/relation_override.rb', line 47

def find_by!(*args)
  value = find_by(*args)
  unless value
    raise ActiveRecord::RecordNotFound.new("Couldn't find #{klass.name}", klass.name)
  end
  value
end