Cache API keys in Python

Let’s cache them all!

Mário Victor Ribeiro Silva
2 min readSep 6, 2021
Photo by FLY:D on Unsplash

Introduction

There are many ways to cache functions and variables in Python. Usually, you want to cache CPU and IO bound functions depending on your needs, but in this tutorial we want to fetch an API key from an external service, like Auth0 or GitHub, and store this to use in the entire lifespan of the token.

There are, mainly, 3 ways we can achieve this:

  • Cache the entire function that get the API key (using lru_cache decorator);
  • Cache the variable that store the key (using config.py for example);
  • Use a library to cache the variable or function (It depends on the library);

The approach we are taking is cache the entire function that request the API key.

If you didn’t have the API Key, request in a different function. You really should do this!

Making a timed LRU cache

We’re not going deep in lru_cache in this article. If you want to know more about how the things work under the hood, I suggest two links: this link to the official docs, and this link for a nice tutorial from Real Python.

To use lru_cache, you can simply add the decorator in the function, but the cache will never expire, and it is bad for our problem because API keys do expire, they have a lifespan. We need to expire this cache in a certain time. So, we’ll have to override the decorator with our decorator.

Here is a full code example implementing and showing the decorator in action:

And that’s it! It is really simple to implement and reuse.

Important observation

This method only work for functions outside classes, but there is a way to make it work(like this link in Stack Overflow shows). It adds a lot of overhead, so I prefer to just separate the function from the class.

Thank you for reading. I hope this tutorial was useful to you!.

(And thanks to Mylena Rossato for the English review!)

--

--

Mário Victor Ribeiro Silva

Computer engineer | Always inspired to learn something new.