Writing a web based flask api application with several modules. I would like to incorporate different permissions and privileges for different user logins for the different modules. Query is whether these privileges should be stored as session dictionaries or as shelve values? Which is more efficient and faster? Why would there be a preference of one over the other in this context?
2 Answers
I recommend take a look at NoSQL storage engines like Memcached or Redis.
They give you several advantages:
Place at a separate machine, so if you need to scale your app you'll be able to do it.
Extra interface to check what is stored in them.
Ability to flush if you really need once.
You can connect other apps to the these programs, so you can share sessions across several apps (however it's not recommended for big fast developing apps and keeping complicated structures).

- 12,113
- 5
- 38
- 59
Storing data in flask-related objects such as g object usually relates data close to your flask application context. It is not easy access data stored in g object out from this context and sometimes if you are not familiar with concept of flask's contexts you may have difficulties using related functionality.
I have not used shelve module but this is simple storage and data stored there should be available from any point of your application.
I guess there won't be much difference in performance or memory consumption between these 2 options, since all this data will be managed by the same python process. For sure due to internal implementation performance may differ but not dramatically I believe.
I agree with @Eugene that using external caching service may be another acceptable solution (consider in this case using of some flask modules like Flask-Redis). This may require some code tweaking because such storages usually cannot store any python object and sometimes you may need dump or pickle objects manually. But you may cache any amount of data not impacting python process itself, may use some neat things like data TTL (auto-remove cached data in given time) and other procs @Eugene listed in his answer.