首先安裝模塊:
pip install cachetools
Cachetools提供了五個主要功能:
cached用作裝飾器。當我們調用緩存時,它會將函數緩存起來以備後用。默認情況下,這將執行一個簡單的緩存。
語法結構:
@cached(cache = {})
def some_fun():
pass
示例代碼: 【使用時間模塊來查看模塊的效率】
from cachetools import cached
import time
# without cached
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
s = time.time()
print(fib(36))
print("Time Taken:", time.time() - s)
# Now using cached
s = time.time()
# Use this decorator to enable caching
@cached(cache={})
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
print(fib(36))
print("Time Taken(cached): ", time.time() - s)
運行結果:
LRUCache在緩存裝飾器內部使用。LRU 緩存是指“最近最少使用”的緩存。它接受一個參數“maxsize”,該參數說明應如何緩存最近的函數。
語法結構:
@cached(cache= LRUCache(maxsize= 3))
def some_fun():
pass
示例代碼:
from cachetools import cached, LRUCache
import time
# cache using LRUCache
@cached(cache=LRUCache(maxsize=3))
def my_fun(n):
# This delay resembles some task
s = time.time()
time.sleep(n)
print("\nTime Taken: ", time.time() - s)
return f"I am executed: {n}"
# Takes 3 seconds
print(my_fun(3))
# Takes no time
print(my_fun(3))
# Takes 2 seconds
print(my_fun(2))
# Takes 1 second
print(my_fun(1))
# Takes 4 seconds
print(my_fun(4))
# Takes no time
print(my_fun(1))
# Takes 3 seconds because maxsize = 3
# and the 3 recent used functions had 1,
# 2 and 4.
print(my_fun(3))
運行結果:
注意: LRUCache也可以從標准 Python 包 functools 中調用
from functools import lru_cache
@lru_cache
def myfunc():
pass
TTLCache或“Time To Live”緩存是 cachetools 模塊中包含的第三個功能。它有兩個參數——“maxsize”和“TTL”。“maxsize”的使用與 LRUCache 相同,但這裡的“TTL”值表示緩存應存儲多長時間。該值以秒為單位。
語法結構:
@cached(cache= TTLCache(maxsize= 33, ttl = 600))
def some_fun():
pass
示例代碼:
from cachetools import cached, TTLCache
import time
# Here recent 32 functions
# will we stored for 1 minutes
@cached(cache=TTLCache(maxsize=32, ttl=25))
def my_fun(n):
# This delay resembles some task
s = time.time()
time.sleep(n)
print("\nTime Taken: ", time.time() - s)
return f"I am executed: {n}"
print(my_fun(3))
print(my_fun(3))
print("*" * 100)
time.sleep(24)
print(my_fun(3))
print("*" * 100)
time.sleep(26)
print(my_fun(3))
運行結果:
LFUCache或“Least Frequently Used”緩存是另一種類型的緩存技術,用於檢索項目被調用的頻率。它會在必要時丟棄最不常調用的項目以騰出空間。它采用一個參數——“maxsize”,與 LRUCache 中的相同。
語法結構:
@cached(cache= LFUCache(maxsize= 33))
def some_fun():
pass
示例代碼:
from cachetools import cached, LFUCache
import time
# Here if a particular item is not called
# within 5 successive call of the function,
# it will be discarded
@cached(cache=LFUCache(maxsize=5))
def my_fun(n):
# This delay resembles some task
s = time.time()
time.sleep(n)
print("\nTime Taken: ", time.time() - s)
return f"I am executed: {n}"
print(my_fun(3))
print(my_fun(3))
print(my_fun(2))
print(my_fun(4))
print(my_fun(1))
print(my_fun(1))
print(my_fun(3))
print(my_fun(3))
print(my_fun(4))
運行結果:
RRCache或“Random Replacement”緩存是另一種緩存技術,它隨機選擇緩存中的項目並在必要時丟棄它們以釋放空間。它采用一個參數——“maxsize”,與 LRUCache 中的相同。它還有一個參數選擇,默認設置為“random.choice”。
語法結構:
@cached(cache= RRCache(maxsize= 33))
def some_fun():
pass
示例代碼:
from cachetools import cached, RRCache
import time
# Here if a particular item is not called
# within 5 successive call of the function,
# it will be discarded
@cached(cache=RRCache(maxsize=5))
def my_fun(n):
# This delay resembles some task
s = time.time()
time.sleep(n)
print("\nTime Taken: ", time.time() - s)
return f"I am executed: {n}"
print(my_fun(3))
print(my_fun(3))
print(my_fun(2))
print(my_fun(4))
print(my_fun(1))
print(my_fun(1))
print(my_fun(3))
print(my_fun(2))
print(my_fun(3))
運行結果: