omoide cache
1.0.0
緩存不再需要難。只有幾行代碼, Omoide Cache將立即將您的Python服務提升到一個新的水平!
發布的圖書館:PYPI
源代碼:github
教程№1 :我的博客或媒介
這是一個堅固的,高度可調且易於整合的內存中緩存解決方案,用純Python編寫,沒有依賴性。
它被設計為方法級緩存,使用方法調用參數作為緩存鍵並存儲其返回值。
可自定義以適合您的特定用例,提供各種有效期和刷新選項。
非常用戶友好,非常易於與簡單的裝飾器集成(IE註釋,對於來自Java的人),無需在您的代碼中添加複雜的邏輯,只需在服務中的任何方法中使用@omoide_cache()即可。它將通過默認設置自動為您的方法生成緩存。您可以通過Dechator參數進一步調整這些設置。
公平的警告 - 該項目處於其生命週期的最早階段,將來會有很多改進和錯誤修復。非常歡迎所有建議和錯誤報告!
pip install omoide-cachegit clone https://github.com/jpleorx/omoide-cache.git
cd omoide-cache
pip install --editable . import time
from omoide_cache import omoide_cache
# A class where cache was added to a simulated long running method
class ExampleService :
@ omoide_cache ()
def time_consuming_method ( self , x : int ) -> int :
time . sleep ( 2.0 )
return x * x
service = ExampleService ()
# The first call will execute real logic and store the result in cache
service . time_consuming_method ( 1 )
# The second call will get results from cache
service . time_consuming_method ( 1 )在這裡,我們添加一個緩存,該緩存將丟棄最低頻繁訪問的項目,當時緩存變得太大。
import time
from omoide_cache import omoide_cache , ExpireMode
class ExampleService :
@ omoide_cache ( max_allowed_size = 10 , size_expire_mode = ExpireMode . ACCESS_COUNT_BASED )
def time_consuming_method ( self , x : int ) -> int :
time . sleep ( 2.0 )
return x * x 在這裡,緩存將自動刪除2分鐘前最後一次訪問的項目。
import time
from omoide_cache import omoide_cache
class ExampleService :
@ omoide_cache ( expire_by_access_duration_s = 120 )
def time_consuming_method ( self , x : int ) -> int :
time . sleep ( 2.0 )
return x * x另外,我們可以刪除2分鐘前計算的項目。
import time
from omoide_cache import omoide_cache
class ExampleService :
@ omoide_cache ( expire_by_computed_duration_s = 120 )
def time_consuming_method ( self , x : int ) -> int :
time . sleep ( 2.0 )
return x * x 在這裡,緩存將異步刷新2分鐘前計算的項目。嘗試刷新每10秒鐘一次。
import time
from omoide_cache import omoide_cache , RefreshMode
class ExampleService :
@ omoide_cache ( refresh_duration_s = 120 , refresh_period_s = 10 , refresh_mode = RefreshMode . INDEPENDENT )
def time_consuming_method ( self , x : int ) -> int :
time . sleep ( 2.0 )
return x * x@omoide_cache() ,而不是@omoide_cache 。老實說,我沒有他媽的想法,為什麼裝飾器中的這種怪異行為會盡我所能在以後的更新中進行修復。如果您想檢查我的其他工作或與我聯繫: