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



69
70
71
72
73
74
75
# File 'lib/support_table_cache/relation_override.rb', line 69

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



83
84
85
86
87
88
89
# File 'lib/support_table_cache/relation_override.rb', line 83

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

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

Parameters:

  • args (Array<Object>)

    Arguments passed to find_by.

Returns:

  • (ActiveRecord::Base, nil)

    The found record or nil if not found.



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
44
45
46
47
48
49
# File 'lib/support_table_cache/relation_override.rb', line 11

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

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

  # Skip caching for has_many or has_many :through associations
  return super if is_a?(ActiveRecord::Associations::CollectionProxy)

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

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

Parameters:

  • args (Array<Object>)

    Arguments passed to find_by!.

Returns:

  • (ActiveRecord::Base)

    The found record.

Raises:

  • (ActiveRecord::RecordNotFound)

    if no record is found.



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

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