diff options
| author | Craig Hawco <craig.hawco@gmail.com> | 2010-09-03 10:27:52 -0400 |
|---|---|---|
| committer | Craig Hawco <craig.hawco@gmail.com> | 2010-09-03 10:27:52 -0400 |
| commit | 77422cac6c832f3726557ca397884fc3d415ec54 (patch) | |
| tree | 1c1a0ac7faf2359d3464993bf699d0aa7b881c67 | |
| parent | 16c0eff87e7762d82fdd37d9949e33a7376d5357 (diff) | |
| download | redis-py-77422cac6c832f3726557ca397884fc3d415ec54.tar.gz | |
Add WATCH command to redis-py
| -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 |
