Skip to content

Separated touch and delete #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions memcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,15 +433,30 @@ def delete_multi(self, keys, time=0, key_prefix=''):
rc = 0
return rc

def delete(self, key, time=0):
def delete(self, key):
'''Deletes a key from the memcache.

@return: Nonzero on success.
@param time: number of seconds any subsequent set / update commands
should fail. Defaults to None for no delay.
@rtype: int
'''
return self._deletetouch(['DELETED','NOT_FOUND'], "delete", key, time)
if self.do_check_key:
self.check_key(key)
server, key = self._get_server(key)
if not server:
return 0
self._statlog('delete')
cmd = "delete %s" % key

try:
server.send_cmd(cmd)
line = server.readline()
if line and line.strip() in ['DELETED', 'NOT_FOUND']: return 1
self.debuglog('Delete expected DELETED or NOT_FOUND, got: %s'
% repr(line))
except socket.error, msg:
if isinstance(msg, tuple): msg = msg[1]
server.mark_dead(msg)
return 0

def touch(self, key, time=0):
'''Updates the expiration time of a key in memcache.
Expand All @@ -454,26 +469,20 @@ def touch(self, key, time=0):
default to 0 == cache forever.
@rtype: int
'''
return self._deletetouch(['TOUCHED'], "touch", key, time)

def _deletetouch(self, expected, cmd, key, time=0):
if self.do_check_key:
self.check_key(key)
server, key = self._get_server(key)
if not server:
return 0
self._statlog(cmd)
if time != None and time != 0:
cmd = "%s %s %d" % (cmd, key, time)
else:
cmd = "%s %s" % (cmd, key)
self._statlog('touch')
cmd = "touch %s %d" % (key, time)

try:
server.send_cmd(cmd)
line = server.readline()
if line and line.strip() in expected: return 1
self.debuglog('%s expected %s, got: %s'
% (cmd, ' or '.join(expected), repr(line)))
if line and line.strip() in ['TOUCHED']: return 1
self.debuglog('Touch expected TOUCHED, got: %s'
% (repr(line)))
except socket.error, msg:
if isinstance(msg, tuple): msg = msg[1]
server.mark_dead(msg)
Expand Down