我有一个用户控件,只要会话不变,我就想缓存它。
<%@ OutputCache Duration="18000" VaryByParam="none" VaryByCustom="changeIt" %>
现在,只要Session["UserId"]
不变,它就应该缓存,但是当会话值更改(甚至会话变为空)时,它应该清除缓存并再次维护,直到会话更改。 TIA。
我尝试在全局文件中使用以下代码,但未获得逻辑(如何比较当前会话和先前会话的值):
public override string GetVaryByCustomString(HttpContext ctx, string custom)
{
string name = System.Web.HttpContext.Current.Cache["KeyName"] as string;
if (custom == "changeIt")
{
string lastSessionVal = "", currentSessionVal = "";
if (Session["Last_BusinessID"] != null)
{
lastSessionVal = Convert.ToString(Session["Last_BusinessID"]);
}
if (System.Web.HttpContext.Current.Session["GSID"] != null)
currentSessionVal = Convert.ToString(System.Web.HttpContext.Current.Session["GSID"]);
else if (System.Web.HttpContext.Current.Session["PLID"] != null)
currentSessionVal = Convert.ToString(System.Web.HttpContext.Current.Session["PLID"]);
else
{
Session.Abandon();
Session.Clear();
string webaurl = ConfigurationManager.AppSettings["weburl"];
Response.Redirect(webaurl + "gs/business-provider-login");
}
Session["Last_BusinessID"] = currentSessionVal;
if (lastSessionVal != currentSessionVal)
{
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
//Response.Cache.SetCacheability(HttpCacheability.NoCache);
//Response.Cache.SetNoStore();
Session["ReloadLeftMenus"] = "1";
}
else { Session["ReloadLeftMenus"] = null; }
return Guid.NewGuid().ToString();
}
return base.GetVaryByCustomString(ctx, custom);
}
现在可以正常工作了,但是我想在新的会话值更改时再次缓存,应该缓存直到会话值再次更改。