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 | |
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>
-rw-r--r-- | CHANGES | 2 | ||||
-rwxr-xr-x | redis/client.py | 20 | ||||
-rw-r--r-- | tests/test_commands.py | 17 |
3 files changed, 34 insertions, 5 deletions
@@ -14,6 +14,8 @@ in 3.4.0. This ended up being a bad idea as two separate connection pools be considered equal yet manage a completely separate set of connections. + * HSET command now can accept multiple pairs. HMSET has been marked as + deprecated now. Thanks to @laixintao #1271 * 3.4.0 * Allow empty pipelines to be executed if there are WATCHed keys. This is a convenient way to test if any of the watched keys changed 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 = [] diff --git a/tests/test_commands.py b/tests/test_commands.py index b2bd2da..15a76de 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -1587,7 +1587,7 @@ class TestRedisCommands(object): # HASH COMMANDS def test_hget_and_hset(self, r): - r.hmset('a', {'1': 1, '2': 2, '3': 3}) + r.hmset('a', mapping={'1': 1, '2': 2, '3': 3}) assert r.hget('a', '1') == b'1' assert r.hget('a', '2') == b'2' assert r.hget('a', '3') == b'3' @@ -1603,6 +1603,21 @@ class TestRedisCommands(object): # key inside of hash that doesn't exist returns null value assert r.hget('a', 'b') is None + def test_hset_with_multi_key_values(self, r): + r.hset('a', mapping={'1': 1, '2': 2, '3': 3}) + assert r.hget('a', '1') == b'1' + assert r.hget('a', '2') == b'2' + assert r.hget('a', '3') == b'3' + + r.hset('b', "foo", "bar", mapping={'1': 1, '2': 2}) + assert r.hget('b', '1') == b'1' + assert r.hget('b', '2') == b'2' + assert r.hget('b', 'foo') == b'bar' + + def test_hset_without_data(self, r): + with pytest.raises(exceptions.DataError): + r.hset("x") + def test_hdel(self, r): r.hmset('a', {'1': 1, '2': 2, '3': 3}) assert r.hdel('a', '2') == 1 |