Class: MemcachedWrapper
- Defined in:
- lib/memcached_wrapper.rb,
lib/memcached_wrapper.rb
Overview
TODO add logging?
Constant Summary collapse
- DEFAULTS =
{ :servers => '127.0.0.1:11211' }
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
-
#add(key, value, ttl = @default_ttl, raw = false) ⇒ Object
Wraps Memcached::Rails#add to return a text string - for cache money.
- #append(key, value) ⇒ Object
-
#cas(key, ttl = @default_ttl, raw = false, &block) ⇒ Object
(also: #compare_and_swap)
Wraps Memcached#cas so that it doesn’t raise.
- #decr(*args) ⇒ Object
- #delete(key) ⇒ Object
- #fetch(key, expiry = 0, raw = false) ⇒ Object
-
#get(key, raw = false) ⇒ Object
(also: #[])
Wraps Memcached#get so that it doesn’t raise.
- #get_multi(*keys) ⇒ Object
- #get_server_for_key(key, options = {}) ⇒ Object
- #incr(*args) ⇒ Object
-
#initialize(*args) ⇒ MemcachedWrapper
constructor
See Memcached#new for details.
- #namespace ⇒ Object
- #prepend(key, value) ⇒ Object
- #replace(key, value, ttl = @default_ttl, raw = false) ⇒ Object
- #set(key, value, ttl = @default_ttl, raw = false) ⇒ Object (also: #[]=)
- #symbolize_keys(opts) ⇒ Object
Constructor Details
#initialize(*args) ⇒ MemcachedWrapper
See Memcached#new for details.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/memcached_wrapper.rb', line 32 def initialize(*args) opts = DEFAULTS.merge(args.last.is_a?(Hash) ? args.pop : {}) if opts.respond_to?(:symbolize_keys!) opts.symbolize_keys! else opts = symbolize_keys(opts) end servers = Array( args.any? ? args.unshift : opts.delete(:servers) ).flatten.compact opts[:prefix_key] ||= "#{opts[:namespace]}:" @logger = opts[:logger] @debug = opts[:debug] super(servers, opts) end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
29 30 31 |
# File 'lib/memcached_wrapper.rb', line 29 def logger @logger end |
Instance Method Details
#add(key, value, ttl = @default_ttl, raw = false) ⇒ Object
Wraps Memcached::Rails#add to return a text string - for cache money
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/memcached_wrapper.rb', line 71 def add(key, value, ttl=@default_ttl, raw=false) logger.debug("Memcached add: #{key.inspect}") if logger && @debug super(key, value, ttl, !raw) logger.debug("Memcached hit: #{key.inspect}") if logger && @debug stored rescue Memcached::NotStored logger.debug("Memcached miss: #{key.inspect}") if logger && @debug not_stored rescue Memcached::Error log_error($!) if logger not_stored end |
#append(key, value) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/memcached_wrapper.rb', line 176 def append(key, value) logger.debug("Memcached append: #{key.inspect}") if logger && @debug super(key, value) logger.debug("Memcached hit: #{key.inspect}") if logger && @debug stored rescue Memcached::NotStored logger.debug("Memcached miss: #{key.inspect}") if logger && @debug not_stored rescue Memcached::Error log_error($!) if logger end |
#cas(key, ttl = @default_ttl, raw = false, &block) ⇒ Object Also known as: compare_and_swap
Wraps Memcached#cas so that it doesn’t raise. Doesn’t set anything if no value is present.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/memcached_wrapper.rb', line 129 def cas(key, ttl=@default_ttl, raw=false, &block) logger.debug("Memcached cas: #{key.inspect}") if logger && @debug super(key, ttl, !raw, &block) logger.debug("Memcached hit: #{key.inspect}") if logger && @debug stored rescue Memcached::NotFound logger.debug("Memcached miss: #{key.inspect}") if logger && @debug rescue TypeError log_error($!) if logger delete(key) logger.debug("Memcached deleted: #{key.inspect}") if logger && @debug rescue Memcached::Error if $!.is_a?(Memcached::ClientError) raise $! end log_error($!) if logger end |
#decr(*args) ⇒ Object
219 220 221 222 223 224 |
# File 'lib/memcached_wrapper.rb', line 219 def decr(*args) super rescue Memcached::NotFound rescue Memcached::Error log_error($!) if logger end |
#delete(key) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/memcached_wrapper.rb', line 200 def delete(key) logger.debug("Memcached delete: #{key.inspect}") if logger && @debug super(key) logger.debug("Memcached hit: #{key.inspect}") if logger && @debug deleted rescue Memcached::NotFound logger.debug("Memcached miss: #{key.inspect}") if logger && @debug not_found rescue Memcached::Error log_error($!) if logger end |
#fetch(key, expiry = 0, raw = false) ⇒ Object
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/memcached_wrapper.rb', line 117 def fetch(key, expiry = 0, raw = false) value = get(key, !raw) if value.nil? && block_given? value = yield add(key, value, expiry, !raw) end value end |
#get(key, raw = false) ⇒ Object Also known as: []
Wraps Memcached#get so that it doesn’t raise. This has the side-effect of preventing you from storing nil
values.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/memcached_wrapper.rb', line 99 def get(key, raw=false) logger.debug("Memcached get: #{key.inspect}") if logger && @debug value = super(key, !raw) logger.debug("Memcached hit: #{key.inspect}") if logger && @debug value rescue Memcached::NotFound logger.debug("Memcached miss: #{key.inspect}") if logger && @debug nil rescue TypeError log_error($!) if logger delete(key) logger.debug("Memcached deleted: #{key.inspect}") if logger && @debug nil rescue Memcached::Error log_error($!) if logger nil end |
#get_multi(*keys) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/memcached_wrapper.rb', line 147 def get_multi(*keys) keys.flatten! logger.debug("Memcached get_multi: #{keys.inspect}") if logger && @debug values = super(keys, true) logger.debug("Memcached hit: #{keys.inspect}") if logger && @debug values rescue Memcached::NotFound logger.debug("Memcached miss: #{keys.inspect}") if logger && @debug {} rescue TypeError log_error($!) if logger keys.each { |key| delete(key) } logger.debug("Memcached deleted: #{keys.inspect}") if logger && @debug {} rescue Memcached::Error log_error($!) if logger {} end |
#get_server_for_key(key, options = {}) ⇒ Object
226 227 228 |
# File 'lib/memcached_wrapper.rb', line 226 def get_server_for_key(key, = {}) server_by_key(key) end |
#incr(*args) ⇒ Object
212 213 214 215 216 217 |
# File 'lib/memcached_wrapper.rb', line 212 def incr(*args) super rescue Memcached::NotFound rescue Memcached::Error log_error($!) if logger end |
#namespace ⇒ Object
66 67 68 |
# File 'lib/memcached_wrapper.rb', line 66 def namespace [:prefix_key] end |
#prepend(key, value) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/memcached_wrapper.rb', line 188 def prepend(key, value) logger.debug("Memcached prepend: #{key.inspect}") if logger && @debug super(key, value) logger.debug("Memcached hit: #{key.inspect}") if logger && @debug stored rescue Memcached::NotStored logger.debug("Memcached miss: #{key.inspect}") if logger && @debug not_stored rescue Memcached::Error log_error($!) if logger end |
#replace(key, value, ttl = @default_ttl, raw = false) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/memcached_wrapper.rb', line 84 def replace(key, value, ttl = @default_ttl, raw = false) logger.debug("Memcached replace: #{key.inspect}") if logger && @debug super(key, value, ttl, !raw) logger.debug("Memcached hit: #{key.inspect}") if logger && @debug stored rescue Memcached::NotStored logger.debug("Memcached miss: #{key.inspect}") if logger && @debug not_stored rescue Memcached::Error log_error($!) if logger not_stored end |
#set(key, value, ttl = @default_ttl, raw = false) ⇒ Object Also known as: []=
166 167 168 169 170 171 172 173 174 |
# File 'lib/memcached_wrapper.rb', line 166 def set(key, value, ttl=@default_ttl, raw=false) logger.debug("Memcached set: #{key.inspect}") if logger && @debug super(key, value, ttl, !raw) logger.debug("Memcached hit: #{key.inspect}") if logger && @debug stored rescue Memcached::Error log_error($!) if logger not_stored end |
#symbolize_keys(opts) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/memcached_wrapper.rb', line 53 def symbolize_keys(opts) # Destructively convert all keys to symbols. if opts.kind_of?(Hash) && !opts.kind_of?(HashWithIndifferentAccess) opts.keys.each do |key| unless key.is_a?(Symbol) opts[key.to_sym] = opts[key] opts.delete(key) end end end opts end |