Skip to content

Commit ab668ed

Browse files
committed
Removing "time" from delete and making expiry mandatory in touch.
Port of erankor's PR #30 to the latest code. In that PR, the "time" argument to delete() is removed because it is not supported by the protocol: https://github.com/memcached/memcached/blob/master/doc/protocol.txt#L330 And touch requires the expiry parameter due to protocol: https://github.com/memcached/memcached/blob/master/doc/protocol.txt#L399
1 parent 488b9a8 commit ab668ed

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

memcache.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -518,18 +518,36 @@ def delete_multi(self, keys, time=None, key_prefix='', noreply=False):
518518
rc = 0
519519
return rc
520520

521-
def delete(self, key, time=None, noreply=False):
521+
def delete(self, key, noreply=False):
522522
'''Deletes a key from the memcache.
523523
524524
@return: Nonzero on success.
525-
@param time: number of seconds any subsequent set / update commands
526-
should fail. Defaults to None for no delay.
527525
@param noreply: optional parameter instructs the server to not send the
528526
reply.
529527
@rtype: int
530528
'''
531-
return self._deletetouch([b'DELETED', b'NOT_FOUND'], "delete", key,
532-
time, noreply)
529+
key = self._encode_key(key)
530+
if self.do_check_key:
531+
self.check_key(key)
532+
server, key = self._get_server(key)
533+
if not server:
534+
return 0
535+
self._statlog('delete')
536+
fullcmd = self._encode_cmd('delete', key, None, noreply)
537+
538+
try:
539+
server.send_cmd(fullcmd)
540+
if noreply:
541+
return 1
542+
line = server.readline()
543+
if line and line.strip() in [b'DELETED', b'NOT_FOUND']:
544+
return 1
545+
self.debuglog('delete expected DELETED or NOT_FOUND, got: %r' % (line,))
546+
except socket.error as msg:
547+
if isinstance(msg, tuple):
548+
msg = msg[1]
549+
server.mark_dead(msg)
550+
return 0
533551

534552
def touch(self, key, time=0, noreply=False):
535553
'''Updates the expiration time of a key in memcache.
@@ -544,31 +562,23 @@ def touch(self, key, time=0, noreply=False):
544562
reply.
545563
@rtype: int
546564
'''
547-
return self._deletetouch([b'TOUCHED'], "touch", key, time, noreply)
548-
549-
def _deletetouch(self, expected, cmd, key, time=0, noreply=False):
550565
key = self._encode_key(key)
551566
if self.do_check_key:
552567
self.check_key(key)
553568
server, key = self._get_server(key)
554569
if not server:
555570
return 0
556-
self._statlog(cmd)
557-
if time is not None:
558-
headers = str(time)
559-
else:
560-
headers = None
561-
fullcmd = self._encode_cmd(cmd, key, headers, noreply)
571+
self._statlog('touch')
572+
fullcmd = self._encode_cmd('touch', key, str(time), noreply)
562573

563574
try:
564575
server.send_cmd(fullcmd)
565576
if noreply:
566577
return 1
567578
line = server.readline()
568-
if line and line.strip() in expected:
579+
if line and line.strip() in [b'TOUCHED']:
569580
return 1
570-
self.debuglog('%s expected %s, got: %r'
571-
% (cmd, b' or '.join(expected), line))
581+
self.debuglog('touch expected TOUCHED, got: %r' % (line,))
572582
except socket.error as msg:
573583
if isinstance(msg, tuple):
574584
msg = msg[1]

tests/test_memcache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def test_touch_unexpected_reply(self, mock_readline, mock_send_cmd):
245245
self.mc.touch('key')
246246
self.assertEqual(
247247
output.getvalue(),
248-
"MemCached: touch expected %s, got: 'SET'\n" % b'TOUCHED'
248+
"MemCached: touch expected %s, got: 'SET'\n" % 'TOUCHED'
249249
)
250250

251251

0 commit comments

Comments
 (0)