我有一些我需要在客户端缓存的小数据对象。这是用户信息,如他们的名字和角色。我有HttpCacheability.Private集合。当我使用Fiddler查看页面时,可以看到缓存是私有的,但不是为每个用户存储缓存,而是当不同的用户浏览到该网站时,它将显示首先登录的用户的缓存值。我好像是在存储服务器值。HttpCacheability.Private不按预期工作
例如,缓存在10分钟内过期。缓存过期后,我去了网站。缓存为空,因此它从数据库中查找我的用户信息(基于当前登录用户)并将其写入缓存。现在,John Doe在我做完3分钟后到达现场,他看到了我的信息而不是他的。
我不知道还需要做什么来缓存每个用户的信息在他们的客户端,而不是服务器。任何帮助将被赞赏。
感谢,
朗达
public static void AddUserToCache(User userToCache, string Key)
{
HttpContext context = HttpContext.Current;
HttpCachePolicy cachePolicy = context.Response.Cache;
cachePolicy.SetCacheability(HttpCacheability.Private);
if (context.Cache[Key] == null)
{
context.Cache.Add(Key, userToCache, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(UIConstants.CACHETIMEOUTHOURS, 0, 0), System.Web.Caching.CacheItemPriority.Default, null);
}
}
if (Cache["User"] == null)
{
user = BusinessUtility.GetCurrentUser();
if (user != null)
{
CacheUtility.AddUserToCache(user, "User");
}
}
else
{
user = (User)Cache["User"];
}
我还应该提到,在我的代码的原始版本中,我没有cachepolicy的东西作为私人应该是默认的。它不会以任何方式工作。 – Rhonda 2012-02-28 19:43:33