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
permalink #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
permalink #logging ⇒ Object
Returns the value of attribute logging.
54 55 56 |
# File 'lib/cash/mock.rb', line 54 def logging @logging end |
permalink #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
permalink #add(key, value, ttl = CacheEntry.default_ttl, raw = false) ⇒ Object
[View source]
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 |
permalink #append(key, value) ⇒ Object
[View source]
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 |
permalink #decr(key, amount = 1) ⇒ Object
[View source]
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 |
permalink #delete(key, options = {}) ⇒ Object
[View source]
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 |
permalink #flush_all ⇒ Object
[View source]
131 132 133 134 |
# File 'lib/cash/mock.rb', line 131 def flush_all log('< flush_all') clear end |
permalink #get(key, raw = false) ⇒ Object
[View source]
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 |
permalink #get_multi(keys) ⇒ Object
[View source]
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 |
permalink #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 |
permalink #incr(key, amount = 1) ⇒ Object
[View source]
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 |
permalink #log(message) ⇒ Object
[View source]
148 149 150 151 |
# File 'lib/cash/mock.rb', line 148 def log() return unless logging logger.debug() end |
permalink #logger ⇒ Object
[View source]
153 154 155 |
# File 'lib/cash/mock.rb', line 153 def logger @logger ||= ActiveSupport::BufferedLogger.new(Rails.root.join('log/cash_mock.log')) end |
permalink #namespace ⇒ Object
[View source]
127 128 129 |
# File 'lib/cash/mock.rb', line 127 def namespace nil end |
permalink #reset_runtime ⇒ Object
[View source]
140 141 142 |
# File 'lib/cash/mock.rb', line 140 def reset_runtime [0, Hash.new(0)] end |
permalink #set(key, value, ttl = CacheEntry.default_ttl, raw = false) ⇒ Object
[View source]
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 |
permalink #stats ⇒ Object
[View source]
136 137 138 |
# File 'lib/cash/mock.rb', line 136 def stats {} end |