首页 > 解决方案 > 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?

标签: pythonpython-2.7

解决方案


我认为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 ...

它们将存储在storagedict 中(除非您终止该进程)。如果您想在终止进程后保留缓存,类似@ring.memcacheor的东西@ring.redis会帮助您。


推荐阅读