summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean Reifschneider <jafo@tummy.com>2014-02-08 13:57:23 -0700
committerSean Reifschneider <jafo@tummy.com>2014-02-08 13:57:23 -0700
commit6b0dc409e8fda4eefc690806adfa90a6793b3ff6 (patch)
tree9548f5adacb4dedbd66abdffa6d6dca430f81815
parentdb92520d9c1f9c53fb88b1e7d560079ef28e6a6c (diff)
parentf769de86d9722d7e3ffda3a52354c9613d93151f (diff)
downloadpython-memcached-6b0dc409e8fda4eefc690806adfa90a6793b3ff6.tar.gz
Merge pull request #28 from erankor/master
add support for touch command
-rw-r--r--memcache.py28
1 files changed, 22 insertions, 6 deletions
diff --git a/memcache.py b/memcache.py
index 5a65d55..8031929 100644
--- a/memcache.py
+++ b/memcache.py
@@ -441,23 +441,39 @@ class Client(local):
should fail. Defaults to None for no delay.
@rtype: int
'''
+ return self._deletetouch(['DELETED','NOT_FOUND'], "delete", key, time)
+
+ def touch(self, key, time=0):
+ '''Updates the expiration time of a key in memcache.
+
+ @return: Nonzero on success.
+ @param time: Tells memcached the time which this value should
+ expire, either as a delta number of seconds, or an absolute
+ unix time-since-the-epoch value. See the memcached protocol
+ docs section "Storage Commands" for more info on <exptime>. We
+ 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('delete')
+ self._statlog(cmd)
if time != None and time != 0:
- cmd = "delete %s %d" % (key, time)
+ cmd = "%s %s %d" % (cmd, key, time)
else:
- cmd = "delete %s" % key
+ cmd = "%s %s" % (cmd, 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))
+ if line and line.strip() in expected: return 1
+ self.debuglog('%s expected %s, got: %s'
+ % (cmd, ' or '.join(expected), repr(line)))
except socket.error, msg:
if isinstance(msg, tuple): msg = msg[1]
server.mark_dead(msg)