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 。老实说,我没有他妈的想法,为什么装饰器中的这种怪异行为会尽我所能在以后的更新中进行修复。如果您想检查我的其他工作或与我联系: