Class: ActiveCachedResource::Configuration

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/active_cached_resource/configuration.rb

Constant Summary collapse

CACHING_STRATEGIES =
{
  active_record_sql: ActiveCachedResource::CachingStrategies::SQLCache,
  active_support_cache: ActiveCachedResource::CachingStrategies::ActiveSupportCache
}
OPTIONS =
%i[cache_key_prefix logger enabled ttl]

Instance Method Summary collapse

Constructor Details

#initialize(model, options = {}) ⇒ ActiveCachedResource::Configuration

Note:

If ‘cache_store` is provided and is a CachingStrategies::Base instance, it will be used as the cache strategy. Otherwise, `cache_strategy` must be provided to determine the cache strategy.

Initializes a new configuration for the given model with the specified options.

Parameters:

  • model (Class)

    The model class for which the configuration is being set.

  • options (Hash) (defaults to: {})

    A hash of options to customize the configuration.

Options Hash (options):

  • :cache_store (Symbol)

    The cache store to be used.

  • :cache_strategy (Symbol)

    The cache strategy to be used. One of :active_record_sql or :active_support_cache.

  • :cache_key_prefix (String)

    The prefix for cache keys (default: model name underscored).

  • :logger (Logger)

    The logger instance to be used (default: ActiveCachedResource::Logger).

  • :enabled (Boolean)

    Whether caching is enabled (default: true).

  • :ttl (Integer)

    The time-to-live for cache entries in seconds (default: 86400).



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_cached_resource/configuration.rb', line 31

def initialize(model, options = {})
  super(
    {
      cache: determine_cache_strategy(options[:cache_store], options[:cache_strategy]),
      cache_key_prefix: model.name.underscore,
      logger: ActiveCachedResource::Logger.new(model.name),
      enabled: true,
      ttl: 86400
    }.merge(options.slice(*OPTIONS))
  )
end

Instance Method Details

#off!void

This method returns an undefined value.

Disables caching.



53
54
55
# File 'lib/active_cached_resource/configuration.rb', line 53

def off!
  self.enabled = false
end

#on!void

This method returns an undefined value.

Enables caching.



46
47
48
# File 'lib/active_cached_resource/configuration.rb', line 46

def on!
  self.enabled = true
end