Class: Cash::Mock
- Inherits:
-
HashWithIndifferentAccess
- Object
- HashWithIndifferentAccess
- Cash::Mock
- Defined in:
- lib/cash/mock.rb
Defined Under Namespace
Classes: CacheEntry
Instance Attribute Summary collapse
-
#logging ⇒ Object
Returns the value of attribute logging.
-
#servers ⇒ Object
Returns the value of attribute servers.
Instance Method Summary collapse
- #add(key, value, ttl = CacheEntry.default_ttl, raw = false) ⇒ Object
- #append(key, value) ⇒ Object
- #decr(key, amount = 1) ⇒ Object
- #delete(key, options = {}) ⇒ Object
- #flush_all ⇒ Object
- #get(key, raw = false) ⇒ Object
- #get_multi(keys) ⇒ Object
- #has_unexpired_key?(key) ⇒ Boolean
- #incr(key, amount = 1) ⇒ Object
-
#initialize ⇒ Mock
constructor
A new instance of Mock.
- #log(message) ⇒ Object
- #logger ⇒ Object
- #namespace ⇒ Object
- #reset_runtime ⇒ Object
- #set(key, value, ttl = CacheEntry.default_ttl, raw = false) ⇒ Object
- #stats ⇒ Object
Constructor Details
#initialize ⇒ Mock
Returns a new instance of Mock.
56 57 58 |
# File 'lib/cash/mock.rb', line 56 def initialize @logging = false end |
Instance Attribute Details
#logging ⇒ Object
Returns the value of attribute logging.
54 55 56 |
# File 'lib/cash/mock.rb', line 54 def logging @logging end |
#servers ⇒ Object
Returns the value of attribute servers.
3 4 5 |
# File 'lib/cash/mock.rb', line 3 def servers @servers end |
Instance Method Details
#add(key, value, ttl = CacheEntry.default_ttl, raw = false) ⇒ Object
114 115 116 117 118 119 120 121 |
# File 'lib/cash/mock.rb', line 114 def add(key, value, ttl = CacheEntry.default_ttl, raw = false) if self.has_unexpired_key?(key) "NOT_STORED\r\n" else set(key, value, ttl, raw) "STORED\r\n" end end |
#append(key, value) ⇒ Object
123 124 125 |
# File 'lib/cash/mock.rb', line 123 def append(key, value) set(key, get(key, true).to_s + value.to_s, nil, true) end |
#decr(key, amount = 1) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/cash/mock.rb', line 107 def decr(key, amount = 1) if self.has_unexpired_key?(key) self[key].decrement(amount) self[key].to_i end end |
#delete(key, options = {}) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/cash/mock.rb', line 90 def delete(key, = {}) log "< delete #{key}" if self.has_unexpired_key?(key) log "> DELETED" super(key) else log "> NOT FOUND" end end |
#flush_all ⇒ Object
131 132 133 134 |
# File 'lib/cash/mock.rb', line 131 def flush_all log('< flush_all') clear end |
#get(key, raw = false) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/cash/mock.rb', line 70 def get(key, raw = false) if key.is_a?(Array) get_multi(*key) else log "< get #{key}" unless self.has_unexpired_key?(key) log('> END') return nil end log("> sending key #{key}") log('> END') if raw self[key].value else self[key].unmarshal end end end |
#get_multi(keys) ⇒ Object
60 61 62 |
# File 'lib/cash/mock.rb', line 60 def get_multi(keys) slice(*keys).collect { |k,v| [k, v.unmarshal] }.to_hash_without_nils end |
#has_unexpired_key?(key) ⇒ Boolean
144 145 146 |
# File 'lib/cash/mock.rb', line 144 def has_unexpired_key?(key) self.has_key?(key) && !self[key].expired? end |
#incr(key, amount = 1) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/cash/mock.rb', line 100 def incr(key, amount = 1) if self.has_unexpired_key?(key) self[key].increment(amount) self[key].to_i end end |
#log(message) ⇒ Object
148 149 150 151 |
# File 'lib/cash/mock.rb', line 148 def log() return unless logging logger.debug() end |
#logger ⇒ Object
153 154 155 |
# File 'lib/cash/mock.rb', line 153 def logger @logger ||= ActiveSupport::BufferedLogger.new(Rails.root.join('log/cash_mock.log')) end |
#namespace ⇒ Object
127 128 129 |
# File 'lib/cash/mock.rb', line 127 def namespace nil end |
#reset_runtime ⇒ Object
140 141 142 |
# File 'lib/cash/mock.rb', line 140 def reset_runtime [0, Hash.new(0)] end |
#set(key, value, ttl = CacheEntry.default_ttl, raw = false) ⇒ Object
64 65 66 67 68 |
# File 'lib/cash/mock.rb', line 64 def set(key, value, ttl = CacheEntry.default_ttl, raw = false) log "< set #{key} #{ttl}" self[key] = CacheEntry.new(value, raw, ttl) log('> STORED') end |
#stats ⇒ Object
136 137 138 |
# File 'lib/cash/mock.rb', line 136 def stats {} end |