@@ -168,6 +168,7 @@ def __init__(
168
168
# This way, it holds the same positional param place for PCA,
169
169
# when we would eventually want to add this feature to PCA in future.
170
170
exclude_scopes = None ,
171
+ http_cache = None ,
171
172
):
172
173
"""Create an instance of application.
173
174
@@ -334,6 +335,46 @@ def __init__(
334
335
If that is unnecessary or undesirable for your app,
335
336
now you can use this parameter to supply an exclusion list of scopes,
336
337
such as ``exclude_scopes = ["offline_access"]``.
338
+
339
+ :param dict http_cache:
340
+ MSAL has long been caching tokens in the ``token_cache``.
341
+ Recently, MSAL also introduced a concept of ``http_cache``,
342
+ by automatically caching some finite amount of non-token http responses,
343
+ so that *long-lived*
344
+ ``PublicClientApplication`` and ``ConfidentialClientApplication``
345
+ would be more performant and responsive in some situations.
346
+
347
+ This ``http_cache`` parameter accepts any dict-like object.
348
+ If not provided, MSAL will use an in-memory dict.
349
+
350
+ If your app is a command-line app (CLI),
351
+ you would want to persist your http_cache across different CLI runs.
352
+ The Python standard library's ``shelve`` module comes in handy. Recipe::
353
+
354
+ # Just add the following 3 lines at the beginning of your CLI script
355
+ import sys, atexit, shelve
356
+ persisted_http_cache = shelve.open(sys.argv[0] + ".http_cache")
357
+ atexit.register(persisted_http_cache.close)
358
+
359
+ # And then you can implement your app as you normally would
360
+ app = msal.PublicClientApplication(
361
+ "your_client_id",
362
+ ...,
363
+ http_cache=persisted_http_cache, # Utilize persisted_http_cache
364
+ ...,
365
+ #token_cache=..., # You may combine the old token_cache trick
366
+ # Please refer to token_cache recipe at
367
+ # https://msal-python.readthedocs.io/en/latest/#msal.SerializableTokenCache
368
+ )
369
+ app.acquire_token_interactive(["your", "scope"], ...)
370
+
371
+ Content inside ``http_cache`` are cheap to obtain.
372
+ There is no need to share them among different apps.
373
+
374
+ Content inside ``http_cache`` will contain no tokens nor
375
+ Personally Identifiable Information (PII). Encryption is unnecessary.
376
+
377
+ New in version 1.15.0.
337
378
"""
338
379
self .client_id = client_id
339
380
self .client_credential = client_credential
@@ -368,7 +409,7 @@ def __init__(
368
409
self .http_client .mount ("https://" , a )
369
410
self .http_client = ThrottledHttpClient (
370
411
self .http_client ,
371
- {} # Hard code an in-memory cache, for now
412
+ {} if http_cache is None else http_cache , # Default to an in-memory dict
372
413
)
373
414
374
415
self .app_name = app_name
0 commit comments