Module: Datadog::Tracing::Contrib::ActiveSupport::Cache::Redis::Patcher

Included in:
Datadog::Tracing::Contrib::ActiveSupport::Cache::Redis
Defined in:
lib/datadog/tracing/contrib/active_support/cache/redis.rb

Overview

Patching behavior for Redis with ActiveSupport

Instance Method Summary collapse

Instance Method Details

#cache_store_class(meth) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/datadog/tracing/contrib/active_support/cache/redis.rb', line 44

def cache_store_class(meth)
  if patch_redis_store?(meth)
    [::ActiveSupport::Cache::RedisStore, ::ActiveSupport::Cache::Store]
  elsif patch_redis_cache_store?(meth)
    [::ActiveSupport::Cache::RedisCacheStore, ::ActiveSupport::Cache::Store]
  else
    super
  end
end

#patch_redis_cache_store?(meth) ⇒ Boolean

Patches the Rails built-in Redis cache backend ‘redis_cache_store`, added in Rails 5.2. We avoid loading the RedisCacheStore class, as it invokes the statement `gem “redis”, “>= 4.0.1”` which fails if the application is using an old version of Redis, or not using Redis at all.



35
36
37
38
39
40
41
42
# File 'lib/datadog/tracing/contrib/active_support/cache/redis.rb', line 35

def patch_redis_cache_store?(meth)
  Gem.loaded_specs['redis'] &&
    # Autoload constants return `constant` for `defined?`, but that doesn't mean they are loaded...
    defined?(::ActiveSupport::Cache::RedisCacheStore) &&
    # ... to check that we need to call `autoload?` and check if it returns `nil`, meaning it's loaded.
    ::ActiveSupport::Cache.autoload?(:RedisCacheStore).nil? &&
    ::ActiveSupport::Cache::RedisCacheStore.instance_methods(false).include?(meth)
end

#patch_redis_store?(meth) ⇒ Boolean

For Rails < 5.2 w/ redis-activesupport… When Redis is used, we can’t only patch Cache::Store as it is Cache::RedisStore, a sub-class of it that is used, in practice. We need to do a per-method monkey patching as some of them might be redefined, and some of them not. The latest version of redis-activesupport redefines write but leaves untouched read and delete: github.com/redis-store/redis-activesupport/blob/v4.1.5/lib/active_support/cache/redis_store.rb

For Rails >= 5.2 w/o redis-activesupport… ActiveSupport includes a Redis cache store internally, and does not require these overrides. github.com/rails/rails/blob/master/activesupport/lib/active_support/cache/redis_cache_store.rb

Returns:

  • (Boolean)


25
26
27
28
29
# File 'lib/datadog/tracing/contrib/active_support/cache/redis.rb', line 25

def patch_redis_store?(meth)
  !Gem.loaded_specs['redis-activesupport'].nil? \
    && defined?(::ActiveSupport::Cache::RedisStore) \
    && ::ActiveSupport::Cache::RedisStore.instance_methods(false).include?(meth)
end