【发布时间】:2012-01-25 16:39:22
【问题描述】:
可能重复:
Cache v.s Session
我正在使用一些使用 HttpRuntime.Cache 来存储值的代码。但是,当我关闭窗口时,缓存消失了。通过 Session 使用它有什么好处吗?
这是我的代码:
protected dynamic Code()
{
dynamic code;
if (String.IsNullOrEmpty(myHttpContext.Request.QueryString["code"]))
{
code = HttpRuntime.Cache["code"];
}
else
{
code = myHttpContext.Request.QueryString["code"];
HttpRuntime.Cache.Insert("code", myHttpContext.Request.QueryString["code"]);
}
return code;
}
protected string GetAccessToken(bool regenerate = false)
{
if (HttpRuntime.Cache["access_token"] == null || regenerate == true)
{
try
{
Dictionary<string, string> args = GetOauthTokens(myHttpContext.Request.QueryString["code"]);
HttpRuntime.Cache.Insert("access_token", args["access_token"], null, DateTime.Now.AddMinutes(Convert.ToDouble(args["expires"])), TimeSpan.Zero);
}
catch
{
OutputError("Code", "Bad Verification Code");
}
}
return HttpRuntime.Cache["access_token"].ToString();
}
【问题讨论】: