~notmyname/swift/stats_mapper

« back to all changes in this revision

Viewing changes to swift/common/memcached.py

  • Committer: Tarmac
  • Author(s): David Goetz
  • Date: 2010-10-21 15:22:30 UTC
  • mfrom: (104.2.2 fix_ratelimit)
  • Revision ID: hudson@openstack.org-20101021152230-pur20ej7f7igezea
Bug fix: Memcached does not allow negative numbers passed to incr.  Adding/using decr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
190
190
            except Exception, e:
191
191
                self._exception_occurred(server, e)
192
192
 
 
193
    def decr(self, key, delta=1, timeout=0):
 
194
        """
 
195
        Decrements a key which has a numeric value by delta.
 
196
        If the key can't be found, it's added as 0.  Memcached
 
197
        will treat data values below 0 as 0 with incr/decr.
 
198
 
 
199
        :param key: key
 
200
        :param delta: amount to subtract to the value of key (or set
 
201
                      as the value if the key is not found)
 
202
        :param timeout: ttl in memcache
 
203
        """
 
204
        key = md5hash(key)
 
205
        for (server, fp, sock) in self._get_conns(key):
 
206
            try:
 
207
                sock.sendall('decr %s %s\r\n' % (key, delta))
 
208
                line = fp.readline().strip().split()
 
209
                if line[0].upper() == 'NOT_FOUND':
 
210
                    line[0] = '0'
 
211
                    sock.sendall('add %s %d %d %s noreply\r\n%s\r\n' %
 
212
                                 (key, 0, timeout, len(line[0]), line[0]))
 
213
                ret = int(line[0].strip())
 
214
                self._return_conn(server, fp, sock)
 
215
                return ret
 
216
            except Exception, e:
 
217
                self._exception_occurred(server, e)
 
218
 
193
219
    def delete(self, key):
194
220
        """
195
221
        Deletes a key/value pair from memcache.