@@ -445,6 +445,7 @@ def _get_internal_client(
445445 bypass_encryption = opts ._bypass_auto_encryption ,
446446 encrypted_fields_map = encrypted_fields_map ,
447447 bypass_query_analysis = opts ._bypass_query_analysis ,
448+ key_expiration_ms = opts ._key_expiration_ms ,
448449 ),
449450 )
450451 self ._closed = False
@@ -547,11 +548,10 @@ class QueryType(str, enum.Enum):
547548
548549
549550def _create_mongocrypt_options (** kwargs : Any ) -> MongoCryptOptions :
550- opts = MongoCryptOptions (** kwargs )
551- # Opt into range V2 encryption.
552- if hasattr (opts , "enable_range_v2" ):
553- opts .enable_range_v2 = True
554- return opts
551+ # For compat with pymongocrypt <1.13, avoid setting the default key_expiration_ms.
552+ if kwargs .get ("key_expiration_ms" ) is None :
553+ kwargs .pop ("key_expiration_ms" , None )
554+ return MongoCryptOptions (** kwargs )
555555
556556
557557class AsyncClientEncryption (Generic [_DocumentType ]):
@@ -564,6 +564,7 @@ def __init__(
564564 key_vault_client : AsyncMongoClient [_DocumentTypeArg ],
565565 codec_options : CodecOptions [_DocumentTypeArg ],
566566 kms_tls_options : Optional [Mapping [str , Any ]] = None ,
567+ key_expiration_ms : Optional [int ] = None ,
567568 ) -> None :
568569 """Explicit client-side field level encryption.
569570
@@ -630,7 +631,12 @@ def __init__(
630631 Or to supply a client certificate::
631632
632633 kms_tls_options={'kmip': {'tlsCertificateKeyFile': 'client.pem'}}
634+ :param key_expiration_ms: The cache expiration time for data encryption keys.
635+ Defaults to ``None`` which defers to libmongocrypt's default which is currently 60000.
636+ Set to 0 to disable key expiration.
633637
638+ .. versionchanged:: 4.12
639+ Added the `key_expiration_ms` parameter.
634640 .. versionchanged:: 4.0
635641 Added the `kms_tls_options` parameter and the "kmip" KMS provider.
636642
@@ -666,14 +672,19 @@ def __init__(
666672 key_vault_coll = key_vault_client [db ][coll ]
667673
668674 opts = AutoEncryptionOpts (
669- kms_providers , key_vault_namespace , kms_tls_options = kms_tls_options
675+ kms_providers ,
676+ key_vault_namespace ,
677+ kms_tls_options = kms_tls_options ,
678+ key_expiration_ms = key_expiration_ms ,
670679 )
671680 self ._io_callbacks : Optional [_EncryptionIO ] = _EncryptionIO (
672681 None , key_vault_coll , None , opts
673682 )
674683 self ._encryption = AsyncExplicitEncrypter (
675684 self ._io_callbacks ,
676- _create_mongocrypt_options (kms_providers = kms_providers , schema_map = None ),
685+ _create_mongocrypt_options (
686+ kms_providers = kms_providers , schema_map = None , key_expiration_ms = key_expiration_ms
687+ ),
677688 )
678689 # Use the same key vault collection as the callback.
679690 assert self ._io_callbacks .key_vault_coll is not None
@@ -700,6 +711,7 @@ async def create_encrypted_collection(
700711 creation. :class:`~pymongo.errors.EncryptionError` will be
701712 raised if the collection already exists.
702713
714+ :param database: the database to create the collection
703715 :param name: the name of the collection to create
704716 :param encrypted_fields: Document that describes the encrypted fields for
705717 Queryable Encryption. The "keyId" may be set to ``None`` to auto-generate the data keys. For example:
0 commit comments