class CachedResource::Configuration

The Configuration class manages class specific options for cached resource.

Constants

CACHE

default or fallback cache without rails

LOGGER

default or fallback logger without rails

LOGGER_PREFIX

prefix for log messages

Public Class Methods

new(options={}) click to toggle source

Initialize a Configuration with the given options, overriding any defaults. The following options exist for cached resource: :enabled, default: true :ttl, default: 604800 :race_condition_ttl: 86400 :ttl_randomization, default: false, :ttl_randomization_scale, default: 1..2, :collection_synchronize, default: false, :collection_arguments, default: [:all] :cache, default: Rails.cache or ActiveSupport::Cache::MemoryStore.new, :logger, default: Rails.logger or ActiveSupport::Logger.new(NilIO.new)

Calls superclass method
# File lib/cached_resource/configuration.rb, line 26
def initialize(options={})
  super({
    :enabled => true,
    :race_condition_ttl => 86400,
    :ttl => 604800,
    :ttl_randomization => false,
    :ttl_randomization_scale => 1..2,
    :collection_synchronize => false,
    :collection_arguments => [:all],
    :cache => defined?(Rails.cache)  && Rails.cache || CACHE,
    :logger => defined?(Rails.logger) && Rails.logger || LOGGER
  }.merge(options))
end

Public Instance Methods

generate_ttl() click to toggle source

Determine the time until a cache entry should expire. If ttl_randomization is enabled, then a the set ttl will be multiplied by a random value from ttl_randomization_scale.

# File lib/cached_resource/configuration.rb, line 43
def generate_ttl
  ttl_randomization && randomized_ttl || ttl
end
off!() click to toggle source

Disables caching.

# File lib/cached_resource/configuration.rb, line 53
def off!
  self.enabled = false
end
on!() click to toggle source

Enables caching.

# File lib/cached_resource/configuration.rb, line 48
def on!
  self.enabled = true
end

Private Instance Methods

randomized_ttl() click to toggle source

Get a randomized ttl value between ttl * ttl_randomization_scale begin and ttl * ttl_randomization_scale end

# File lib/cached_resource/configuration.rb, line 61
def randomized_ttl
  ttl * sample_range(ttl_randomization_scale)
end
sample_range(range, seed=nil) click to toggle source

Choose a random value from within the given range, optionally seeded by seed.

# File lib/cached_resource/configuration.rb, line 67
def sample_range(range, seed=nil)
  srand seed if seed
  rand * (range.end - range.begin) + range.begin
end