Is ASP.NET caching available to users or to individual users?

I have a quick question that I'm just not sure about. I am trying to improve performance on an intranet site. I am trying to limit trips to the database by implementing some caching. Am I new to caching and was not sure if this is shared between users? I hope it is unique to the individual user. Just as each user will have their own independent cache, similar to a session object. I'm trying something like this

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

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

Cheers, ~ ck in San Diego

+4
source share
2 answers

ASP.Net Cache is attached to the application domain, so it is used by all users.

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

+11
source

The cache has a global scope and is maintained for the duration of your web application. This is not for http request.

You can verify this with 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 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 on one page. Expand your website. Remove the page from the instance of your browser window, place the test text in the text box and click the "Put in cache" button.

Open a new browser window and click on the page. Click the Get from Cache button and see the results.

+3
source

Source: https://habr.com/ru/post/1301026/


All Articles