class ActiveSupport::Cache::NamespacedStore

Public: A wrapper around an ActiveSupport::Cache to prefix all keys.

Example

cache = ActiveSupport::Cache::NamespacedStore.new 'namespace:', Rails.cache

Attributes

namespace[R]
store[R]

Public Class Methods

new(namespace, store) click to toggle source
# File lib/active_support/cache/namespaced_store.rb, line 12
def initialize(namespace, store)
  @namespace = namespace
  @store = store
end

Public Instance Methods

clear() click to toggle source
# File lib/active_support/cache/namespaced_store.rb, line 36
def clear
  store.delete_matched(namespace)
end
fetch(key, *args, &block) click to toggle source
# File lib/active_support/cache/namespaced_store.rb, line 32
def fetch(key, *args, &block)
  store.fetch(namespaced(key), *args, &block)
end
read(key, *args, &block) click to toggle source
# File lib/active_support/cache/namespaced_store.rb, line 17
def read(key, *args, &block)
  store.read(namespaced(key), *args, &block)
end
read_multi(*keys) click to toggle source
# File lib/active_support/cache/namespaced_store.rb, line 21
def read_multi(*keys)
  k = keys.map { |key| namespaced(key) }
  store.read_multi(*k).each_with_object(Hash.new) do |t,memo|
    memo[t[0].sub(namespace, '')] = t[1]
  end
end
write(key, *args, &block) click to toggle source
# File lib/active_support/cache/namespaced_store.rb, line 28
def write(key, *args, &block)
  store.write(namespaced(key), *args, &block)
end

Private Instance Methods

namespaced(key) click to toggle source
# File lib/active_support/cache/namespaced_store.rb, line 42
def namespaced(key)
  "#{namespace}#{key}"
end