将用户数据存储在 MVC 2 中

发布于 2024-11-05 05:43:25 字数 560 浏览 0 评论 0原文

因此,当用户访问我的页面时,我想要存储他们与页面交互方式的某些数据。但这些数据都没有被持久化,所以我试图找出最好的方法来做到这一点。

例如,他们登录我的页面,我通过 javascript/google 地图获取他们的位置。我应该将其存储在会话中吗?我还通过 javascript 获取他们的 IP。我应该在每次需要使用 JavaScript 查询时进行查询,还是应该将其存储在会话中?

缓存比在会话中存储内容更好吗?我是否应该将所有这些都放入 model 中,将 model 放入缓存中,然后使用 Guid 来检索正确的 model 对于缓存中的正确用户?如果成千上万的用户同时访问该页面,这会变得非常昂贵吗?

用户还可以使用我作为存储过程的参数收集的这些信息从数据库中获取记录列表。这个列表有时需要重新查询,但其他时候,只需要在页面上重新排序即可。我不想每次只是对它们进行排序时都查询所有这些,但是缓存是否能够为每个登录用户保存数千条记录?

我以前从未在 MVC 中遇到过这些东西,所以我可以使用一些指导。

谢谢你们。

So when a user comes to my page, there is certain data that I want to store from how they are interacting with the page. None of this data is being persisted though, so I'm trying to figure out the best way to do it.

For example, they log onto my page, and I'm getting their location through javascript/google maps. Should I store that in the session? I'm also getting their IP through javascript. Should I just make the javascript query everytime I need to use it or should I store that in the session as well?

Is cacheing better than storing things in the session? Should I put this all in a model, put the model in the cache, and then use a Guid to retirve the right model for the right user in the cache? Would this get crazy expensive if thousands of users were on the page at once?

Users are also able to get a list of records from the database using this information that I'm gathering as params to a stored proc. This list sometimes needs to be re-queried for, but other times, it just needs to be re-sorted on the page. I wouldn't want to query for all of these everytime I'm just sorting through it, but would the cache be able to hold like thousands of records in it for each user logged in?

I've just never came across this stuff in MVC before so I could use a little guidance.

Thanks guys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从来不烧饼 2024-11-12 05:43:26

如果您想在用户访问过程中保留一些用户数据,您可以使用 ASP.NET 会话、cookie 或其他一些更持久的持久介质(例如数据库)。我通常将用户数据存储为序列化为 json、加密并写入 cookie 的对象。这使我能够在多次访问中保留数据,即使他们离开并回来也是如此。

至于缓存数据,有很多选择,ASP.NET 有一些很好的内置功能,可以让您更轻松地使用。当您缓存数据时,它将存储在 Web 服务器的内存中,除非您设置了外部缓存服务器(例如 memcached)。如果是公共数据,缓存的数据可以在多个用户之间共享,以消除缓存的浪费。您还可以设置缓存失效,以便在数据变旧时自动清除缓存。

至于要缓存的数据量,这取决于您要缓存的内容、这些对象在内存中的大小、将有多少用户创建这些缓存的对象以及您的服务器有多少可用内存。

理想情况下,您需要计算出来以确定成本是多少。

If you want to persist some user data through the course of the user's visit, you can use the ASP.NET session, cookies or some other more permanent persistence medium such as a database. I typically store user data as objects that are serialized to json, encrypted and written out to a cookie. This allows me to persist the data across multiple visits, even if they leave and come back.

As far as caching data, there are many choices and ASP.NET has some nice built in features to make it easier for you. When you cache data it will be store in the memory on your web server, unless you setup an external caching server such as memcached. The cached data can be shared between multiple users if it is common data, to eliminate wasteful caching. You can also set up caching invalidation so the cache gets automatically cleared if the data becomes old.

As far as amount of data to cache, that will depend on what you're caching, how large those objects are in memory, how many users will be creating these cached objects, and how much memory your server has available.

Ideally you would want to calculate it out to determine what the cost will be.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文