简体   繁体   English

同步我的数据库和从 API 接收的数据的最有效方法是什么?

[英]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.

相关问题 将数据保存到数据库中最有效的结构是什么? - What is the most efficient structure to save data in my database? 用来自Django后端的数据创建图表/图形的最有效方法是什么 - What would be the most efficient way of creating charts/graphs with data from a Django backend 将数据库导入Django / Postgres的有效方法是什么? - What is an efficient method to import a database into Django/Postgres? 将数据从 google api 导入我的数据库 - Importing data from google api to my database 同步本地文件夹和服务器的最有效方法(django dev) - Most efficient way to synchronize local folders and server (django dev) 在Django应用中,目前(自1.8版起)重用导致数据库查询的变量的最有效方法是什么? - What is the current (as of 1.8) most efficient way to reuse a variable that results in database queries in a Django app? Django:什么是最有效的基于日期和外键进行查询和汇总的方法 - Django: What is the most efficient method to query and aggregate based on date + foreign key 在对象上计数的最有效方法是什么? - What is the most efficient way to count votes on an object? 过滤 django 中的对象的最有效方法是什么 - What will be the most efficient way to filter the objects in django 将字符串与枚举进行比较的最有效方法是什么? - What is the most efficient way to compare a string to Enums?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM