程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python中的cachetools用法詳解(Cached、LRUCache、TTLCache、LFUCache、RRCache)

編輯:Python

首先安裝模塊:

pip install cachetools

Cachetools提供了五個主要功能:

  • Cached
  • LRUCache
  • TTLCache
  • LFUCache
  • RRCache

Cached

        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

        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

        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

        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

        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))

運行結果:


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved