[51CTO Quick Translation] In today's article, we will start with a simple example to learn how to use the cache mechanism. After that, we will further use the functools module of the Python standard library to create a cache that suits our needs. Without further ado, let's get started. Caching is a way of storing quantitative data in preparation for subsequent requests, in order to speed up data retrieval. In today's article, we will start with a simple example to learn how to use the cache mechanism. After that, we will further use the functools module of the Python standard library to create a cache that suits our needs. As a starting point, we first create a class to build our cache dictionary, and then expand it as needed. The following is the specific code:
There is nothing special in the above class example. We just create a simple class and set two class variables or attributes, cache and max_cache_size. Cache is an empty dictionary, and max_cache_size obviously represents the maximum cache capacity. Let's further flesh out the code to make it functional:
Here we import the datetime and random modules, and we can see the class we created earlier. This time, we add a few methods to it. One of the methods does the magic, called _contains_. Although it is not necessary to use this method here, the basic idea is that it allows us to check the class instance to see if it contains the key we are looking for. In addition, the update method is responsible for updating the cache dictionary with the new key/value pair. Once the maximum capacity of the cache is reached or exceeded, it will also delete the oldest input data. In addition, the remove_oldest method is responsible for the specific removal of early data in the dictionary. Finally, we also introduced an attribute called size, which can return the specific capacity of the cache. After adding the following code, we can test that the cache works as expected:
In this example, we set up a number of predefined keys and loops. If the key does not exist yet, we add it to the cache. However, the sample code above does not mention how to update the access date, so you can explore it as an exercise. After running this code, you will notice that when the cache is full, it will correctly delete the older entries. Now, let’s move on and see how to leverage another way to create a cache using Python’s built-in functools module. Using functools.lru_cache Python's functools module provides a very useful decorator, lru_cache. It should be noted that it was only added in version 3.2. According to the documentation, this decorator can "pack functions into callable memory to reduce the size of the most recently called functions." Next, we will write a basic function based on the example mentioned in the documentation, which contains multiple network pages. In this case, we can get the pages directly from the Python documentation site.
In the code above, we decorate the get_webpage function with lru_cache and set its maximum size to 24 calls. After that, we set a webpage string variable and pass it the module we want the function to get. In my experience, it works better if you run this in a Python interpreter, such as IDLE. This way, we can run multiple loops through the function. You can see that when you run the code first, the output is relatively slow. But if you run it again in the same session, it will be much faster, which means that lru_cache has cached the call correctly. You can experiment with your own interpreter instance and see the results for yourself. Additionally, we can pass a typed parameter to the decorator. This is a Boolean that tells the decorator to cache different types of parameters separately when typed is set to True. Summarize Now you have a basic understanding of how to write your own cache mechanism in Python. This is a fun tool and can be very useful if you have a lot of high-intensity I/O calls or want to cache commonly used information such as login credentials. Original title: Python Development: Introduction to Cache Mechanism [Translated by 51CTO. Please indicate the original translator and source as 51CTO.com when reprinting on partner sites] |
>>: Carelessness leads to failure: Lee Sedol loses in first round of man vs. machine match
Image caption: Comet Purple Mountain-Atlas in the...
Are you used to using the same toothpaste all the...
In 2020, there are many content platforms besides...
From 24:00 on March 31, the domestic gasoline and...
Recently, the Beijing Cancer Prevention and Treat...
One of the most requested features of iOS is a cu...
As live streaming and short video promotion are i...
With the rapid development of mobile Internet, th...
Nowadays, no matter what industry you are in, if ...
[[124030]] Android Studio 1.0 is finally released...
The New Yorker recently reported that Chat GPT...
Nowadays, countdowns are widely used, including b...
[[251953]] Android Studio's official Android ...
Last Tuesday, Apple released the official iOS 16....
Recently, CleanTechnica, a foreign new energy veh...