Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Cache.Insert("City", UserCity, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(24, 0, 0));
我想为特定用户保存 24 小时的缓存,但是每当我打开页面时,都会为所有用户显示相同的缓存详细信息。我如何区分每个用户?
忽略这是否是一个好主意的考虑,您可以通过在缓存键中包含唯一标识用户的内容(例如 userName)来做到这一点:
string key = HttpContext.Current.User.Identity.Name + "-City"; Cache.Insert(key,...)
如果您可以拥有大量用户,则将特定于用户的数据放入缓存中是不可扩展的,但在某些情况下可能是合适的。例如,RoleProvider在已知并发用户数上限的 Intranet 应用程序中使用的自定义将用户特定角色存储在 Cache 中是合理的。
RoleProvider