[英]What is the most efficient method to synchronize my database and the data received from an API?
I'm storing user's data and articles using the Pocket API into a SQLite Database using Django Framework.我使用 Django 框架将使用Pocket API 的用户数据和文章存储到 SQLite 数据库中。 How can I efficiently maintain consistency between the database and the data received from the API?
如何有效地维护数据库和从 API 接收到的数据之间的一致性? I'm planning to update the database once every 24 hours.
我计划每 24 小时更新一次数据库。
Should be simple with chron jobs .使用chron jobs应该很简单。 If you already have a model with logic to fetch data from the Pocket API and parse it, it's as simple as:
如果您已经有一个带有逻辑的模型,可以从 Pocket API 获取数据并对其进行解析,那么它很简单:
class MyCronJob(CronJobBase):
RUN_EVERY_MINS = 120 # every 2 hours
schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
code = 'my_app.my_cron_job' # a unique code
def do(self):
data = fetch_your_data()
save_data(data)
If you don't have the parsing logic yet, I'd suggest Django Rest Framework serializers .如果您还没有解析逻辑,我建议您使用Django Rest Framework serializers 。 Using the serializers is simple and saving the data shrinks down to:
使用序列化程序很简单,并且保存数据会缩小到:
def save_data(json_data):
serializer = YourPocketDataSerializer(json_data)
serializer.save()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.