class ActiveSupport::Cache::Level2

Attributes

stores[R]

Public Class Methods

instrument() click to toggle source

Rails 3 doesn't instrument by default, this overrides it

# File lib/active_support/cache/level2.rb, line 40
def self.instrument
  true
end
new(store_options) click to toggle source
# File lib/active_support/cache/level2.rb, line 14
def initialize(store_options)
  @stores = store_options.each_with_object({}) do |(name,options), h|
    h[name] = ActiveSupport::Cache.lookup_store(options)
  end
  @options = {}
end

Public Instance Methods

cleanup(*args) click to toggle source
# File lib/active_support/cache/level2.rb, line 21
def cleanup(*args)
  @stores.each_value { |s| s.cleanup(*args) }
end
clear(*args) click to toggle source
# File lib/active_support/cache/level2.rb, line 25
def clear(*args)
  @stores.each_value { |s| s.clear(*args) }
end
read_multi(*names) click to toggle source
# File lib/active_support/cache/level2.rb, line 29
def read_multi(*names)
  result = {}
  @stores.each do |_name,store|
    data = store.read_multi(*names)
    result.merge! data
    names -= data.keys
  end
  result
end

Protected Instance Methods

delete_entry(key, options) click to toggle source
# File lib/active_support/cache/level2.rb, line 67
def delete_entry(key, options)
  selected_stores(options)
  stores.map { |name,store|
    store.send :delete_entry, key, options
  }.all?
end
instrument(operation, key, options = nil) { |payload| ... } click to toggle source
Calls superclass method
# File lib/active_support/cache/level2.rb, line 46
def instrument(operation, key, options = nil)
  super(operation, key, options) do |payload|
    yield(payload).tap do
      payload[:level] = current_level if payload
    end
  end
end
read_entry(key, options) click to toggle source
# File lib/active_support/cache/level2.rb, line 54
def read_entry(key, options)
  stores = selected_stores(options)
  read_entry_from(stores, key, options)
end
write_entry(key, entry, options) click to toggle source
# File lib/active_support/cache/level2.rb, line 59
def write_entry(key, entry, options)
  stores = selected_stores(options)
  stores.each do |name, store|
    result = store.send :write_entry, key, entry, options
    return false unless result
  end
end

Private Instance Methods

current_level() click to toggle source
# File lib/active_support/cache/level2.rb, line 76
def current_level
  Thread.current[:level2_current]
end
current_level!(name) click to toggle source
# File lib/active_support/cache/level2.rb, line 80
def current_level!(name)
  Thread.current[:level2_current] = name
end
read_entry_from(stores, key, options) click to toggle source
# File lib/active_support/cache/level2.rb, line 84
def read_entry_from(stores, key, options)
  return if stores.empty?

  (name,store), *other_stores = stores.to_a
  current_level! name
  entry = store.send :read_entry, key, options
  return entry if entry.present?

  entry = read_entry_from(other_stores, key, options)
  unless entry.present?
    current_level! :all
    return
  end
  store.send :write_entry, key, entry, {}
  
  entry
end
selected_stores(options) click to toggle source
# File lib/active_support/cache/level2.rb, line 102
def selected_stores(options)
  only = options[:only]
  if only.nil?
    current_level! :all
    @stores
  else
    current_level! only
    @stores.select { |name,_| name == only }
  end
end