c# - asp.net中的条件缓存用户控制
问题描述
我有一个用户控件,只要会话不变,我就想缓存它。
<%@ 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);
}
到目前为止它工作正常,但是我想在新的会话值更改时再次缓存,并且应该缓存它直到会话值再次更改。
解决方案
推荐阅读
- wpf - UniformGrid 中按钮的选取框/矩形选择
- jquery - focusout 上的验证消息移动按钮,它可以防止 onclick 事件
- sql - 如何改进这个 SQL 查询?
- office-ui-fabric - 枢轴:从不选择选项卡开始
- angular - Angular 8 - 找不到模块:错误:无法解析“fs”
- python - Linux 下 Python 3.5.3 的内存泄漏问题
- java - Spark:createDataset() 进入数据集
?
- javascript - 确定关闭所有选项卡与关闭当前选项卡
- certificate - 我可以从根 CA 创建证书链吗
- angular - 函数需要等待其他函数的结果