Dar*_*ren 16 c# asp.net 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();
}
Run Code Online (Sandbox Code Playgroud)
And*_*bel 24
HttpRuntime.Cache是全球性的应用程序; 它在网站的所有用户/会话之间共享.
Session每个用户会话是唯一的.一个用户会话存储在该会话中的Session私有内容.另一个会话将有自己的存储空间.