Skip to content

Commit

Permalink
Implement LPUSHX and RPUSHX.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdavisp3 committed Jun 3, 2011
1 parent f3baa14 commit bf87f92
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
31 changes: 24 additions & 7 deletions txredis/protocol.py
Expand Up @@ -591,6 +591,8 @@ def unwatch(self, *keys):
# List Commands:
# RPUSH
# LPUSH
# RPUSHX
# LPUSHX
# LLEN
# LRANGE
# LTRIM
Expand All @@ -603,22 +605,29 @@ def unwatch(self, *keys):
# BRPOP
# RPOPLPUSH
# SORT
def push(self, key, value, tail=False):
def push(self, key, value, tail=False, no_create=False):
"""
@param key Redis key
@param value String element of list
Add the string value to the head (LPUSH) or tail (RPUSH) of the
list stored at key key. If the key does not exist an empty list is
created just before the append operation. If the key exists but is
not a List an error is returned.
Add the string value to the head (LPUSH/LPUSHX) or tail
(RPUSH/RPUSHX) of the list stored at key key. If the key does
not exist and no_create is False (the default) an empty list
is created just before the append operation. If the key exists
but is not a List an error is returned.
@note Time complexity: O(1)
"""
if tail:
return self.rpush(key, value)
if no_create:
return self.rpushx(key, value)
else:
return self.rpush(key, value)
else:
return self.lpush(key, value)
if no_create:
return self.lpushx(key, value)
else:
return self.lpush(key, value)

def lpush(self, key, value):
self._send('LPUSH', key, value)
Expand All @@ -628,6 +637,14 @@ def rpush(self, key, value):
self._send('RPUSH', key, value)
return self.getResponse()

def lpushx(self, key, value):
self._send('LPUSHX', key, value)
return self.getResponse()

def rpushx(self, key, value):
self._send('RPUSHX', key, value)
return self.getResponse()

def llen(self, key):
"""
@param key Redis key
Expand Down
9 changes: 9 additions & 0 deletions txredis/test/test_redis.py
Expand Up @@ -650,6 +650,15 @@ def test_push(self):
ex = 'OK'
t(a, ex)

yield r.delete('l')
a = yield r.push('l', 'a', no_create=True)
ex = 0
t(a, ex)

a = yield r.push('l', 'a', tail=True, no_create=True)
ex = 0
t(a, ex)

@defer.inlineCallbacks
def test_llen(self):
r = self.redis
Expand Down

0 comments on commit bf87f92

Please sign in to comment.