Class: Merb::Cache::AdhocStore

Inherits:
AbstractStrategyStore show all
Defined in:
lib/merb-cache/stores/strategy/adhoc_store.rb

Overview

General purpose store, use for your own contexts. Since it wraps access to multiple fundamental stores, it's easy to use this strategy store with distributed cache stores like Memcached.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractStrategyStore

#clone, contextualize

Methods inherited from AbstractStore

#delete_all

Constructor Details

#initialize(*names) ⇒ AdhocStore

Returns a new instance of AdhocStore



14
15
16
# File 'lib/merb-cache/stores/strategy/adhoc_store.rb', line 14

def initialize(*names)
  @stores = names.map {|n| Merb::Cache[n]}
end

Instance Attribute Details

#storesObject

Returns the value of attribute stores



12
13
14
# File 'lib/merb-cache/stores/strategy/adhoc_store.rb', line 12

def stores
  @stores
end

Instance Method Details

#delete(key, parameters = {}) ⇒ Object

deletes the entry for the key & parameter from the store.



58
59
60
# File 'lib/merb-cache/stores/strategy/adhoc_store.rb', line 58

def delete(key, parameters = {})
  @stores.map {|s| s.delete(key, parameters)}.any?
end

#delete_all!Object

deletes all entries for the key & parameters for the store. considered dangerous because strategy stores which call delete_all! on their context stores could delete other store's entrees.



65
66
67
# File 'lib/merb-cache/stores/strategy/adhoc_store.rb', line 65

def delete_all!
  @stores.map {|s| s.delete_all!}.all?
end

#exists?(key, parameters = {}) ⇒ Boolean

returns true/false/nil based on if data identified by the key & parameters is persisted in the store.

Returns:

  • (Boolean)


53
54
55
# File 'lib/merb-cache/stores/strategy/adhoc_store.rb', line 53

def exists?(key, parameters = {})
  @stores.capture_first {|s| s.exists?(key, parameters)}
end

#fetch(key, parameters = {}, conditions = {}, &blk) ⇒ Object

tries to read the data from the store. If that fails, it calls the block parameter and persists the result. If it cannot be fetched, the block call is returned.



45
46
47
48
49
# File 'lib/merb-cache/stores/strategy/adhoc_store.rb', line 45

def fetch(key, parameters = {}, conditions = {}, &blk)
  read(key, parameters) ||
    @stores.capture_first {|s| s.fetch(key, parameters, conditions, &blk)} ||
    blk.call
end

#read(key, parameters = {}) ⇒ Object

gets the data from the store identified by the key & parameters. return nil if the entry does not exist.



24
25
26
# File 'lib/merb-cache/stores/strategy/adhoc_store.rb', line 24

def read(key, parameters = {})
  @stores.capture_first {|s| s.read(key, parameters)}
end

#writable?(key, parameters = {}, conditions = {}) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/merb-cache/stores/strategy/adhoc_store.rb', line 18

def writable?(key, parameters = {}, conditions = {})
  @stores.capture_first {|s| s.writable?(key, parameters, conditions)}
end

#write(key, data = nil, parameters = {}, conditions = {}) ⇒ Object

persists the data so that it can be retrieved by the key & parameters. returns nil if it is unable to persist the data. returns true if successful.



31
32
33
# File 'lib/merb-cache/stores/strategy/adhoc_store.rb', line 31

def write(key, data = nil, parameters = {}, conditions = {})
  @stores.capture_first {|s| s.write(key, data, parameters, conditions)}
end

#write_all(key, data = nil, parameters = {}, conditions = {}) ⇒ Object

persists the data to all context stores. returns nil if none of the stores were able to persist the data. returns true if at least one write was successful.



38
39
40
# File 'lib/merb-cache/stores/strategy/adhoc_store.rb', line 38

def write_all(key, data = nil, parameters = {}, conditions = {})
  @stores.map {|s| s.write_all(key, data, parameters, conditions)}.all?
end