From c2ffb83c7152b8d13f8d33a239f5b299f3554cc5 Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Mon, 25 Mar 2024 12:36:32 -0700 Subject: [PATCH] fix(handle_jwt.py): cache public keys caches jwt public keys - reducing need for making http calls on every request --- litellm/proxy/auth/handle_jwt.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/litellm/proxy/auth/handle_jwt.py b/litellm/proxy/auth/handle_jwt.py index 898fca8ce..268208bae 100644 --- a/litellm/proxy/auth/handle_jwt.py +++ b/litellm/proxy/auth/handle_jwt.py @@ -155,9 +155,19 @@ class JWTHandler: if keys_url is None: raise Exception("Missing JWT Public Key URL from environment.") - response = await self.http_handler.get(keys_url) + cached_keys = await self.user_api_key_cache.async_get_cache( + "litellm_jwt_auth_keys" + ) + if cached_keys is None: + response = await self.http_handler.get(keys_url) - keys = response.json()["keys"] + keys = response.json()["keys"] + + await self.user_api_key_cache.async_set_cache( + key="litellm_jwt_auth_keys", value=keys, ttl=600 # cache for 10 mins + ) + else: + keys = cached_keys header = jwt.get_unverified_header(token)