python - How to store information from JSON files in cache using Python?
问题描述
I have a Python script that is called every x minutes to parse a JSON file.
The JSON contains data points with an ID. I want to be able to see if I encountered an ID in a previous run of the script so that I can append the information contained in my data point to my old data with the same ID.
In order to do this I want to store the data as a key value pair in cache. The key in this case being the ID and the value the rest of the information contained in the JSON. How can I do this in Python?
解决方案
我认为Ring可以帮助您解决问题。https://ring-cache.readthedocs.io/en/latest/
当你的函数看起来像这样时:
def your_function(data_id):
return ...
Ring的适应性是这样的:
import ring
storage = {}
@ring.dict(storage, expire=60) # 60 seconds cache in `storage` dict
def your_function(data_id):
return ...
它们将存储在storage
dict 中(除非您终止该进程)。如果您想在终止进程后保留缓存,类似@ring.memcache
or的东西@ring.redis
会帮助您。
推荐阅读
- c# - 在类中创建方法的位置
- c# - 如何在 linq 或 lambda 中使用 join 和 where 子句编写 sql 查询
- sql - 使用 Outlook VBA 将数据插入 SharePoint 表(如何加快流程?)
- excel - Excel多个IF AND
- angular - Chartjs 图表未加载取决于我使用的方法
- android - ZXing IntentResult 给出空值
- javascript - 使用 FormData 发送文件
- c++ - 将缺少的 const 应用于文件、项目或解决方案
- java - 如何将外部 jar 依赖项包含到 Maven 项目中?
- python - 制作聚合网站时避免对 API 造成压力