Skip to content
Browse files

add touch() support

  • Loading branch information...
1 parent 7262727 commit 581559f081841632bc30aa40dde5041063ba123d @davies davies committed May 30, 2012
Showing with 35 additions and 4 deletions.
  1. +23 −4 cmemcached_imp.pyx
  2. +12 −0 cmemcached_test.py
View
27 cmemcached_imp.pyx
@@ -223,7 +223,7 @@ cdef extern from "libmemcached/memcached.h":
time_t expiration)
memcached_return memcached_mget(memcached_st *ptr,
char **keys, size_t *key_length,
- unsigned int number_of_keys)
+ size_t number_of_keys)
char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length,
size_t *value_length, uint32_t *flags,
memcached_return *error)
@@ -259,6 +259,8 @@ cdef extern from "libmemcached/memcached.h":
char *value, size_t value_length,
time_t expiration,
uint32_t flags)
+ memcached_return memcached_touch(memcached_st *ptr, char *key,
+ size_t key_length, time_t expiration)
memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_return *error)
void memcached_stat_free(memcached_st *ptr, memcached_stat_st *stat)
uint32_t memcached_generate_hash(memcached_st *ptr, char *key, size_t key_length)
@@ -644,14 +646,15 @@ cdef class Client:
cdef char *c_key
cdef memcached_return retval
cdef PyThreadState *_save
+
+ if not self.check_key(key):
+ return 0
PyString_AsStringAndSize(key, &c_key, &key_len)
- if key_len >= MEMCACHED_MAX_KEY:
- return 0
-
_save = PyEval_SaveThread()
retval = memcached_delete(self.mc, c_key, key_len, time)
PyEval_RestoreThread(_save)
+
return retval in (MEMCACHED_SUCCESS, MEMCACHED_NOTFOUND)
def delete_multi(self, keys, time_t time=0):
@@ -673,6 +676,22 @@ cdef class Client:
return retval == MEMCACHED_SUCCESS
+ def touch(self, key, time_t exptime):
+ cdef Py_ssize_t key_len
+ cdef char *c_key
+ cdef memcached_return retval
+ cdef PyThreadState *_save
+
+ if not self.check_key(key):
+ return 0
+
+ PyString_AsStringAndSize(key, &c_key, &key_len)
+ _save = PyEval_SaveThread()
+ retval = memcached_touch(self.mc, c_key, key_len, exptime)
+ PyEval_RestoreThread(_save)
+
+ return retval == MEMCACHED_SUCCESS
+
def get_raw(self, key):
cdef char *c_key
cdef Py_ssize_t key_len
View
12 cmemcached_test.py
@@ -295,6 +295,18 @@ def test_client_pickable(self):
self.mc = pickle.loads(d)
self.test_stats()
+ def test_touch(self):
+ self.mc.set('test', 1)
+ self.assertEqual(self.mc.get('test'), 1)
+ self.assertEqual(self.mc.touch('test', -1), 1)
+ self.assertEqual(self.mc.get('test'), None)
+
+ self.mc.set('test', 1)
+ self.assertEqual(self.mc.get('test'), 1)
+ self.assertEqual(self.mc.touch('test', 1), 1)
+ time.sleep(1)
+ self.assertEqual(self.mc.get('test'), None)
+
class TestUnixSocketCmemcached(TestCmemcached):

0 comments on commit 581559f

Please sign in to comment.
Something went wrong with that request. Please try again.