Module: Cash::Accessor::ClassMethods
- Defined in:
- lib/cash/accessor.rb
Constant Summary collapse
- NIL_CACHE_VALUE =
'NIL_CACHE_VALUE'
Instance Method Summary collapse
- #add(key, value, options = {}) ⇒ Object
- #cache_key(key) ⇒ Object
- #decr(key, delta = 1, ttl = nil) ⇒ Object
- #expire(key) ⇒ Object
- #fetch(keys, options = {}, &block) ⇒ Object
- #get(keys, options = {}, &block) ⇒ Object
- #incr(key, delta = 1, ttl = nil) ⇒ Object
- #set(key, value, options = {}) ⇒ Object
Instance Method Details
#add(key, value, options = {}) ⇒ Object
54 55 56 57 58 |
# File 'lib/cash/accessor.rb', line 54 def add(key, value, = {}) if repository.add(cache_key(key), value, [:ttl] || cache_config.[:ttl], [:raw]) == "NOT_STORED\r\n" yield if block_given? end end |
#cache_key(key) ⇒ Object
84 85 86 87 |
# File 'lib/cash/accessor.rb', line 84 def cache_key(key) ready = key =~ /#{name}:#{cache_config.version}/ ready ? key : "#{name}:#{cache_config.version}/#{key.to_s.gsub(' ', '+')}" end |
#decr(key, delta = 1, ttl = nil) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/cash/accessor.rb', line 72 def decr(key, delta = 1, ttl = nil) ttl ||= cache_config.[:ttl] repository.decr(cache_key = cache_key(key), delta) || begin repository.add(cache_key, (result = yield).to_s, ttl, true) { repository.decr(cache_key) } result end end |
#expire(key) ⇒ Object
80 81 82 |
# File 'lib/cash/accessor.rb', line 80 def expire(key) repository.delete(cache_key(key)) end |
#fetch(keys, options = {}, &block) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/cash/accessor.rb', line 13 def fetch(keys, = {}, &block) case keys when Array cache_and_actual_keys = keys.inject({}) { |memo, key| memo[cache_key(key)] = key; memo } cache_keys = keys.collect {|key| cache_key(key)} hits = repository.get_multi(cache_keys) if (missed_cache_keys = cache_keys - hits.keys).any? actual_missed_keys = missed_cache_keys.collect {|missed_cache_key| cache_and_actual_keys[missed_cache_key]} misses = block.call(actual_missed_keys) hits.merge!(misses) end hits else repository.get(cache_key(keys), [:raw]) || (block ? block.call : nil) end end |
#get(keys, options = {}, &block) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/cash/accessor.rb', line 32 def get(keys, = {}, &block) case keys when Array results = fetch(keys, ) do |missed_keys| results = yield(missed_keys) results.each {|key, value| add(key, wrap_nil(value), )} results end results.each { |key, result| results[key] = unwrap_nil(result) } else result = fetch(keys, ) do if block_given? result = yield(keys) value = result.is_a?(Hash) ? result[cache_key(keys)] : result add(keys, wrap_nil(value), ) result end end unwrap_nil(result) end end |
#incr(key, delta = 1, ttl = nil) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/cash/accessor.rb', line 64 def incr(key, delta = 1, ttl = nil) ttl ||= cache_config.[:ttl] repository.incr(cache_key = cache_key(key), delta) || begin repository.add(cache_key, (result = yield).to_s, ttl, true) { repository.incr(cache_key) } result end end |
#set(key, value, options = {}) ⇒ Object
60 61 62 |
# File 'lib/cash/accessor.rb', line 60 def set(key, value, = {}) repository.set(cache_key(key), value, [:ttl] || cache_config.[:ttl], [:raw]) end |