简体   繁体   中英

Django: how to store a lot of data between requests if I don't have a database

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

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

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

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.

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.

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.

Or is there a better approach?

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM