fastapi-cache icon indicating copy to clipboard operation
fastapi-cache copied to clipboard

Caching an Auth token validation until expiry

Open tempcollab opened this issue 2 years ago • 0 comments

I receive an auth token from the frontend with an expiry date in the future. I would like to cache it until expiry date, but the problem is the expiry date cannot be known until the token has been validated once. Here is the example of the function. It currently caches the response for 60 seconds, but I would like it to be timedelta(token_expiry_timestamp - time.now()).

@cache(namespace="auth", expire=60)
def validate_token(auth_token):
    # first I validate the token
    # then I decode it to obtain user_email and token_expiry_timestamp
    #
    
   def main():
   # some code
   validate_token(auth_token)

I was thinking of returning token_expiry_timestamp from validate_token. Then, in main() I'd clear the cache for this specific case if token_expiry_timestamp has already passed. Is there a better way to do this?

tempcollab avatar Apr 19 '22 23:04 tempcollab