Class: MyStuff::Cache::Base
- Inherits:
-
Object
- Object
- MyStuff::Cache::Base
- Defined in:
- lib/my_stuff/cache/base.rb
Overview
Base implementation.
You probably want a subclass instead:
-
MyStuff::Cache::NullCache (not likely)
-
MyStuff::Cache::MemoryCache
-
MyStuff::Cache::MemcachedCache
Direct Known Subclasses
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
-
#get(keys, options = {}) ⇒ Object
Fetch values from cache.
-
#get_with_fallback(ids, key_pattern, options = {}, &fallback) ⇒ Object
Try to get ids from cache, falling back to the given block.
-
#get_with_multi_fallback(ids, key_pattern, options = {}, &fallback) ⇒ Object
Try to get ids from cache, falling back to to the given block.
-
#initialize(options = {}) ⇒ Base
constructor
A new instance of Base.
-
#set(values, options = {}) ⇒ Object
Set values in the cache.
Constructor Details
#initialize(options = {}) ⇒ Base
Returns a new instance of Base.
13 14 15 |
# File 'lib/my_stuff/cache/base.rb', line 13 def initialize = {} @logger = [:logger] || create_logger end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
12 13 14 |
# File 'lib/my_stuff/cache/base.rb', line 12 def logger @logger end |
Instance Method Details
#get(keys, options = {}) ⇒ Object
Fetch values from cache.
Returns an array of values. If a key is not in the cache, nil is returned instead.
keys is an array of cache keys.
Currently, no options are supported.
76 77 78 |
# File 'lib/my_stuff/cache/base.rb', line 76 def get keys, = {} raise NotImplementedError.new end |
#get_with_fallback(ids, key_pattern, options = {}, &fallback) ⇒ Object
Try to get ids from cache, falling back to the given block.
See #get_with_multi_fallback for documentation.
Unlike get_with_multi_fallback, fallback gets called once for each id. You almost certainly want to use #get_with_multi_fallback instead.
24 25 26 27 28 29 |
# File 'lib/my_stuff/cache/base.rb', line 24 def get_with_fallback ids, key_pattern, = {}, &fallback get_with_multi_fallback(ids, key_pattern, ) do |ids| result = ids.map{|id| fallback.call(id) } result end end |
#get_with_multi_fallback(ids, key_pattern, options = {}, &fallback) ⇒ Object
Try to get ids from cache, falling back to to the given block.
-
idsis an array of ids -
key_patternis a printf string to turn an id into a cache key
The following options are supported:
update_cache-
If fallback is called, cache the result (default true)
force_update-
Fetch from the database, even if there’s a value in cache.
ttl: Keep fetched values in cache for the specified number of
seconds. Defaults to forever (0). May be completely ignored.
The block gets passed the list of ids which missed the cache.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/my_stuff/cache/base.rb', line 45 def get_with_multi_fallback ids, key_pattern, = {}, &fallback = { :update_cache => true, }.merge() to_cache = Hash.new if [:force_update] data = Hash[ids.zip([nil] * ids.size)] else data = Hash[ids.zip(get(ids.map{|x| key_pattern % x}))] end misses = data.select{|k,v| !v}.keys unless misses.empty? from_fallback = fallback.call(misses) Hash[misses.zip(from_fallback)].each do |k,v| to_cache[key_pattern % k] = v data[k] = v; end end set(to_cache) unless to_cache.empty? || ![:update_cache] ids.map{|k| data[k]} end |
#set(values, options = {}) ⇒ Object
Set values in the cache.
values is a hash of cache key => values.
The following options are supported: ttl: Keep fetched values in cache for the specified number of
seconds. Defaults to forever (0). May be completely ignored.
87 88 89 |
# File 'lib/my_stuff/cache/base.rb', line 87 def set values, = {} raise NotImplementedError.new end |