简体   繁体   English

Express webapp如何存储/访问用户设置

[英]Express webapp how to store/access user settings

Im currently developing a Nodejs/Express webapp similar to a dashboard/webbrowser homepage. 我目前正在开发类似于仪表板/ webbrowser主页的Nodejs / Express webapp。 The data is shown according to the user's settings. 根据用户的设置显示数据。 At the moment, I'm creating a json file (based on a template) when the user signs-up. 目前,我正在用户注册时创建一个json文件(基于模板)。 This json file has the settings I need to access in order to show the information on the main page. 此json文件具有我需要访问的设置,以便在主页面上显示信息。 Some of this information is: geolocation (city + country) to access a weather api (+ units, forecast days lenght, etc), a list of tvshows (so I can fetch a list of episodes from my db), etc. 其中一些信息是:地理位置(城市+国家)访问天气api(+单位,预测天数等),电视节目列表(所以我可以从我的数据库中获取剧集列表)等。

At the moment, when a user logs-in, I'm reading this file about 4 times. 目前,当用户登录时,我正在阅读此文件大约4次。 I'm sure this is not a good idea, and although its working fine now, I can see the problems I'm going to have in the future (when I add more settings and/or get more traffic). 我确信这不是一个好主意,虽然它现在工作正常,但我可以看到将来会遇到的问题(当我添加更多设置和/或获得更多流量时)。

Since the webapp is almost done, I want to take care of this. 由于webapp几乎已经完成,我想要解决这个问题。 I'm thinking on a couple of options: 我正在考虑几个选项:

  1. Save the user settings in a session store (Redis eg) 将用户设置保存在会话存储中(例如Redis)
  2. Save the user settings in a cookie(s) 将用户设置保存在cookie中
  3. Read the file once when the user logs in, and cache it somehow 用户登录时读取文件一次,并以某种方式缓存它

What's the best option to do this ? 这样做的最佳选择是什么?

If you're looking to store this information client-side (since you've mentioned cookies), you might consider looking into JavaScript's localStorage API (or sessionStorage if you want the data to expire when the user closes his/her browser). 如果您希望在客户端存储此信息(因为您已经提到过cookie),您可以考虑查看JavaScript的localStorage API (如果您希望数据在用户关闭其浏览器时过期,则查看sessionStorage )。

localStorage is a key:value pair (like you'd get with Redis), but the data is stored locally on the user's device. localStorage是一个关键:值对(就像你使用Redis一样),但数据存储在用户设备的本地。 As with any client-side storage solutions, you should never trust the data provided from localStorage without some verification, but it's a tool designed exactly for this kind of task. 与任何客户端存​​储解决方案一样,如果没有一些验证,您永远不应该信任localStorage提供的数据,但它是专为此类任务设计的工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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