Web 应用程序中的持久数据
存储持久数据的最佳方式是什么?我想保存用户最后一次在页面上活动的时间。起初我使用本地存储,然后我切换到cookie,因为它看起来更容易使用,现在我正在考虑将其存储在数据库中。你有什么建议。
另外,您能否建议一种在用户离开页面时进行注册的好方法?
What is the best way of storing persistent data ? I want to save the time the user was last active on the page.At first I used localstorage, then i switched to cookies because it seemed easier to use, now I'm thinking of storing it in a database.What do you suggest.
Also could you please suggest a good method of registering when a user left the page ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您希望该机制的用户特定程度,您应该选择客户端或服务器端方法。本地存储显然取决于设备,或者更准确地说取决于用户访问您的网站所使用的浏览器。
如果用户每次访问您的网站时都可能从同一台机器/浏览器访问您的网站,那么 cookie 就可以完成这项工作。不需要本地存储,因为这更适合更大的数据块。此外,用户可能使用不支持本地存储的浏览器。 Cookie 已存在多年,并且适用于几乎所有浏览器和设备。
但是,如果您的网站可能从不同的设备访问,例如家用计算机、办公室计算机,也可能是智能手机,那么您应该考虑使用用户帐户的服务器端解决方案。但要确保用户确实从创建用户帐户的努力中受益(可能使用像 stackoverflow 这样的 OpenID)。否则,您的网站的成功将面临风险。
简而言之......这更多的是一个设计决策,了解你的用户并确定你想要实现的目标。
Depending on how user specific you want that mechanism to be you should either go for a client or server side approach. Local storage is obviously depending on the device, or to be more precise what browser the user is using to access your website.
If your website is likely to be accessed from the same machine/browser every time the user visits your website then cookies should do the job. There is no need for local storage as this is more suitable for bigger chunks of data. Furthermore the user might be using a browser that doesn't support local storage. Cookies have been around for ages and will work on pretty much every browser and device.
If your website is however likely to be accessed from different devices, let's say home computer, office computer and maybe a smartphone too, then you should think about a server-side solution using a user account. But make sure that the user actually benefits from the effort to create a user account (maybe using OpenID like stackoverflow). Otherwise you put the success of your website at risk.
In short ... it's more a design decision, know your users and identify what you're trying to achieve.
Cookie 经常用于存储非关键用户首选项,这些首选项主要指网站使用情况,例如自动登录、语言、主题、时区等。由于这些首选项存储在用户的本地计算机上,因此 Cookie 是客户端技术,可以由客户端自由删除,因此您可能不希望依赖 cookie 来存储关键数据。
然而,如果存储特定于域的数据,例如用户名、密码、电子邮件、收藏夹等,您可能确实需要使用关系数据库 (RDBMS),例如 MySQL、MS SQL 或 PostgreSQL。这些在处理大量数据和有用功能(即交易、效率、灵活性)方面是可行的。
Cookies are frequently used for storing non-critical user preferences that refer mostly to website usage, such as auto-login, language, themes, time zone etc. Since these are stored on user's local machine, cookies are client-side technology and can be deleted freely by the client, so you probably would not want to rely on cookies for storing critical data.
However, in case of storing domain-specific data, such as usernames, passwords, emails, favorites etc., you really might want to use a relational database (RDBMS), such as MySQL, MS SQL or PostgreSQL. These are viable on handling massive amounts of data and feature scores of useful features, i.e. transactions, efficiency, flexibility.
如果您需要保存有关用户的其他信息,我会说将此信息与其他信息一起存储在数据库中,即您可以将字段“last_time”添加到用户数据库表中。如果您不使用数据库来代替(很奇怪),那么仅仅为了存储这些信息而添加一个数据库可能有点夸张。您能否详细说明如何使用这些信息?使用 cookie 允许您仅在用户连接到站点时才能访问它,因此我认为这不是最好的解决方案。
总的来说,无论如何,我认为SQL是最好的选择。
If you need to save other info about the users I would say to store this info in the database along with the other information, i.e. you could add a field "last_time" to the users database table. If you don't use a DB instead (very strange), adding one just to store this information may be a bit exaggerate. Could you elaborate a little on HOW you have to use this information? Using cookies allows you to access it only when the user is connected to the site, so I don't think it is the best solution.
Overall, anyway, I think SQL is the best choice.