我們大多數人都會聽到“緩存”這個詞,但不是所有聽到這個詞的人都知道它。就計算機技術而言,緩存是存儲數據(用戶在計算機上執行的活動)的軟件或硬件組件。當將來對相同的數據提出請求時,這非常有幫助,因為這樣,相同的數據將得到更快的服務。存儲在系統的緩存組件中的數據可能是因為之前執行的計算,或者相同數據的副本存儲在其他地方。這就是我們如何看到高速緩存對於更快的計算速度和更短的時間內獲得更好的結果是多麼重要。與數據的實際大小相比,緩存佔用的空間很小,因此對用戶來說更有用。
Python 為我們提供了各種模塊,通過這些模塊,我們可以處理系統中存在的緩存內存。Python 還為我們提供了模塊,在這些模塊中,我們可以使用高速緩存來加快計算速度並節省時間。這樣的 Python 模塊之一是 Python 中的 Cachetools 模塊,我們將在本教程中學習這個模塊。我們將簡要詳細地了解 CacheTools 模塊&它在 Python 中的功能,以及這些功能如何在系統中存在的緩存的幫助下幫助執行各種任務。
Python 中的 CacheTools 模塊
CacheTools 模塊:簡介
Cachetools 是一個 Python 模塊,它為我們提供了各種裝飾器(這是這個模塊中可用的功能)和記憶集合。緩存工具幫助我們有效地處理系統中存在的緩存,我們可以將該緩存與 CacheTools 模塊一起使用,以更快地處理 Python 程序。Cachetools 模塊還包括 Functools 模塊(另一個 Python 模塊)的@lru_cache 裝飾函數的一些變體。
CacheTools 模塊:安裝
Cachetools Module 不是 Python 的內置模塊,因此如果我們想在我們的系統中使用這個模塊及其功能,我們必須先安裝這個模塊。在本節中,我們將通過帶有畫中畫安裝程序的命令提示符終端安裝 CacheTools 模塊。首先,我們必須打開命令提示符終端,然後我們必須在終端內部編寫以下 pip 命令:
pip install cachetools
當我們按下回車鍵時,畫中畫安裝程序將開始通過終端在我們的系統中安裝 CacheTools 模塊。
如我們所見,Cachetools 模塊已經成功安裝到我們的系統中,現在我們可以在 Python 程序中調用這個模塊,通過 Python 處理我們系統中存在的緩存。
CacheTools 模塊:函數
在這裡,我們將只討論 Python 的 Cachetools 模塊中存在的函數。這裡需要注意的是,這些函數在 Python 中是裝飾函數類型,當我們使用這些函數時,它們將在程序中作為裝飾函數使用。
下面是 Python CacheTools 模塊中的五個函數:
- 藏起
- TTLCache
- rcache
- 烏克蘭語
- 圖片
現在,我們將詳細研究上面提到的所有函數,並通過在 Python 程序中使用它們來了解它們的工作原理。
緩存函數
CacheTools 模塊的緩存函數在 Python 中用作裝飾函數,默認情況下,它在程序中執行簡單的緩存。當我們使用緩存裝飾器調用一個函數時,它將緩存我們調用的函數,以便以後用作緩存。
語法:
下面是在 Python 程序中使用緩存裝飾函數的語法:
@cached(cache = {})
def any_funct():
pass # Code in the function
參數:在緩存裝飾器中,我們使用了一個帶有 any_funct()名稱的默認函數(我們甚至可以在函數內部給出參數)。在 Python 程序中使用緩存裝飾器時,我們將在默認函數中編寫邏輯代碼,而不是 pass
語句。
現在,讓我們在 Python 程序中使用這個緩存裝飾器來更好地理解它的工作原理以及它如何節省我們的時間。
例 1:
查看下面的程序,了解緩存裝飾器的實現:
# Import cached from Cachetools Module
from cachetools import cached
# Import time module in the program
import time
# Calling a function without using cache
def fiban(n):
return n if n<2 else fiban(n-1) + fiban(n-2)
# Calculating total time for performing operation
totalTime1 = time.time()
# Printing function result
print("Result of operation: ", fiban(31))
# Total time taken
print("Time Taken by function without cache: ", time.time() - totalTime1)
# Calling function using cache
@cached(cache = {})
def fiban(n):
return n if n<2 else fiban(n-1) + fiban(n-2)
# Result of operation performed by function
totalTime2 = time.time()
print("Result of operation: ", fiban(31))
# Now total time taken in Operation
print("Time Taken by function with cached: ", time.time() - totalTime2)
輸出:
Result of operation: 1346269
Time Taken by function without cache: 0.8905675411224365
Result of operation: 1346269
Time Taken by function with cached: 0.015604496002197266
說明:
我們首先從 CacheTools 模塊中導入緩存的裝飾函數,以便在我們的程序中使用它。我們還在程序中導入了時間模塊,以保持對時間的計數,並檢查緩存如何使函數調用更快。之後,我們定義了一個默認函數,並使用邏輯代碼通過運算來計算值,並在運算後返回值。當我們在這個 fiban()函數中使用一個參數來查找一個實際值時,我們先使用 time()函數,然後再找出執行這個操作需要多少時間。然後,我們打印我們在操作中得到的值以及執行操作所花費的總時間。之後,我們使用了緩存的 decorator 函數,並定義了相同的默認函數,即 fiban()函數,其中包含相同的邏輯。現在,我們再次使用時間函數來計算調用該函數所需的總時間。然後,在 fiban()函數中給定一個參數後,我們打印了操作以及使用緩存裝飾器執行操作所花費的總時間。
我們可以在輸出中看到,當我們在沒有緩存的情況下調用函數時,比使用緩存裝飾器調用函數花費的時間要多得多(當兩個函數相同時,具有精確的邏輯,並且在調用時具有相同的參數)。這就是緩存裝飾器如何通過將函數保存在緩存內存中來幫助我們,並在再次調用時節省大量時間。
TTLCache 函數
TTLCache 也稱為“生存時間”緩存,它也是 Cachetools Module 包中包含的裝飾函數。當我們在程序中調用一個函數時,它也在系統中的緩存上工作。但是它的工作方式與 Cachetools 模塊的前一個函數非常不同,我們可以從這兩個函數的語法中看到這一點。
語法:
下面是在 Python 程序中使用 TTLCache 裝飾函數的語法:
# Working on cache
@cached(cache = TTLCache(maxsize = 33, ttl = 600))
# Default function
def anyFunct():
pass # logical code in the function
參數:與緩存裝飾器語法不同,我們必須在 TTLCache 裝飾器函數中總共給出兩個參數。我們還可以看到,在使用函數中的參數之前,我們已經將緩存類型指定為 TTLCache。以下是我們必須在 TTLCache 裝飾器中使用的參數:
- maxsize:TTLCache 函數內部使用的 maxsize 定義了可以存儲在 TTL cache 內部的函數的最大大小。
- ttl: 我們在‘TTL’參數中給出的值表示函數的緩存將在系統的緩存內存中存儲多長時間。我們在 ttl 參數中給出的值以秒為單位。
現在,讓我們在 Python 程序中使用這個 TTLCache 裝飾器來理解它的工作原理以及它對我們的用處。
例 2:
查看以下程序,了解 TTLCache 功能的實現:
# Import cached and TTLCACHE from Cachetools Module
from cachetools import cached, TTLCache
# Import time module in the program
import time
# Using the TTLCache decorator
@cached(cache = TTLCache(maxsize = 64, ttl = 90))
# A default function to carry operations
def myFunc(num):
# Keeping track on total time taken
timeTaken = time.time()
time.sleep(num)
print("\nTotal time taken for executing the function: ", time.time() - timeTaken)
return (f"I am executed with the function number: {num}")
# Calling the default function twice
print(myFunc(4))
print(myFunc(4))
# Using sleep function to create a break
time.sleep(100)
# Calling function after break
print(myFunc(4))
輸出:
Total time taken for executing the function: 4.015438556671143
I am executed with the function number: 4
I am executed with the function number: 4
Total time taken for executing the function: 4.015343904495239
I am executed with the function number: 4
說明:
我們已經從程序中的 CacheTools 模塊和時間模塊中導入了緩存和 TTLCache 裝飾器,以記錄調用函數所花費的時間。我們使用了 TTLCache 裝飾器,定義其參數如下:定義的最大化參數等於 64,存儲緩存的總時間等於 90 秒。之後,我們定義了一個默認函數來執行操作,然後我們使用 time()函數來計算調用該函數所需的時間。之後,我們在 print 語句中調用了該函數兩次。然後,我們暫停使用 sleep()函數第三次調用該函數。我們使用了 100 秒的休息時間,因為在 TTLCache 函數的參數中,我們給了 90 秒來存儲緩存。
我們可以在輸出中看到,當我們第二次調用函數時,它是從 TTLCache 裝飾器的緩存中調用的。但是當我們暫停時,它再次從程序中調用,因為 TTLCache 不再存儲函數的緩存內存。
rcache
RRCache 是隨機替換緩存的縮寫,顧名思義,RRCache 是一種緩存技術,在這種技術中,函數從緩存內存中的項目中隨機選擇並丟棄它們。RRCache 函數隨機執行此操作,以便在需要時釋放緩存內存中的一些空間。
語法:以下是在 Python 程序中使用 RRCache 裝飾函數的語法:
# Working on cache
@cached(cache = RRCache(maxsize = 34))
# Default function
def anyFunct():
pass # logical code in the function
參數:RRCache 有一個必要的參數,即 maxsize,它與我們在 TTLCache 裝飾函數中使用的 maxsize 參數相同。除了這個參數,RRCache 還接受一個可選的“choice 參數”,在程序中默認設置為“random.choice”。
現在,讓我們在 Python 程序中使用這個 RRCache 裝飾器來理解它的工作原理以及它對我們的用處。
示例 3: 查看以下程序,了解 RRCache 功能的實現:
# Import cached and RRCACHE from Cachetools Module
from cachetools import cached, RRCache
# Import time module in the program
import time
# Using the RRCache decorator
@cached(cache = TTLCache(maxsize = 32))
# A default function to carry operations
def newFunc(m):
# Keeping track on total time taken
timeTaken = time.time()
time.sleep(m) # Delay in printing result displays task is under process
print("\nTotal time taken for executing the function: ", time.time() - timeTaken)
return (f"I am executed with the function number: {m}")
# Calling the default function in print statement
print(newFunc(2))
print(newFunc(3))
print(newFunc(1))
print(newFunc(2))
print(newFunc(2))
print(newFunc(4))
print(newFunc(3))
print(newFunc(3))
print(newFunc(2))
輸出:
Total time taken for executing the function: 2.01472806930542
I am executed with the function number: 2
Total time taken for executing the function: 3.01532244682312
I am executed with the function number: 3
Total time taken for executing the function: 1.015653133392334
I am executed with the function number: 1
I am executed with the function number: 2
I am executed with the function number: 2
Total time taken for executing the function: 4.003695011138916
I am executed with the function number: 4
I am executed with the function number: 3
I am executed with the function number: 3
I am executed with the function number: 2
說明:
在輸出中,我們可以看到 RRCache 裝飾器的行為和功能,因為它在任何函數調用時都會隨機丟棄緩存,並且很多時候函數是從存儲在 RRCache 中的緩存中調用的。
烏克蘭語
LRUCache decorator 函數在緩存的 decorator 中使用,就像我們已經了解的另外兩個函數一樣。LRUCache 代表最近最少使用的緩存,顧名思義,這個裝飾函數為程序中最近使用的函數保留緩存。
語法:以下是在 Python 程序中使用 LRUCache 裝飾函數的語法:
# Working on cache
@cached(cache = LRUCache(maxsize = 4))
# Cache stored for this Default function
def anyFunc():
pass # some logical code will be written here
參數: LRUCache decorator 只取一個參數,即 maxsize,它設置緩存中將存儲多少最近調用的函數。
現在,讓我們在 Python 程序中使用這個 LRUCache 裝飾器來理解它的工作原理以及它對我們的用處。
例 4:
查看以下程序,了解 LRUCache 功能的實現:
# Import cached and LRUCACHE from Cachetools Module
from cachetools import cached, LRUCache
# Import time module in the program
import time
# Using the LRUCache decorator function
@cached(cache = LRUCache(maxsize = 5))
# A default function to carry operations
def newFunc(m):
# Keeping track on total time taken
timeTaken = time.time()
time.sleep(m) # Delay in printing result displays task is under process
print("\nTotal time taken for calling the default function: ", time.time() - timeTaken)
return (f"I am executed with the function number: {m}")
# Calling the default function in print statement
print(newFunc(2))
print(newFunc(3))
print(newFunc(1))
print(newFunc(2))
print(newFunc(2))
print(newFunc(4))
print(newFunc(3))
輸出:
Total time taken for calling the default function: 2.0151138305664062
I am executed with the function number: 2
Total time taken for calling the default function: 3.015472888946533
I am executed with the function number: 3
Total time taken for calling the default function: 1.0000226497650146
I am executed with the function number: 1
I am executed with the function number: 2
I am executed with the function number: 2
Total time taken for calling the default function: 4.015437841415405
I am executed with the function number: 4
I am executed with the function number: 3
說明:
我們調用的默認函數正好比我們為 LRUCache 函數定義的 maxsize 參數多 2 倍,即 7 倍。這是因為,首先,當函數被調用時,它被存儲在 LRUCache 的高速緩衝存儲器中,然後,在接下來的 5 次中,函數將從 LRUCache 裝飾器中存在的高速緩衝存儲器中被調用。但是,這是第七次從程序的內存中調用默認函數,因為現在它超過了我們為 LRUCache 定義的最大大小,即 5。
我們可以看到 LRUCache 裝飾器導致的結果變化。
圖片
LFUCache 是另一種類型的緩存技術,代表最少使用的緩存,它也在緩存裝飾器中被調用。LFUCache 緩存技術用於檢索項目或函數在程序中被調用的頻率(頻率)。在 LFUCache 緩存技術中,程序中調用次數最少的項或函數會從緩存中丟棄,執行此操作是為了釋放緩存中的一些空間。
語法:以下是在 Python 程序中使用 LFUCache 裝飾函數的語法:
# Working on cache
@cached(cache = LFUCache(maxsize = 5))
# Cache stored for this Default function
def newFunc():
pass # some logical code will be written here
參數:lfcache decorator 函數只接受一個參數,即 maxsize,它設置了緩存中將存儲多少個調用頻率最低的函數。如果一個項或函數參數在 maxsize 中沒有被連續調用多次,那麼它將從緩存中被丟棄。
現在,讓我們在 Python 程序中使用這個 LFUCache 裝飾器來理解它的工作原理以及它對我們的用處。
例 5:
查看以下程序,了解 LFUCache 功能的實現:
# Import cached and LFUCACHE from Cachetools Module
from cachetools import cached, LFUCache
# Import time module in the program
import time
# Using the LFUCache decorator function
@cached(cache = LFUCache(maxsize = 5))
# A default function to carry operations
def newFunct(anyNum):
# Keeping track on total time taken
totalTime = time.time()
time.sleep(anyNum) # Delay in printing result displays task is under process
print("\nTotal time taken for calling the default function: ", time.time() - totalTime)
return (f"I am executed with the function number: {anyNum}")
# Calling the default function in print statement with number parameters
print(newFunct(2))
print(newFunct(3))
print(newFunct(1))
print(newFunct(2))
print(newFunct(2))
print(newFunct(4))
print(newFunct(1))
print(newFunct(3))
print(newFunct(3))
print(newFunct(2))
輸出:
Total time taken for calling the default function: 2.01556396484375
I am executed with the function number: 2
Total time taken for calling the default function: 3.00508451461792
I am executed with the function number: 3
Total time taken for calling the default function: 1.0156123638153076
I am executed with the function number: 1
I am executed with the function number: 2
I am executed with the function number: 2
Total time taken for calling the default function: 4.0026326179504395
I am executed with the function number: 4
I am executed with the function number: 1
I am executed with the function number: 3
I am executed with the function number: 3
I am executed with the function number: 2
說明:
我們已經調用了參數為 3 的默認函數,在此之後,我們連續 5 次沒有調用它,這等於 LFUCache 函數中的 maxsize 參數)。因此,LFUCache 將從緩存中丟棄參數為 3 的默認函數,並將從程序的內存中再次調用它。
我們可以看到由於 LFUCache 裝飾器導致的結果變化。
結論
在這裡,我們已經完成了 CacheTools 模塊教程,並了解了 CacheTools 模塊中的所有五個函數。我們已經了解了 CacheTools 模塊的功能在 Python 程序中是如何工作的,以及如何使用它們來處理程序的緩存。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/272035.html