summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy McCurdy <andy@andymccurdy.com>2010-05-10 23:57:57 -0700
committerAndy McCurdy <andy@andymccurdy.com>2010-05-10 23:57:57 -0700
commit2b07bd7ad65d4fdee11e1a3dba65180dd920cf75 (patch)
tree7f23c64b6c6327754c032048e4a0c2508aa5e811
parent09e2be4054debe073a4f351d3543d7fc48652833 (diff)
downloadredis-py-2b07bd7ad65d4fdee11e1a3dba65180dd920cf75.tar.gz
added missing EXPIREAT and SETEX commands
added a few missing tests
-rw-r--r--redis/client.py23
-rw-r--r--tests/server_commands.py34
2 files changed, 42 insertions, 15 deletions
diff --git a/redis/client.py b/redis/client.py
index 28dbf46..d6932d1 100644
--- a/redis/client.py
+++ b/redis/client.py
@@ -2,6 +2,7 @@ import datetime
import errno
import socket
import threading
+import time
import warnings
from itertools import chain
from redis.exceptions import ConnectionError, ResponseError, InvalidResponse
@@ -201,8 +202,8 @@ class Redis(threading.local):
"""
RESPONSE_CALLBACKS = dict_merge(
string_keys_to_dict(
- 'AUTH DEL EXISTS EXPIRE HDEL HEXISTS HMSET MOVE MSETNX RENAMENX '
- 'SADD SISMEMBER SMOVE SETNX SREM ZADD ZREM',
+ 'AUTH DEL EXISTS EXPIRE EXPIREAT HDEL HEXISTS HMSET MOVE MSETNX '
+ 'RENAMENX SADD SISMEMBER SMOVE SETEX SETNX SREM ZADD ZREM',
bool
),
string_keys_to_dict(
@@ -513,9 +514,18 @@ class Redis(threading.local):
__contains__ = exists
def expire(self, name, time):
- "Set an expire on key ``name`` for ``time`` seconds"
+ "Set an expire flag on key ``name`` for ``time`` seconds"
return self.execute_command('EXPIRE', name, time)
+ def expireat(self, name, when):
+ """
+ Set an expire flag on key ``name``. ``when`` can be represented
+ as an integer indicating unix time or a Python datetime object.
+ """
+ if isinstance(when, datetime.datetime):
+ when = int(time.mktime(when.timetuple()))
+ return self.execute_command('EXPIREAT', name, when)
+
def get(self, name):
"""
Return the value at key ``name``, or None of the key doesn't exist
@@ -621,6 +631,13 @@ class Redis(threading.local):
return self.execute_command('SET', name, value)
__setitem__ = set
+ def setex(self, name, value, time):
+ """
+ Set the value of key ``name`` to ``value``
+ that expires in ``time`` seconds
+ """
+ return self.execute_command('SETEX', name, time, value)
+
def setnx(self, name, value):
"Set the value of key ``name`` to ``value`` if key doesn't exist"
return self.execute_command('SETNX', name, value)
diff --git a/tests/server_commands.py b/tests/server_commands.py
index c1f4384..056188a 100644
--- a/tests/server_commands.py
+++ b/tests/server_commands.py
@@ -89,10 +89,24 @@ class ServerCommandsTestCase(unittest.TestCase):
self.client['a'] = 'foo'
self.assertEquals(self.client.exists('a'), True)
- def expire(self):
- self.assertEquals(self.client.expire('a'), False)
+ def test_expire_and_ttl(self):
+ self.assertEquals(self.client.expire('a', 10), False)
self.client['a'] = 'foo'
- self.assertEquals(self.client.expire('a'), True)
+ self.assertEquals(self.client.expire('a', 10), True)
+ self.assertEquals(self.client.ttl('a'), 10)
+
+ def test_expireat(self):
+ expire_at = datetime.datetime.now() + datetime.timedelta(minutes=1)
+ self.assertEquals(self.client.expireat('a', expire_at), False)
+ self.client['a'] = 'foo'
+ # expire at in unix time
+ expire_at_seconds = int(time.mktime(expire_at.timetuple()))
+ self.assertEquals(self.client.expireat('a', expire_at_seconds), True)
+ self.assertEquals(self.client.ttl('a'), 60)
+ # expire at given a datetime object
+ self.client['b'] = 'bar'
+ self.assertEquals(self.client.expireat('b', expire_at), True)
+ self.assertEquals(self.client.ttl('b'), 60)
def test_getset(self):
self.assertEquals(self.client.getset('a', 'foo'), None)
@@ -158,6 +172,11 @@ class ServerCommandsTestCase(unittest.TestCase):
self.assertEquals(self.client['a'], '1')
self.assertEquals(self.client['b'], '2')
+ def test_setex(self):
+ self.assertEquals(self.client.setex('a', '1', 60), True)
+ self.assertEquals(self.client['a'], '1')
+ self.assertEquals(self.client.ttl('a'), 60 )
+
def test_setnx(self):
self.assert_(self.client.setnx('a', '1'))
self.assertEquals(self.client['a'], '1')
@@ -178,15 +197,6 @@ class ServerCommandsTestCase(unittest.TestCase):
self.client['a'] = 123456 # does substr work with ints?
self.assertEquals(self.client.substr('a', 2, -2), '345')
- def test_ttl(self):
- self.assertEquals(self.client.ttl('a'), None)
- self.client['a'] = '1'
- self.assertEquals(self.client.ttl('a'), None)
- self.client.expire('a', 10)
- # this could potentially fail if for some reason there's a gap of
- # time between these commands.
- self.assertEquals(self.client.ttl('a'), 10)
-
def test_type(self):
self.assertEquals(self.client.type('a'), 'none')
self.client['a'] = '1'