diff options
-rw-r--r-- | redis/client.py | 16 | ||||
-rw-r--r-- | redis/exceptions.py | 14 |
2 files changed, 22 insertions, 8 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") diff --git a/redis/exceptions.py b/redis/exceptions.py index d3449b6..b3257ac 100644 --- a/redis/exceptions.py +++ b/redis/exceptions.py @@ -2,19 +2,21 @@ class RedisError(Exception): pass - + class AuthenticationError(RedisError): pass - + class ConnectionError(RedisError): pass - + class ResponseError(RedisError): pass - + class InvalidResponse(RedisError): pass - + class InvalidData(RedisError): pass -
\ No newline at end of file + +class WatchError(RedisError): + pass |