--Investigate whether python can also speed up processing using cache. --As a result, it turned out that it can be easily used by using a library called cachetools. ――Therefore, this time, the outline of the library and the description example of the process are described. -** * Since this content deals with the outline, result image, and basic description, refer to Document for details. ** **
--Cachetools is a collection library that summarizes high-speed processing (memoization) using cache. --The features are as follows. --You can handle many cache algorithms with few descriptions. --It is expandable and can be changed to an appropriate mechanism according to the application and environment. --Data is stored in RAM.
――As shown in the two comparison images below, it is possible to easily speed up heavy processing with a small amount of description.


--Install the cachetools library with the following command.
pip install cachetools
** * Since this content deals with the outline, result image, and basic description, refer to Document for details. ** **
--When using standard cache processing (no options specified).
#Loading the library
from cachetools import cached
#Cache activation
@cached(cache ={})
def fibonacci(n):
  if n <= 2:
    return 1
  else:
    return fibonacci(n - 2) + fibonacci(n - 1)
print(fibonacci(30))
--The following types of cache processing can be used.
| name | Contents | 
|---|---|
| TTLCache | Specify the cache lifetime time. No access to anything that exceeds the lifetime. Discard the least used items first.  | 
| LFUCache | Minimum frequency of use. Measure item acquisition frequency and discard the least frequently used items | 
| LRUCache | Maximum unused frequency. Discard from the least used one | 
| RRCache | random. Randomly select items and discard from there | 
--The following is a description example
#Loading the library
from cachetools import cached, LFUCache, TTLCache, RRCache
@cached(cache=LFUCache(maxsize=10))  #Maximum number of holdings
def get_all_item:
  #processing
@cached(cache=TTLCache(maxsize=10, ttl=300))  #Maximum retention and lifetime
def get_user_item:
  #processing
@cached(cache=RRCache(maxsize=10, choice=min))  #Alternate function that returns the maximum number of holdings and any element
def get_random:
  #processing. cache.Function specified by choice(min)Can be called.
Recommended Posts