Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the difference between HttpRuntime.Cache and Session? [duplicate]

Possible Duplicate:
Cache v.s Session

I am using some code that uses HttpRuntime.Cache to store a value. However when I close the window the cache goes away. Is there any benefit to using this over Session?

Here is my code:

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();
}
like image 569
Darren Avatar asked Dec 21 '11 22:12

Darren


3 Answers

HttpRuntime.Cache is global for the application; it is shared among all users/sessions of the web site.

Session is unique per user session. What one user session stores in the Session is private to that session. Another session will have its own storage.

like image 169
Anders Abel Avatar answered Oct 13 '22 12:10

Anders Abel


Possibly the reason you are seeing the cache clear down is that you are restarting your web server when you are rerunning your site. This would make it seem like the cache object and session were behaving in the same way when in fact they are very different.

It could be for instance that this happens when using Visual Studio's built in web server i.e. when running the site as a web application.

Just a thought.

like image 36
Crab Bucket Avatar answered Oct 13 '22 12:10

Crab Bucket


The Cache object lives in memory only and is global to the application. Objects can be removed from Cache at any time by ASP.NET (although you can influence that to some degree with arguments to Cache.Add()). When the AppPool recycles or shuts down due to inactivity, or if you restart your application (such as by changing web.config), the Cache will also be dropped.

Just closing a web page is not enough by itself to drop the Cache.

The Session object is unique per user session (usually unique per browser instance). At the end of each request, in can either be serialized and stored in SQL Server, or just kept in memory (InProc mode), or serialized and sent to a specialized app called StateServer. If it's stored in memory, it will be lost under the same conditions as Cache. If it's written to SQL Server, then it will be kept until the session expires, even if the AppPool recycles.

Note that the code in your question should use a lock() when reading/updating the Cache object. Otherwise, you have a potential race condition.

like image 37
RickNZ Avatar answered Oct 13 '22 11:10

RickNZ