Class: ActiveSupport::Cache::CascadeStore

Inherits:
Store
  • Object
show all
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

Instance Method Summary collapse

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(options = nil, &blk)
  options ||= {}
  super(options)
  @monitor = Monitor.new
  store_options = options.delete(:stores) || []
  @stores = store_options.map do |o|
    o = o.is_a?(Symbol) ? [o, options] : o
    ActiveSupport::Cache.lookup_store(*o)
  end
end

Instance Attribute Details

#storesObject (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, options = nil)
  nums = cascade(:decrement, name, amount, options)
  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, options = nil)
  cascade(:delete_matched, matcher, options)
  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, options = nil)
  nums = cascade(:increment, name, amount, options)
  nums.detect {|n| !n.nil?}
end