キャッシュはもう難しい必要はありません。わずか数行のコードOmoideキャッシュを使用すると、Pythonサービスを次のレベルに即座に実現します!
リリースライブラリ:Pypi
ソースコード:github
チュートリアル№1 :私のブログまたはメディア
これは、依存性のない純粋なPythonで記述された堅牢で、非常に調整可能で、統合しやすいインメモリ内キャッシュソリューションです。
メソッドレベルのキャッシュになり、単一のクラスメソッドをラッピングし、メソッドコールの引数をキャッシュキーとして使用し、その返品値を保存するように設計されています。
特定のユースケースに合わせてカスタマイズでき、さまざまな有効期限と更新オプションを提供します。
非常にユーザーフレンドリーで、シンプルなデコレーター(つまり、Javaから来る人のために、 @omoide_cache()など)と統合しやすく、複雑なロジックをコードに追加する必要はありません。デフォルト設定を使用して、メソッドのキャッシュを自動生成します。これらの設定は、デコレーターパラメーターを介してさらに調整できます。
公正な警告 - このプロジェクトは、ライフサイクルの最も早い段階にあり、将来的には多くの改善とバグの修正があります。すべての提案とバグレポートは大歓迎です!
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()を使用してください。正直に言って、なぜデコレーターにこの奇妙な振る舞いがあるのか、将来の更新でそれを修正するために最善を尽くします。あなたが私の他の仕事をチェックしたい場合や私に連絡したい場合: