Module: Twigg::Cacher
- Extended by:
- Dependency
- Defined in:
- lib/twigg/cacher.rb
Overview
The Cacher module is an abstraction around caching that offers two public methods, Cacher.get and Cacher.set.
If the twigg-cache gem is installed and enabled, delegates the storing of cached items to Memcached. In the absence of the gem, every cache lookup is treated as a miss.
Defined Under Namespace
Classes: DummyClient
Class Method Summary collapse
-
.get(key, *args, &block) ⇒ Object
Gets stored value for ‘key`; in the event of a cache miss, yields to `block`, and caches and returns the result.
-
.set(key, *args, &block) ⇒ Object
Stores the result of yielding to ‘block` in the cache, with `key` as key, and returns the result.
Class Method Details
.get(key, *args, &block) ⇒ Object
Gets stored value for ‘key`; in the event of a cache miss, yields to `block`, and caches and returns the result. `*args`, if present, are hashed and appended to key to permit the results of different calls to the same method to be conveniently stored and retrieved independently.
Note: if a ‘nil` or `false` value is ever stored in the cache, this method will consider any lookup of the corresponding key to be a miss, because we employ a simple truthiness check to determine presence.
28 29 30 31 32 |
# File 'lib/twigg/cacher.rb', line 28 def get(key, *args, &block) raise ArgumentError, 'block required by not given' unless block_given? digest = hashed_key_and_args(key, *args) client.get(digest) || set(key, *args, &block) end |
.set(key, *args, &block) ⇒ Object
Stores the result of yielding to ‘block` in the cache, with `key` as key, and returns the result.
As with get, any ‘*args`, if present, are hashed and appended to the key to premit the results of different calls to the same method to be conveniently stored and retrieved independently.
40 41 42 43 |
# File 'lib/twigg/cacher.rb', line 40 def set(key, *args, &block) digest = hashed_key_and_args(key, *args) yield.tap { |result| client.set(digest, result) } end |