@@ -139,6 +139,7 @@ def __init__(
139
139
# This way, it holds the same positional param place for PCA,
140
140
# when we would eventually want to add this feature to PCA in future.
141
141
exclude_scopes = None ,
142
+ http_cache = None ,
142
143
):
143
144
"""Create an instance of application.
144
145
@@ -305,6 +306,46 @@ def __init__(
305
306
If that is unnecessary or undesirable for your app,
306
307
now you can use this parameter to supply an exclusion list of scopes,
307
308
such as ``exclude_scopes = ["offline_access"]``.
309
+
310
+ :param dict http_cache:
311
+ MSAL has long been caching tokens in the ``token_cache``.
312
+ Recently, MSAL also introduced a concept of ``http_cache``,
313
+ by automatically caching some finite amount of non-token http responses,
314
+ so that *long-lived*
315
+ ``PublicClientApplication`` and ``ConfidentialClientApplication``
316
+ would be more performant and responsive in some situations.
317
+
318
+ This ``http_cache`` parameter accepts any dict-like object.
319
+ If not provided, MSAL will use an in-memory dict.
320
+
321
+ If your app is a command-line app (CLI),
322
+ you would want to persist your http_cache across different CLI runs.
323
+ The Python standard library's ``shelve`` module comes in handy. Recipe::
324
+
325
+ # Just add the following 3 lines at the beginning of your CLI script
326
+ import sys, atexit, shelve
327
+ persisted_http_cache = shelve.open(sys.argv[0] + ".http_cache")
328
+ atexit.register(persisted_http_cache.close)
329
+
330
+ # And then you can implement your app as you normally would
331
+ app = msal.PublicClientApplication(
332
+ "your_client_id",
333
+ ...,
334
+ http_cache=persisted_http_cache, # Utilize persisted_http_cache
335
+ ...,
336
+ #token_cache=..., # You may combine the old token_cache trick
337
+ # Please refer to token_cache recipe at
338
+ # https://msal-python.readthedocs.io/en/latest/#msal.SerializableTokenCache
339
+ )
340
+ app.acquire_token_interactive(["your", "scope"], ...)
341
+
342
+ Content inside ``http_cache`` are cheap to obtain.
343
+ There is no need to share them among different apps.
344
+
345
+ Content inside ``http_cache`` will contain no tokens nor
346
+ Personally Identifiable Information (PII). Encryption is unnecessary.
347
+
348
+ New in version 1.15.0.
308
349
"""
309
350
self .client_id = client_id
310
351
self .client_credential = client_credential
@@ -339,7 +380,7 @@ def __init__(
339
380
self .http_client .mount ("https://" , a )
340
381
self .http_client = ThrottledHttpClient (
341
382
self .http_client ,
342
- {} # Hard code an in-memory cache, for now
383
+ {} if http_cache is None else http_cache , # Default to an in-memory dict
343
384
)
344
385
345
386
self .app_name = app_name
0 commit comments