Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions redis/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -2358,6 +2358,7 @@ def inner(*args, **kwargs):
"MGET NONATOMIC",
"MOVE",
"MSET",
"MSETEX",
"MSET NONATOMIC",
"MSETNX",
"PFCOUNT",
Expand Down
88 changes: 88 additions & 0 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,16 @@ def execute(self) -> ResponseT:
return self.client.execute_command(*command)


class DataPersistOptions(Enum):
# set the value for each provided key to each
# provided value only if all do not already exist.
NX = "NX"

# set the value for each provided key to each
# provided value only if all already exist.
XX = "XX"


class BasicKeyCommands(CommandsProtocol):
"""
Redis basic key-based commands
Expand Down Expand Up @@ -1987,6 +1997,10 @@ def mget(self, keys: KeysT, *args: EncodableT) -> ResponseT:
"""
Returns a list of values ordered identically to ``keys``

** Important ** When this method is used with Cluster clients, all keys
must be in the same hash slot, otherwise a RedisClusterException
will be raised.

For more information, see https://redis.io/commands/mget
"""
from redis.client import EMPTY_RESPONSE
Expand All @@ -2004,20 +2018,94 @@ def mset(self, mapping: Mapping[AnyKeyT, EncodableT]) -> ResponseT:
key/value pairs. Both keys and values should be strings or types that
can be cast to a string via str().

** Important ** When this method is used with Cluster clients, all keys
must be in the same hash slot, otherwise a RedisClusterException
will be raised.

For more information, see https://redis.io/commands/mset
"""
items = []
for pair in mapping.items():
items.extend(pair)
return self.execute_command("MSET", *items)

def msetex(
self,
mapping: Mapping[AnyKeyT, EncodableT],
data_persist_option: Optional[DataPersistOptions] = None,
ex: Optional[ExpiryT] = None,
px: Optional[ExpiryT] = None,
exat: Optional[AbsExpiryT] = None,
pxat: Optional[AbsExpiryT] = None,
keepttl: bool = False,
) -> Union[Awaitable[int], int]:
"""
Sets key/values based on the provided ``mapping`` items.

** Important ** When this method is used with Cluster clients, all keys
must be in the same hash slot, otherwise a RedisClusterException
will be raised.

``mapping`` accepts a dict of key/value pairs that will be added to the database.

``data_persist_option`` can be set to ``NX`` or ``XX`` to control the
behavior of the command.
``NX`` will set the value for each provided key to each
provided value only if all do not already exist.
``XX`` will set the value for each provided key to each
provided value only if all already exist.

``ex`` sets an expire flag on the keys in ``mapping`` for ``ex`` seconds.

``px`` sets an expire flag on the keys in ``mapping`` for ``px`` milliseconds.

``exat`` sets an expire flag on the keys in ``mapping`` for ``ex`` seconds,
specified in unix time.

``pxat`` sets an expire flag on the keys in ``mapping`` for ``ex`` milliseconds,
specified in unix time.

``keepttl`` if True, retain the time to live associated with the keys.

Returns the number of fields that were added.

Available since Redis 8.4
For more information, see https://redis.io/commands/msetex
"""
opset = {ex, px, exat, pxat}
if len(opset) > 2 or len(opset) > 1 and keepttl:
raise DataError(
"``ex``, ``px``, ``exat``, ``pxat``, "
"and ``keepttl`` are mutually exclusive."
)

exp_options: list[EncodableT] = []
if data_persist_option:
exp_options.append(data_persist_option.value)

exp_options.extend(extract_expire_flags(ex, px, exat, pxat))

if keepttl:
exp_options.append("KEEPTTL")

pieces = ["MSETEX", len(mapping)]

for pair in mapping.items():
pieces.extend(pair)

return self.execute_command(*pieces, *exp_options)

def msetnx(self, mapping: Mapping[AnyKeyT, EncodableT]) -> ResponseT:
"""
Sets key/values based on a mapping if none of the keys are already set.
Mapping is a dictionary of key/value pairs. Both keys and values
should be strings or types that can be cast to a string via str().
Returns a boolean indicating if the operation was successful.

** Important ** When this method is used with Cluster clients, all keys
must be in the same hash slot, otherwise a RedisClusterException
will be raised.

For more information, see https://redis.io/commands/msetnx
"""
items = []
Expand Down
Loading
Loading