diff options
author | 赖信涛 <laixintao@users.noreply.github.com> | 2020-02-08 02:17:45 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-07 10:17:45 -0800 |
commit | 3d8d523bef4d77d712117ece76b2b08c47b432f5 (patch) | |
tree | 0948a44a7e5965e1122dc58c01ad7b5202493792 /redis/client.py | |
parent | 4b7f562f8066f44b1123d5e2600d42daa9855ff2 (diff) | |
download | redis-py-3d8d523bef4d77d712117ece76b2b08c47b432f5.tar.gz |
Make hset support multiple field/value pairs. (#1271)
* make `hset` command support multi field/value pairs.
see: https://redis.io/commands/hset
close https://github.com/andymccurdy/redis-py/issues/1269
deprecated: hmset
Co-authored-by: Alan Mai <0110amai@gmail.com>
Diffstat (limited to 'redis/client.py')
-rwxr-xr-x | redis/client.py | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/redis/client.py b/redis/client.py index dde701f..8ebcece 100755 --- a/redis/client.py +++ b/redis/client.py @@ -2996,12 +2996,23 @@ class Redis(object): "Return the number of elements in hash ``name``" return self.execute_command('HLEN', name) - def hset(self, name, key, value): + def hset(self, name, key=None, value=None, mapping=None): """ - Set ``key`` to ``value`` within hash ``name`` - Returns 1 if HSET created a new field, otherwise 0 + Set ``key`` to ``value`` within hash ``name``, + Use ``mappings`` keyword args to set multiple key/value pairs + for a hash ``name``. + Returns the number of fields that were added. """ - return self.execute_command('HSET', name, key, value) + if not key and not mapping: + raise DataError("'hset' with no key value pairs") + items = [] + if key: + items.extend((key, value)) + if mapping: + for pair in mapping.items(): + items.extend(pair) + + return self.execute_command('HSET', name, *items) def hsetnx(self, name, key, value): """ @@ -3015,6 +3026,7 @@ class Redis(object): Set key to value within hash ``name`` for each corresponding key and value from the ``mapping`` dict. """ + warnings.warn(DeprecationWarning('Use hset')) if not mapping: raise DataError("'hmset' with 'mapping' of length 0") items = [] |