Class: Merb::Cache::AbstractStrategyStore

Inherits:
AbstractStore show all
Defined in:
lib/merb-cache/stores/strategy/abstract_strategy_store.rb

Overview

A strategy store wraps one or more fundamental stores, acting as a middle man between caching requests.

For example, if you need to save memory on your Memcache server, you could wrap your MemcachedStore with a GzipStore. This would automatically compress the cached data when put into the cache, and decompress it on the way out. You can even wrap strategy caches with other strategy caches. If your key was comprised of sensitive information, like a SSN, you might want to encrypt the key before storage. Wrapping your GzipStore in a SHA1Store would take care of that for you.

The AbstractStore class defines 9 methods as the API:

writable?(key, parameters = {}, conditions = {}) exists?(key, parameters = {}) read(key, parameters = {}) write(key, data = nil, parameters = {}, conditions = {}) write_all(key, data = nil, parameters = {}, conditions = {}) fetch(key, parameters = {}, conditions = {}, &blk) delete(key, parameters = {}) delete_all delete_all!

AbstractStrategyStore implements all of these with the exception of delete_all. If a strategy store can guarantee that calling delete_all on it’s wrapped store(s) will only delete entries populated by the strategy store, it may define the safe version of delete_all. However, this is usually not the case, hence delete_all is not part of the public API for AbstractStrategyStore.

Direct Known Subclasses

ActionStore, AdhocStore, GzipStore, PageStore, SHA1Store

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractStore

#delete_all

Constructor Details

#initialize(config = {}) ⇒ AbstractStrategyStore

Returns a new instance of AbstractStrategyStore.



51
52
53
54
55
56
57
58
59
60
# File 'lib/merb-cache/stores/strategy/abstract_strategy_store.rb', line 51

def initialize(config = {})
  @stores = contextualized_stores.map do |cs|
    case cs
    when Symbol
      Merb::Cache[cs]
    when Class
      cs.new(config)
    end
  end
end

Instance Attribute Details

#storesObject

END: interface for creating strategy stores.



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

def stores
  @stores
end

Class Method Details

.contextualize(*stores) ⇒ Object Also known as: []

START: interface for creating strategy stores. This should/might change.



37
38
39
40
41
42
43
# File 'lib/merb-cache/stores/strategy/abstract_strategy_store.rb', line 37

def self.contextualize(*stores)
  Class.new(self) do
    cattr_accessor :contextualized_stores

    self.contextualized_stores = stores
  end
end

Instance Method Details

#cloneObject



116
117
118
119
120
# File 'lib/merb-cache/stores/strategy/abstract_strategy_store.rb', line 116

def clone
  twin = super
  twin.stores = self.stores.map {|s| s.clone}
  twin
end

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

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

Raises:

  • (NotImplementedError)


105
106
107
# File 'lib/merb-cache/stores/strategy/abstract_strategy_store.rb', line 105

def delete(key, parameters = {})
  raise NotImplementedError
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.

Raises:

  • (NotImplementedError)


112
113
114
# File 'lib/merb-cache/stores/strategy/abstract_strategy_store.rb', line 112

def delete_all!
  raise NotImplementedError
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)

Raises:

  • (NotImplementedError)


100
101
102
# File 'lib/merb-cache/stores/strategy/abstract_strategy_store.rb', line 100

def exists?(key, parameters = {})
  raise NotImplementedError
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.

Raises:

  • (NotImplementedError)


94
95
96
# File 'lib/merb-cache/stores/strategy/abstract_strategy_store.rb', line 94

def fetch(key, parameters = {}, conditions = {}, &blk)
  raise NotImplementedError
end

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

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

Raises:

  • (NotImplementedError)


74
75
76
# File 'lib/merb-cache/stores/strategy/abstract_strategy_store.rb', line 74

def read(key, parameters = {})
  raise NotImplementedError
end

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

determines if the store is able to persist data identified by the key & parameters with the given conditions.

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


68
69
70
# File 'lib/merb-cache/stores/strategy/abstract_strategy_store.rb', line 68

def writable?(key, parameters = {}, conditions = {})
  raise NotImplementedError
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.

Raises:

  • (NotImplementedError)


81
82
83
# File 'lib/merb-cache/stores/strategy/abstract_strategy_store.rb', line 81

def write(key, data = nil, parameters = {}, conditions = {})
  raise NotImplementedError
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.

Raises:

  • (NotImplementedError)


88
89
90
# File 'lib/merb-cache/stores/strategy/abstract_strategy_store.rb', line 88

def write_all(key, data = nil, parameters = {}, conditions = {})
  raise NotImplementedError
end