summaryrefslogtreecommitdiff
path: root/redis/client.py
diff options
context:
space:
mode:
authorCraig Hawco <craig.hawco@gmail.com>2010-09-03 10:27:52 -0400
committerCraig Hawco <craig.hawco@gmail.com>2010-09-03 10:27:52 -0400
commit77422cac6c832f3726557ca397884fc3d415ec54 (patch)
tree1c1a0ac7faf2359d3464993bf699d0aa7b881c67 /redis/client.py
parent16c0eff87e7762d82fdd37d9949e33a7376d5357 (diff)
downloadredis-py-77422cac6c832f3726557ca397884fc3d415ec54.tar.gz
Add WATCH command to redis-py
Diffstat (limited to 'redis/client.py')
-rw-r--r--redis/client.py16
1 files changed, 14 insertions, 2 deletions
diff --git a/redis/client.py b/redis/client.py
index 0e3cac9..3719d36 100644
--- a/redis/client.py
+++ b/redis/client.py
@@ -5,7 +5,7 @@ import threading
import time
import warnings
from itertools import chain, imap
-from redis.exceptions import ConnectionError, ResponseError, InvalidResponse
+from redis.exceptions import ConnectionError, ResponseError, InvalidResponse, WatchError
from redis.exceptions import RedisError, AuthenticationError
@@ -219,7 +219,7 @@ class Redis(threading.local):
string_keys_to_dict('ZSCORE ZINCRBY', float_or_none),
string_keys_to_dict(
'FLUSHALL FLUSHDB LSET LTRIM MSET RENAME '
- 'SAVE SELECT SET SHUTDOWN',
+ 'SAVE SELECT SET SHUTDOWN WATCH',
lambda r: r == 'OK'
),
string_keys_to_dict('BLPOP BRPOP', lambda r: r and tuple(r) or None),
@@ -683,6 +683,14 @@ class Redis(threading.local):
"Returns the type of key ``name``"
return self.execute_command('TYPE', name)
+ def watch(self, name):
+ """
+ Watches the value at key ``name``, or None of the key doesn't exist
+ """
+ if self.subscribed:
+ raise RedisError("Can't call 'watch' from a pipeline'")
+
+ return self.execute_command('WATCH', name)
#### LIST COMMANDS ####
def blpop(self, keys, timeout=0):
@@ -1294,6 +1302,10 @@ class Pipeline(Redis):
_ = self.parse_response('_')
# parse the EXEC. we want errors returned as items in the response
response = self.parse_response('_', catch_errors=True)
+
+ if response is None:
+ raise WatchError("Watched variable changed.")
+
if len(response) != len(commands):
raise ResponseError("Wrong number of response items from "
"pipeline execution")