Class: ActiveSupport::Cache::CascadeStore
- Inherits:
-
Store
- Object
- Store
- ActiveSupport::Cache::CascadeStore
- Defined in:
- lib/active_support/cache/cascade_store.rb
Overview
A thread-safe cache store implementation that cascades operations to a list of other cache stores. It is used to provide fallback cache stores when primary stores become unavailable. For example, to initialize a CascadeStore that cascades through MemCacheStore, MemoryStore, and FileStore:
ActiveSupport::Cache.lookup_store(:cascade_store,
:stores => [
:mem_cache_store,
:memory_store,
:file_store
]
})
Cache operation behavior:
Read: returns first cache hit from :stores, nil if none found
Write/Delete: write/delete through to each cache store in :stores
Increment/Decrement: increment/decrement each store, returning the new number if any stores was successfully incremented/decremented, nil otherwise
Instance Attribute Summary collapse
-
#stores ⇒ Object
readonly
Returns the value of attribute stores.
Instance Method Summary collapse
- #decrement(name, amount = 1, options = nil) ⇒ Object
- #delete_matched(matcher, options = nil) ⇒ Object
- #increment(name, amount = 1, options = nil) ⇒ Object
-
#initialize(options = nil, &blk) ⇒ CascadeStore
constructor
Initialize a CascadeStore with
options[:stores], an array of options to initialize other ActiveSupport::Cache::Store implementations.
Constructor Details
#initialize(options = nil, &blk) ⇒ CascadeStore
Initialize a CascadeStore with options[:stores], an array of options to initialize other ActiveSupport::Cache::Store implementations. If options is a symbol, top level CascadeStore options are used for cascaded stores. If options is an array, they are passed on unchanged.
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/active_support/cache/cascade_store.rb', line 37 def initialize( = nil, &blk) ||= {} super() @monitor = Monitor.new = .delete(:stores) || [] @stores = .map do |o| o = o.is_a?(Symbol) ? [o, ] : o ActiveSupport::Cache.lookup_store(*o) end end |
Instance Attribute Details
#stores ⇒ Object (readonly)
Returns the value of attribute stores.
30 31 32 |
# File 'lib/active_support/cache/cascade_store.rb', line 30 def stores @stores end |
Instance Method Details
#decrement(name, amount = 1, options = nil) ⇒ Object
53 54 55 56 |
# File 'lib/active_support/cache/cascade_store.rb', line 53 def decrement(name, amount = 1, = nil) nums = cascade(:decrement, name, amount, ) nums.detect {|n| !n.nil?} end |
#delete_matched(matcher, options = nil) ⇒ Object
58 59 60 61 |
# File 'lib/active_support/cache/cascade_store.rb', line 58 def delete_matched(matcher, = nil) cascade(:delete_matched, matcher, ) nil end |
#increment(name, amount = 1, options = nil) ⇒ Object
48 49 50 51 |
# File 'lib/active_support/cache/cascade_store.rb', line 48 def increment(name, amount = 1, = nil) nums = cascade(:increment, name, amount, ) nums.detect {|n| !n.nil?} end |