Class: MemcachedWrapper

Inherits:
MemCache
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ MemcachedWrapper

See Memcached#new for details.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/memcached_wrapper.rb', line 24

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

#default_ttlObject (readonly)

Returns the value of attribute default_ttl.



21
22
23
# File 'lib/memcached_wrapper.rb', line 21

def default_ttl
  @default_ttl
end

#loggerObject (readonly)

Returns the value of attribute logger.



21
22
23
# File 'lib/memcached_wrapper.rb', line 21

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



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/memcached_wrapper.rb', line 63

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



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/memcached_wrapper.rb', line 168

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.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/memcached_wrapper.rb', line 121

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



211
212
213
214
215
216
# File 'lib/memcached_wrapper.rb', line 211

def decr(*args)
  super
rescue Memcached::NotFound
rescue Memcached::Error
  log_error($!) if logger
end

#delete(key) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
# File 'lib/memcached_wrapper.rb', line 192

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



109
110
111
112
113
114
115
116
117
118
# File 'lib/memcached_wrapper.rb', line 109

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.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/memcached_wrapper.rb', line 91

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



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/memcached_wrapper.rb', line 139

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



218
219
220
# File 'lib/memcached_wrapper.rb', line 218

def get_server_for_key(key, options = {})
  server_by_key(key)
end

#incr(*args) ⇒ Object



204
205
206
207
208
209
# File 'lib/memcached_wrapper.rb', line 204

def incr(*args)
  super
rescue Memcached::NotFound
rescue Memcached::Error
  log_error($!) if logger
end

#namespaceObject



58
59
60
# File 'lib/memcached_wrapper.rb', line 58

def namespace
  options[:prefix_key]
end

#prepend(key, value) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/memcached_wrapper.rb', line 180

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



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/memcached_wrapper.rb', line 76

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: []=



158
159
160
161
162
163
164
165
166
# File 'lib/memcached_wrapper.rb', line 158

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



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/memcached_wrapper.rb', line 45

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