Class: CachedResource::Configuration
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- CachedResource::Configuration
- Defined in:
- lib/cached_resource/configuration.rb
Overview
The Configuration class manages class specific options for cached resource.
Constant Summary collapse
- CACHE =
default or fallback cache without rails
ActiveSupport::Cache::MemoryStore.new
- LOGGER =
default or fallback logger without rails
ActiveSupport::Logger.new(NilIO.instance)
- LOGGER_PREFIX =
prefix for log messages
"[cached_resource]"
Instance Method Summary collapse
-
#generate_ttl ⇒ Object
Determine the time until a cache entry should expire.
-
#initialize(options = {}) ⇒ Configuration
constructor
Initialize a Configuration with the given options, overriding any defaults.
-
#off! ⇒ Object
Disables caching.
-
#on! ⇒ Object
Enables caching.
Constructor Details
#initialize(options = {}) ⇒ Configuration
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)
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/cached_resource/configuration.rb', line 26 def initialize(={}) 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()) end |
Instance Method Details
#generate_ttl ⇒ Object
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.
43 44 45 |
# File 'lib/cached_resource/configuration.rb', line 43 def generate_ttl ttl_randomization && randomized_ttl || ttl end |
#off! ⇒ Object
Disables caching.
53 54 55 |
# File 'lib/cached_resource/configuration.rb', line 53 def off! self.enabled = false end |
#on! ⇒ Object
Enables caching.
48 49 50 |
# File 'lib/cached_resource/configuration.rb', line 48 def on! self.enabled = true end |