From the course: Faster Python Code

Unlock the full course today

Join today to access over 22,700 courses taught by industry experts or purchase this course individually.

lru_cache

lru_cache - Python Tutorial

From the course: Faster Python Code

Start my 1-month free trial

lru_cache

- [Instructor] They have a slight API and as a good security practice demand we don't store keys in plain text but encrypt them. This means that every time someone makes an API call we need to encrypt the key and check if it matches the one we have in our key database. Here's the initial code. We use the crypt method from the crypt library to encrypt keys and we have a database in memory of users and function user from key in line 16 takes a key, encrypts it and get the user from the user database. Let's check the run time. So ipython and we run LRU and we use time it to get user from key of bugs. And it's 3.37 microseconds. This is an excellent candidate for caching. The problem is that if we have many users and maybe many fraud attempts our cache can get rid of it. Once the cache grows over the size of physical memory it will start swapping to this and performance will degrade. We'd like to…

Contents