【问题标题】:Caching a downloaded file based on modified time using dogpile使用 dogpile 根据修改时间缓存下载的文件
【发布时间】:2014-12-31 05:38:39
【问题描述】:
我正在编写一个程序,它可以下载一个大文件 (~150MB) 并将数据解析为更有用的文本格式文件。下载过程,尤其是解析过程很慢(总共约 20 分钟),所以我想缓存结果。
下载的结果是一堆文件,解析的结果是单个文件,所以我可以手动检查这些文件是否存在,如果存在,检查它们的修改时间;但是,由于我已经在代码中的其他地方使用带有 redis 后端的 dogpile 进行 Web 服务调用,我想知道是否可以使用 dogpile?
所以我的问题是:dogpile 可以根据修改时间来缓存文件吗?
【问题讨论】:
标签:
python
caching
dogpile.cache
【解决方案1】:
为什么你不想把程序分成几个部分:
您可以使用缓存变量来存储您需要的值,您将在文件更新时对其进行更新。
import os
import threading
_lock_services=threading.Lock()
tmp_file="/tmp/txt.json"
update_time_sec=3300
with _lock_services:
# if file was created more the 50min ago
# here you can check if file was updated and update your cache variable
if os.path.getctime(tmp_file) < (time.time() - update_time_sec):
os.system("%s >%s" %("echo '{}'",tmp_file))
with open(tmp_file,"r") as json_data:
cache_variable = json.load(json_data)