Class: AwsDevUtils::Cache
- Inherits:
-
Object
- Object
- AwsDevUtils::Cache
- Extended by:
- Forwardable
- Includes:
- Singleton
- Defined in:
- lib/aws-dev-utils/cache.rb
Instance Attribute Summary collapse
-
#backend ⇒ Object
writeonly
Injectable backend.
Instance Method Summary collapse
-
#fetch(key, exp = 60, &block) ⇒ Object
Returns a value from the cache for the given key.
-
#get(key) ⇒ Object
Get the value of key.
-
#set(key, value, expiration) ⇒ Object
Set key to hold the value and set key to timeout after the a given expiration time(in seconds).
Instance Attribute Details
#backend=(value) ⇒ Object
Injectable backend.
9 10 11 |
# File 'lib/aws-dev-utils/cache.rb', line 9 def backend=(value) @backend = value end |
Instance Method Details
#fetch(key, exp = 60, &block) ⇒ Object
Returns a value from the cache for the given key. If the key can’t be found, the block will be run and its result returned and be set in the cache.
22 23 24 |
# File 'lib/aws-dev-utils/cache.rb', line 22 def fetch key, exp=60, &block get(key) or block.().tap {|x| set(key, x, exp)} end |
#get(key) ⇒ Object
Get the value of key. If not found, returns nil
27 28 29 |
# File 'lib/aws-dev-utils/cache.rb', line 27 def get key deserialize backend.get key.to_s rescue nil end |
#set(key, value, expiration) ⇒ Object
Set key to hold the value and set key to timeout after the a given expiration time(in seconds).
35 36 37 |
# File 'lib/aws-dev-utils/cache.rb', line 35 def set key, value, expiration backend.set key.to_s, serialize(value), expiration rescue nil end |