Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is caching in ASP.NET shared across users or specific to an individual?

I have a quick question I'm just not sure about. I'm trying to increase performance on an intranet site. I'm trying to limit the trips to the database by implementing some caching. I'm new to caching and I wasn't sure is this shared across users? I'm hoping it's unique to the individual user. Like every user would have their own independent cache much like the session object. I'm trying things like this

   if (Cache["_InfoResult"] == null)
        {
            Cache["_InfoResult"] = db.GetInfoResultBySessionGuid(SessionGuid);
        }

        _InfoResult = (InfoResult)Cache["_InfoResult"];

and then using the _InfoResult object to drive areas of the page. My concern was that I want Cache["_InfoResult"] to be unique for each user. Is this correct or would this object be the same for each user? Thanks for clearing it up.

Cheers, ~ck in San Diego

like image 548
Hcabnettek Avatar asked Dec 07 '22 04:12

Hcabnettek


2 Answers

The ASP.Net Cache is attached to the application domain, so it's shared for all users.

If you want to cache something for an individual user, use the Session object.

like image 86
womp Avatar answered Dec 10 '22 02:12

womp


The cache has a global scope and it persists for the lifetime of your web application. It is not per httprequest.

You can test this with some simple code.

<asp:TextBox ID="txtData" runat="server" Width="200px"></asp:TextBox>
<br />
<asp:Button ID="btnPutToCache" runat="server" Text="Put to Cache" onclick="btnPutToCache_Click" />
<br />
<asp:Button ID="btnGetFromCache" runat="server" text="Get from Cache" onclick="btnGetFromCache_Click" />
<br />
<asp:Label id="lblGetFromCache" runat="server"></asp:Label>

And the codebehind:

protected void btnPutToCache_Click(object sender, EventArgs e) {
        Cache["data"] = txtData.Text;
    }
    protected void btnGetFromCache_Click(object sender, EventArgs e) {
        lblGetFromCache.Text = Cache["data"].ToString();
    }

Put all of the above code into one page. Deploy your website. Hit the page from instance of your browser window, put some test text into the text box and hit the Put to Cache button.

Open a new browser window and hit the page. Click the Get from Cache button, and observe the results.

like image 36
Jagd Avatar answered Dec 10 '22 02:12

Jagd