@@ -193,23 +193,34 @@ async def put(self, key: str, value: bytes, validate_keys: bool = True) -> int:
193193 """
194194 put will place the new value for the key into the store
195195 and return the revision number.
196+
197+ Note: This method does not support TTL. Use create() if you need TTL support.
198+
199+ :param key: The key to put
200+ :param value: The value to store
201+ :param validate_keys: Whether to validate the key format
196202 """
197203 if validate_keys and not _is_key_valid (key ):
198204 raise nats .js .errors .InvalidKeyError (key )
199205
200206 pa = await self ._js .publish (f"{ self ._pre } { key } " , value )
201207 return pa .seq
202208
203- async def create (self , key : str , value : bytes , validate_keys : bool = True ) -> int :
209+ async def create (self , key : str , value : bytes , validate_keys : bool = True , msg_ttl : Optional [ float ] = None ) -> int :
204210 """
205211 create will add the key/value pair iff it does not exist.
212+
213+ :param key: The key to create
214+ :param value: The value to store
215+ :param validate_keys: Whether to validate the key format
216+ :param msg_ttl: Optional TTL (time-to-live) in seconds for this specific message
206217 """
207218 if validate_keys and not _is_key_valid (key ):
208219 raise nats .js .errors .InvalidKeyError (key )
209220
210221 pa = None
211222 try :
212- pa = await self .update (key , value , last = 0 , validate_keys = validate_keys )
223+ pa = await self .update (key , value , last = 0 , validate_keys = validate_keys , msg_ttl = msg_ttl )
213224 except nats .js .errors .KeyWrongLastSequenceError as err :
214225 # In case of attempting to recreate an already deleted key,
215226 # the client would get a KeyWrongLastSequenceError. When this happens,
@@ -229,13 +240,25 @@ async def create(self, key: str, value: bytes, validate_keys: bool = True) -> in
229240 # to recreate using the last revision.
230241 raise err
231242 except nats .js .errors .KeyDeletedError as err :
232- pa = await self .update (key , value , last = err .entry .revision , validate_keys = validate_keys )
243+ pa = await self .update (
244+ key , value , last = err .entry .revision , validate_keys = validate_keys , msg_ttl = msg_ttl
245+ )
233246
234247 return pa
235248
236- async def update (self , key : str , value : bytes , last : Optional [int ] = None , validate_keys : bool = True ) -> int :
249+ async def update (
250+ self ,
251+ key : str ,
252+ value : bytes ,
253+ last : Optional [int ] = None ,
254+ validate_keys : bool = True ,
255+ msg_ttl : Optional [float ] = None ,
256+ ) -> int :
237257 """
238258 update will update the value if the latest revision matches.
259+
260+ Note: TTL parameter is accepted for internal use by create(), but should not be
261+ used directly on update operations per NATS KV semantics.
239262 """
240263 if validate_keys and not _is_key_valid (key ):
241264 raise nats .js .errors .InvalidKeyError (key )
@@ -247,7 +270,7 @@ async def update(self, key: str, value: bytes, last: Optional[int] = None, valid
247270
248271 pa = None
249272 try :
250- pa = await self ._js .publish (f"{ self ._pre } { key } " , value , headers = hdrs )
273+ pa = await self ._js .publish (f"{ self ._pre } { key } " , value , headers = hdrs , msg_ttl = msg_ttl )
251274 except nats .js .errors .APIError as err :
252275 # Check for a BadRequest::KeyWrongLastSequenceError error code.
253276 if err .err_code == 10071 :
@@ -256,9 +279,16 @@ async def update(self, key: str, value: bytes, last: Optional[int] = None, valid
256279 raise err
257280 return pa .seq
258281
259- async def delete (self , key : str , last : Optional [int ] = None , validate_keys : bool = True ) -> bool :
282+ async def delete (
283+ self , key : str , last : Optional [int ] = None , validate_keys : bool = True , msg_ttl : Optional [float ] = None
284+ ) -> bool :
260285 """
261286 delete will place a delete marker and remove all previous revisions.
287+
288+ :param key: The key to delete
289+ :param last: Expected last revision number (for optimistic concurrency)
290+ :param validate_keys: Whether to validate the key format
291+ :param msg_ttl: Optional TTL (time-to-live) in seconds for the delete marker
262292 """
263293 if validate_keys and not _is_key_valid (key ):
264294 raise nats .js .errors .InvalidKeyError (key )
@@ -269,17 +299,20 @@ async def delete(self, key: str, last: Optional[int] = None, validate_keys: bool
269299 if last and last > 0 :
270300 hdrs [api .Header .EXPECTED_LAST_SUBJECT_SEQUENCE ] = str (last )
271301
272- await self ._js .publish (f"{ self ._pre } { key } " , headers = hdrs )
302+ await self ._js .publish (f"{ self ._pre } { key } " , headers = hdrs , msg_ttl = msg_ttl )
273303 return True
274304
275- async def purge (self , key : str ) -> bool :
305+ async def purge (self , key : str , msg_ttl : Optional [ float ] = None ) -> bool :
276306 """
277307 purge will remove the key and all revisions.
308+
309+ :param key: The key to purge
310+ :param msg_ttl: Optional TTL (time-to-live) in seconds for the purge marker
278311 """
279312 hdrs = {}
280313 hdrs [KV_OP ] = KV_PURGE
281314 hdrs [api .Header .ROLLUP ] = MSG_ROLLUP_SUBJECT
282- await self ._js .publish (f"{ self ._pre } { key } " , headers = hdrs )
315+ await self ._js .publish (f"{ self ._pre } { key } " , headers = hdrs , msg_ttl = msg_ttl )
283316 return True
284317
285318 async def purge_deletes (self , olderthan : int = 30 * 60 ) -> bool :
0 commit comments