Cachetools 是一個 Python 模塊,提供各種 memory 集合和裝飾器。它還包括 functools 的 @lru_cache 裝飾器的變體。要使用它,首先我們需要使用 pip 安裝它。
pip install cachetools
Cachetools為我們提供了五個主要函數。
- cached
- LRUCache
- TTLCache
- LFUCache
- RRCache
讓我們通過示例詳細了解以下每個函數。
緩存
緩存用作裝飾器。當我們調用cache時,它會緩存該函數以供以後使用。默認情況下,這將執行簡單的緩存。
用法:
@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(old_fib(35))
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(35))
print("Time Taken(cached): ", time.time() - s)
輸出:
9227465 Time Taken: 4.553245782852173 9227465 Time Taken(cached): 0.0003821849822998047
LRU緩存
LRUCache 在緩存裝飾器內部使用。 LRU緩存意味著“Least Recently Used”緩存。它需要一個參數“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 myfun(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(myfun(3))
# Takes no time
print(myfun(3))
# Takes 2 seconds
print(myfun(2))
# Takes 1 second
print(myfun(1))
# Takes 4 seconds
print(myfun(4))
# Takes no time
print(myfun(1))
# Takes 3 seconds because maxsize = 3
# and the 3 recent used functions had 1,
# 2 and 4.
print(myfun(3))
輸出:
Time Taken: 3.0030977725982666 I am executed: 3 I am executed: 3 Time Taken: 2.002072334289551 I am executed: 2 Time Taken: 1.001115083694458 I am executed: 1 Time Taken: 4.001702070236206 I am executed: 4 I am executed: 1 Time Taken: 3.0030171871185303 I am executed: 3
注意: LRU緩存也可以從標準 Python 包 - functools 調用。它可以看到導入為
from functools import lru_cache @lru_cache def myfunc(): pass
TTL緩存
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 = 60))
def myfun(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(myfun(3))
print(myfun(3))
time.sleep(61)
print(myfun(3))
輸出:
Time Taken: 3.0031025409698486 I am executed: 3 I am executed: 3 Time Taken: 3.0029332637786865 I am executed: 3
LFU緩存
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 myfun(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(myfun(3))
print(myfun(3))
print(myfun(2))
print(myfun(4))
print(myfun(1))
print(myfun(1))
print(myfun(3))
print(myfun(3))
print(myfun(4))
輸出:
Time Taken: 3.002413272857666 I am executed: 3 I am executed: 3 Time Taken: 2.002107620239258 I am executed: 2 Time Taken: 4.003819465637207 I am executed: 4 Time Taken: 1.0010886192321777 I am executed: 1 I am executed: 1 I am executed: 3 I am executed: 3 I am executed: 4
RR緩存
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 myfun(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(myfun(3))
print(myfun(3))
print(myfun(2))
print(myfun(4))
print(myfun(1))
print(myfun(1))
print(myfun(3))
print(myfun(2))
print(myfun(3))
輸出:
Time Taken: 3.003124713897705 I am executed: 3 I am executed: 3 Time Taken: 2.0021231174468994 I am executed: 2 Time Taken: 4.004120588302612 I am executed: 4 Time Taken: 1.0011250972747803 I am executed: 1 I am executed: 1 I am executed: 3 I am executed: 2 I am executed: 3
相關用法
- Python Calendar itermonthdates()用法及代碼示例
- Python Calendar itermonthdays()用法及代碼示例
- Python Calendar itermonthdays2()用法及代碼示例
- Python Calendar itermonthdays3()用法及代碼示例
- Python Calendar itermonthdays4()用法及代碼示例
- Python Calendar iterweekdays()用法及代碼示例
- Python Calendar monthdatescalendar()用法及代碼示例
- Python Calendar monthdays2calendar()用法及代碼示例
- Python Calendar monthdayscalendar()用法及代碼示例
- Python Calendar yeardatescalendar()用法及代碼示例
- Python Calendar yeardays2calendar()用法及代碼示例
- Python Calendar yeardayscalendar()用法及代碼示例
- Python Condition acquire()用法及代碼示例
- Python Condition notify()用法及代碼示例
- Python Condition notify_all()用法及代碼示例
- Python Condition release()用法及代碼示例
- Python Condition wait()用法及代碼示例
- Python Collections.UserList用法及代碼示例
- Python Collections.UserDict用法及代碼示例
- Python Collections.UserString用法及代碼示例
- Python Celsius轉Fahrenheit用法及代碼示例
- Python CSV轉JSON用法及代碼示例
- Python CSV File轉PDF File用法及代碼示例
- Python Complex Number轉String用法及代碼示例
- Python Coordinate Dictionary轉Matrix用法及代碼示例
注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Cachetools module in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。