问题描述
缓存 VS 会话 VS Cookie 的注意事项有哪些?
What are the do's and don'ts about Cache VS Session VS Cookies?
例如:
我经常使用会话变量,当用户开始订购产品然后去吃午饭并在几个小时后回来继续预订时,有时在预订应用程序中会出现问题.我将预订存储在会话中,直到用户确认或中止预订,因此当用户只需单击浏览器中的 X 并且永远不会回来时,我不需要与数据库交谈并处理数据库中的中途预订.
For example:
I'm using Session variables a lot and sometimes have problems in a booking-application when users start to order products and then go to lunch and come back some hours later and continue the booking. I store the booking in the session until the user confirms or aborts the booking so I don't need to talk to the database and handle halfway bookings in the database when users just click the X in the browser and never comes back.
我应该为此使用缓存或 cookie 或某种组合吗?
Should I instead use cache or cookies or some combination for this?
(此外,当应用程序出现错误时,会话对象会自行重置,因此我会遇到更多问题)
(Also when there is some error in the app, the session-object resets itself and I get more problems because of that)
我主要从事桌面编程,但我觉得我在这里缺乏很多知识,因此任何可以扩展在何处使用缓存、会话、Cookie(或数据库)的人将不胜感激
I'm mostly doing desktop-programming and feel I lack lots of knowledge here so anyone who can expand on where to use Cache, Session, Cookies (or db) would be appreciated
从答案看来,DB 和 cookie 的组合是我想要的.
From the answers it seems that a combination of DB and cookies is what I want.
- 我必须将预订存储在连接到会话 ID 的数据库中
- 将会话 ID 存储在 cookie(加密)中.
- 每个页面加载检查cookie并从数据库中获取预订
- 我有一个每周运行一次的清理程序,用于清理未完成的预订.
我无法将预订存储为 cookie,因为这样用户就可以更改价格和其他敏感数据,而我必须验证所有内容(无法信任数据).
I can't store the booking as a cookie because then the user can change prices and other sensitive data and I had to validate everything (can't trust the data).
我做对了吗?
感谢大家的精彩解释!
推荐答案
从桌面应用程序的角度进入 Web 世界时,状态管理是掌握的关键.
State management is a critical thing to master when coming to Web world from a desktop application perspective.
Session
用于在服务器上存储当前 Web 会话的每个用户信息.它支持使用数据库服务器作为后端存储.Cookie
应该用于存储当前 Web 会话的每个用户信息或客户端的持久信息.em>,因此客户端可以控制 cookie 的内容.Cache
对象在单个应用程序中的用户之间共享.它的主要目的是缓存数据存储中的数据,不应用作主存储.它支持自动失效功能.Application
对象在用户之间共享以存储应用程序范围 状态,应相应地使用.
Session
is used to store per-user information for the current Web session on the server. It supports using a database server as the back-end store.Cookie
should be used to store per-user information for the current Web session or persistent information on the client, therefore client has control over the contents of a cookie.Cache
object is shared between users in a single application. Its primary purpose is to cache data from a data store and should not be used as a primary storage. It supports automatic invalidation features.Application
object is shared between users to store application-wide state and should be used accordingly.
如果您的应用程序被许多未经身份验证的用户使用,我建议您将数据存储在 cookie 中.如果需要身份验证,您可以手动将数据存储在数据库中,也可以使用 ASP.NET 配置文件管理功能.
If your application is used by a number of unauthenticated users, I suggest you store the data in a cookie. If it requires authentication, you can either store the data in the DB manually or use ASP.NET profile management features.
这篇关于缓存 VS 会话 VS cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!