Django:如果我没有数据库,如何在请求之间存储大量数据 [英] Django: how to store a lot of data between requests if I don't have a database

查看:45
本文介绍了Django:如果我没有数据库,如何在请求之间存储大量数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,它将以各种方式可视化Strava(运动员的社交网站)用户的活动.

I am writing an app that will visualize Strava (social site for athletes) user's activities in various ways.

要求用户使用其Strava帐户进行身份验证,然后我的应用程序从Strava API下载其活动数据.

The user is required to authenticate with his Strava account, and then my app downloads his activity data from Strava API.

由于我的应用程序中将没有用户帐户-只有Strava身份验证-我决定不使用数据库.

As there will be no user accounts in my app - just the Strava authentication - I have decided to not use a database.

因此,我需要将活动数据存储在内存中.数据会很大:例如,我在Strava上有800个活动,每个活动都是冗长的JSON.

Therefore, I would need to store the activity data in memory. The data would be quite big: for example, I have 800 activities on Strava, each being a longish JSON.

是否适合将数据存储在像 session ['activities'] = some_downloaded_activities 这样的会话中?然后,我将设置API端点,该端点将查询该会话数据,并根据应用程序前端的要求对其进行过滤,然后将其返回.

Would storing the data in session like session['activities'] = some_downloaded_activities be suitable for this? I would then set up API endpoints that would query this session data, filter it as required by the front end of my application and return it.

我的另一种想法是仅使用数据库存储JSON文件,但在用户会话完成后立即删除数据-但这似乎太过分了.

My other idea is to use a database only to store the JSON file, but delete the data immediately after the user session is done - but it seems like an overkill.

还是有更好的方法?

推荐答案

我认为您可以在此处使用 Redis .每当用户进行身份验证时,活动数据(json)都可以像这样直接存储在redis中:

I think you can use Redis here. Whenever a user authenticates, the activity data(json) can be directly stored in redis like this:

import json
import redis

r = redis.StrictRedis(host='localhost', port=6379, db=0)
user_id = request.user.id
json_data = json.dumps(data)
r.set(user_id, json_data)

# delete key whenever the user logs out
r.delete(user_id)

您可以基于Redis中的json进行搜索,过滤等.您也可以存储特定时间的数据.请检查此库以进行Redis集成.

You can do search, filter etc based on that json from Redis. Also you can store data for specific time as well. Please check this library for redis integration.

这篇关于Django:如果我没有数据库,如何在请求之间存储大量数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆