python函数的本地磁盘缓存装饰器。
cache-to-disk的Python项目详细描述
缓存到磁盘
具有自动失效的python函数的本地磁盘缓存装饰器。
这是为了缓存运行时间较长的函数,并且返回值占用的内存太多,无法用redis缓存在内存中。函数的结果将被pickle并保存到文件中,然后取消pickle并在下次调用函数时返回。缓存是特定于参数的,因此如果使用不同的参数调用该函数,则将再次运行该函数。缓存修饰符接受一个整数,表示要为其缓存函数的天数。过了这么多天,下次导入此模块时,将删除该函数的文件。
安装
pip install cache_to_disk
功能:
缓存到磁盘(n天缓存)delete_disk_caches_for_function(function_name)
示例:
缓存到磁盘
"""This example caches the function "my_function" for 3 days."""fromcache_to_diskimportcache_to_disk@cache_to_disk(3)defmy_function():to_return=[]foriinrange(10000):forjinrange(i):to_return.append(i*j**.23)returnto_return
delete_disk_caches_for_函数
"""This example invalidates all of the caches for the function "my_function". The function will be invalidated automatically, but this should be used when the function definition has been changed and you want it to re-run."""fromcache_to_diskimportdelete_disk_caches_for_functiondelete_disk_caches_for_function('my_function')