简体   繁体   English

在asp.net中为每个用户存储不同的缓存值

[英]Store different cache value for every user in asp.net

Cache.Insert("City", UserCity,
             null, System.Web.Caching.Cache.NoAbsoluteExpiration,
             new TimeSpan(24, 0, 0));

I want save cache for 24 hours for a particular user but whenever i open the page the same cache details are shown for all users. 我想为特定用户保存24小时的缓存,但是每当我打开页面时,都会为所有用户显示相同的缓存详细信息。 How do i differentiate for every user? 如何为每个用户区分?

Ignoring considerations of whether or not it's a good idea, you can do this by including something that uniquely identifies the user, such as the userName, in the cache key: 忽略是否​​是一个好主意的考虑,您可以通过在缓存键中包含唯一标识用户的内容(例如userName)来实现:

string key = HttpContext.Current.User.Identity.Name + "-City";
Cache.Insert(key,...)

Putting user-specific data in the Cache is not scalable if you can have large numbers of users, but there may be some cases where it's appropriate. 如果您可以拥有大量用户,则将特定于用户的数据放入缓存中是不可扩展的,但是在某些情况下,这是合适的。 For example, it would be reasonable for a custom RoleProvider used in an Intranet application with a known upper limit on the number of concurrent users to store user-specific roles in Cache. 例如,对于在Intranet应用程序中使用的并发用户数量已知上限的自定义RoleProvider ,将特定于用户的角色存储在Cache中是合理的。

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

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