@@ -5055,6 +5055,82 @@ def hsetnx(self, name: str, key: str, value: str) -> Union[Awaitable[bool], bool
50555055 """
50565056 return self .execute_command ("HSETNX" , name , key , value )
50575057
5058+ def hsetex (
5059+ self ,
5060+ name : str ,
5061+ key : Optional [str ] = None ,
5062+ value : Optional [str ] = None ,
5063+ mapping : Optional [dict ] = None ,
5064+ items : Optional [list ] = None ,
5065+ ex : Union [ExpiryT , None ] = None ,
5066+ px : Union [ExpiryT , None ] = None ,
5067+ exat : Union [AbsExpiryT , None ] = None ,
5068+ pxat : Union [AbsExpiryT , None ] = None ,
5069+ keepttl : bool = False ,
5070+ nx : bool = False ,
5071+ xx : bool = False ,
5072+ fnx : bool = False ,
5073+ fxx : bool = False ,
5074+ ) -> Union [Awaitable [bool ], bool ]:
5075+ """
5076+ Set key to value within hash ``name``,
5077+ ``mapping`` accepts a dict of key/value pairs to be added to hash ``name``.
5078+ ``items`` accepts a list of key/value pairs to be added to hash ``name``.
5079+ Set expiration options for the hash fields.
5080+
5081+ For more information see https://valkey.io/commands/hsetex
5082+ """
5083+
5084+ if key is None and not mapping and not items :
5085+ raise DataError ("'hsetex' with no key value pairs" )
5086+ if int (key is not None ) + int (mapping is not None ) + int (items is not None ) > 1 :
5087+ raise DataError (
5088+ "Only one of 'key/value', 'mapping', or 'items' can be specified."
5089+ )
5090+
5091+ if int (keepttl ) + sum (arg is not None for arg in [ex , px , exat , pxat ]) > 1 :
5092+ raise DataError (
5093+ "Only one of 'ex', 'px', 'exat', 'pxat', or 'keepttl' can be specified."
5094+ )
5095+ if nx and xx :
5096+ raise DataError ("Only one of 'nx' or 'xx' can be specified." )
5097+ if fnx and fxx :
5098+ raise DataError ("Only one of 'fnx' or 'fxx' can be specified." )
5099+ pieces = []
5100+ if ex is not None :
5101+ pieces .extend (["EX" , ex ])
5102+ if px is not None :
5103+ pieces .extend (["PX" , px ])
5104+ if exat is not None :
5105+ pieces .extend (["EXAT" , exat ])
5106+ if pxat is not None :
5107+ pieces .extend (["PXAT" , pxat ])
5108+ if nx :
5109+ pieces .append ("NX" )
5110+ if xx :
5111+ pieces .append ("XX" )
5112+ if fnx :
5113+ pieces .append ("FNX" )
5114+ if fxx :
5115+ pieces .append ("FXX" )
5116+ pieces .append ("FIELDS" )
5117+ if key is not None and value is not None :
5118+ pieces .append (1 ) # for one field
5119+ pieces .append (key )
5120+ pieces .append (value )
5121+ if mapping :
5122+ pieces .append (len (mapping ))
5123+ for key , value in mapping .items ():
5124+ if key is None or value is None :
5125+ raise DataError ("'hsetex' mapping contains None key or value" )
5126+ pieces .append (key )
5127+ pieces .append (value )
5128+ if items :
5129+ pieces .append (len (items ) // 2 )
5130+ pieces .extend (items )
5131+
5132+ return self .execute_command ("HSETEX" , name , * pieces )
5133+
50585134 def hmset (self , name : str , mapping : dict ) -> Union [Awaitable [str ], str ]:
50595135 """
50605136 Set key to value within hash ``name`` for each corresponding
0 commit comments