Skip to content

Commit 46814f2

Browse files
committed
Expose http_cache parameter, with its docs and recipe.
1 parent a1f9ca7 commit 46814f2

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
@@ -139,6 +139,7 @@ def __init__(
139139
# This way, it holds the same positional param place for PCA,
140140
# when we would eventually want to add this feature to PCA in future.
141141
exclude_scopes=None,
142+
http_cache=None,
142143
):
143144
"""Create an instance of application.
144145
@@ -305,6 +306,46 @@ def __init__(
305306
If that is unnecessary or undesirable for your app,
306307
now you can use this parameter to supply an exclusion list of scopes,
307308
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.
308349
"""
309350
self.client_id = client_id
310351
self.client_credential = client_credential
@@ -339,7 +380,7 @@ def __init__(
339380
self.http_client.mount("https://", a)
340381
self.http_client = ThrottledHttpClient(
341382
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
343384
)
344385

345386
self.app_name = app_name

0 commit comments

Comments
 (0)