会员系统的缓存机制[加入缓存/清空缓存]

1.首次登陆 读取用户数据  然后加入缓存:

 

AccountDetail acc = null;
           try
           {
               if (!HttpContext.Current.Request.IsAuthenticated)
                   return null;

               string username = HttpContext.Current.User.Identity.Name;
               string cacheKey ="PASSPORT_" + username;
               if (HttpContext.Current.Cache[cacheKey] == null)
               {
                   acc =DataAccess.Interface.DataProvider.Instance().GetAccountDetail(username);
                   if (acc != null)
                   {
                       HttpContext.Current.Cache.Insert(cacheKey, acc);
                   }
               }
               acc =(AccountDetail)HttpContext.Current.Cache[cacheKey];
           }
           catch (Exception Ex)
           {
               log.Error("GetCurUser() Exception", Ex);
           }

2.有更新数据/清空缓存:
string username = HttpContext.Current.User.Identity.Name;
           string key = "PASSPORT_" + username;
           HttpContext.Current.Cache.Remove(key);

 

posted @ 2010-04-29 13:19  Tommmy  阅读(201)  评论(0编辑  收藏  举报