Skip to content

Commit f88a573

Browse files
committed
Expose http_cache parameter, with its docs and recipe.
1 parent e0a8700 commit f88a573

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

msal/application.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ def __init__(
168168
# This way, it holds the same positional param place for PCA,
169169
# when we would eventually want to add this feature to PCA in future.
170170
exclude_scopes=None,
171+
http_cache=None,
171172
):
172173
"""Create an instance of application.
173174
@@ -334,6 +335,46 @@ def __init__(
334335
If that is unnecessary or undesirable for your app,
335336
now you can use this parameter to supply an exclusion list of scopes,
336337
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.
337378
"""
338379
self.client_id = client_id
339380
self.client_credential = client_credential
@@ -368,7 +409,7 @@ def __init__(
368409
self.http_client.mount("https://", a)
369410
self.http_client = ThrottledHttpClient(
370411
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
372413
)
373414

374415
self.app_name = app_name

0 commit comments

Comments
 (0)