Class: ActiveSupport::Cache::NamespacedStore

Inherits:
Object
  • Object
show all
Defined in:
lib/active_support/cache/namespaced_store.rb

Overview

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

Example

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace, store) ⇒ NamespacedStore

Returns a new instance of NamespacedStore.



12
13
14
15
# File 'lib/active_support/cache/namespaced_store.rb', line 12

def initialize(namespace, store)
  @namespace = namespace
  @store = store
end

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



10
11
12
# File 'lib/active_support/cache/namespaced_store.rb', line 10

def namespace
  @namespace
end

#storeObject (readonly)

Returns the value of attribute store.



10
11
12
# File 'lib/active_support/cache/namespaced_store.rb', line 10

def store
  @store
end

Instance Method Details

#clearObject



36
37
38
# File 'lib/active_support/cache/namespaced_store.rb', line 36

def clear
  store.delete_matched(namespace)
end

#fetch(key, *args, &block) ⇒ Object



32
33
34
# 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) ⇒ Object



17
18
19
# 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) ⇒ Object



21
22
23
24
25
26
# 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) ⇒ Object



28
29
30
# File 'lib/active_support/cache/namespaced_store.rb', line 28

def write(key, *args, &block)
  store.write(namespaced(key), *args, &block)
end