Module: Cash::Accessor::ClassMethods
- Defined in:
- lib/cash/accessor.rb
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
42 43 44 45 46 |
# File 'lib/cash/accessor.rb', line 42 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
72 73 74 |
# File 'lib/cash/accessor.rb', line 72 def cache_key(key) "#{name}:#{cache_config.version}/#{key.to_s.gsub(' ', '+')}" end |
#decr(key, delta = 1, ttl = nil) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/cash/accessor.rb', line 60 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
68 69 70 |
# File 'lib/cash/accessor.rb', line 68 def expire(key) repository.delete(cache_key(key)) end |
#fetch(keys, options = {}, &block) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/cash/accessor.rb', line 11 def fetch(keys, = {}, &block) case keys when Array return {} if keys.empty? keys = keys.collect { |key| cache_key(key) } hits = repository.get_multi(*keys) if (missed_keys = keys - hits.keys).any? missed_values = block.call(missed_keys) hits.merge!(missed_keys.zip(Array(missed_values)).to_hash_without_nils) end hits else repository.get(cache_key(keys), [:raw]) || (block ? block.call : nil) end end |
#get(keys, options = {}, &block) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/cash/accessor.rb', line 28 def get(keys, = {}, &block) case keys when Array fetch(keys, , &block) else fetch(keys, ) do if block_given? add(keys, result = yield(keys), ) result end end end end |
#incr(key, delta = 1, ttl = nil) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/cash/accessor.rb', line 52 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
48 49 50 |
# File 'lib/cash/accessor.rb', line 48 def set(key, value, = {}) repository.set(cache_key(key), value, [:ttl] || cache_config.ttl, [:raw]) end |