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
110 111 112 113 114 115 116 117 |
# File 'lib/cash/mock.rb', line 110 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
119 120 121 |
# File 'lib/cash/mock.rb', line 119 def append(key, value) set(key, get(key, true).to_s + value.to_s, nil, true) end |
#decr(key, amount = 1) ⇒ Object
103 104 105 106 107 108 |
# File 'lib/cash/mock.rb', line 103 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
86 87 88 89 90 91 92 93 94 |
# File 'lib/cash/mock.rb', line 86 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
127 128 129 130 |
# File 'lib/cash/mock.rb', line 127 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 |
# File 'lib/cash/mock.rb', line 70 def get(key, raw = false) 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 |
#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
140 141 142 |
# File 'lib/cash/mock.rb', line 140 def has_unexpired_key?(key) self.has_key?(key) && !self[key].expired? end |
#incr(key, amount = 1) ⇒ Object
96 97 98 99 100 101 |
# File 'lib/cash/mock.rb', line 96 def incr(key, amount = 1) if self.has_unexpired_key?(key) self[key].increment(amount) self[key].to_i end end |
#log(message) ⇒ Object
144 145 146 147 |
# File 'lib/cash/mock.rb', line 144 def log() return unless logging logger.debug() end |
#logger ⇒ Object
149 150 151 |
# File 'lib/cash/mock.rb', line 149 def logger @logger ||= ActiveSupport::BufferedLogger.new(Rails.root.join('log/cash_mock.log')) end |
#namespace ⇒ Object
123 124 125 |
# File 'lib/cash/mock.rb', line 123 def namespace nil end |
#reset_runtime ⇒ Object
136 137 138 |
# File 'lib/cash/mock.rb', line 136 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
132 133 134 |
# File 'lib/cash/mock.rb', line 132 def stats {} end |