Module: Datadog::Tracing::Contrib::Configuration::CachingResolver

Included in:
ActiveRecord::Configuration::Resolver
Defined in:
lib/datadog/tracing/contrib/configuration/resolver.rb

Overview

The CachingResolver is a mixin that provides caching functionality to the Resolver class. This is useful when Resolver#resolve values that are expensive to compute. This is a size-limited, FIFO cache.

Examples:

class MyResolver < Datadog::Tracing::Contrib::Configuration::Resolver
  prepend Datadog::Tracing::Contrib::Configuration::CachingResolver
  # ...
end

Instance Method Summary collapse

Instance Method Details

#add(matcher, value) ⇒ Object

Adds a new ‘matcher`, associating with it a `value`.

This ‘value` is returned when `#resolve` is called with a matching value for this matcher. When multiple matchers would match, `#resolve` returns the latest added one.

The ‘matcher` can be transformed internally by the `#parse_matcher` method before being stored.

The ‘value` can also be retrieved by calling `#get` with the same `matcher` added by this method.

Parameters:

  • matcher (Object)

    integration-specific matcher

  • value (Object)

    arbitrary value to be associated with ‘matcher`



115
116
117
118
# File 'lib/datadog/tracing/contrib/configuration/resolver.rb', line 115

def add(matcher, value)
  reset_cache # Bust the cache when a new matcher is added
  super
end

#initialize(*args, cache_limit: 200) ⇒ Object

Parameters:

  • cache_limit (Integer) (defaults to: 200)

    maximum number of entries to cache



94
95
96
97
98
99
# File 'lib/datadog/tracing/contrib/configuration/resolver.rb', line 94

def initialize(*args, cache_limit: 200)
  super(*args)

  @cache_limit = cache_limit
  @cache = {}
end

#reset_cacheObject

Clears the internal cache.



121
122
123
# File 'lib/datadog/tracing/contrib/configuration/resolver.rb', line 121

def reset_cache
  @cache.clear
end

#resolve(value) ⇒ Object

Matches an arbitrary value against the configured matchers previously set with ‘#add`.

If multiple matchers would match, returns the latest one.

Parameters:

  • value (Object)

    integration-specific value

Returns:

  • (Object)

    matching ‘value` configured at `#add`, or `nil` if none match



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/datadog/tracing/contrib/configuration/resolver.rb', line 102

def resolve(value)
  if @cache.key?(value)
    @cache[value]
  else
    if @cache.size >= @cache_limit
      @cache.shift # Remove the oldest entry if cache is full
    end

    @cache[value] = super
  end
end