views:

68

answers:

5

I have a List of objects in an asp.net page. when the page loads in the browser for the first time, I fetch data from the database, create objects with those data and populate the list. All these are done inside the page load event handler. Now when the page is posted back to the page, the previous list is gone, since the variables were all freed.

How can I cache that List, so that when this page is posted back, all my objects are available?

+1  A: 

use HttpContext.Current.Session.

Yossarian
Session state is not for caching general items.
Eilon
+1  A: 

Use Cache.Add(key_name, list_object, ...) to save the list, and then Cache[key_name] to retrieve it (you will need to cast the retrieved object to the appropriate type). The Add method has several more parameters to specify if and when the cached object expires.

The Cache object is actually System.Web.Caching.Cache and is accessible from your aspx page, or as HttpContext.Current.Cache.

Ray
+2  A: 

As @Eilon asked, is this data user-specific or is it site specific?

If the data is user specific you can use the Session State to store it, however it will expire when the users session ends, and in some cases can still invoke a roundtrip to your database server (if for instance it is backed by SQL server instead of being in-proc, etc).

If the data is application wide you can also use the Application Cache. It is site wide, resides in the process domain and is therefore available to everyone who has sessions on that server. Special care must be taken when using this in a multi-server scenario, but it is easily doable.

It should be noted that the Application Cache (and any other global setup) can make your app load slow for the first user to hit the site if the setup takes time. IIS7 and ASP.NET have attempted to address this with a module released recently that periodically wakes your app up to ensure that the global cache is either pre-populated, or remains alive.

GrayWizardx
+2  A: 

Hi,

if it's only relevant for this page, i would use the ViewState. The syntax is familiar if you used sessionstate before: to set:

ViewState["Persons"] = new List();

to read

List persons = ViewState["Persons"] as List;

The viewstate data is only kept for this page, but is sent as (serialized) text with your page, so don't use it for 1000 Persons because your page will be a very large download . If you have lots of data, you're better of using the Cache object, but only if you're not on a web farm and remember to clear the data once in a while when you no longer need the data and use a cache key per user if it is per user data. Last you can use the Session state, it is a per-user store, but remember to clear the data once in a while when you no longer need the data.

So, a lot to choose from depending on the situation.

Michel

Michel
For storing in viewstate, apart from the list size considerations, the objects need to be serializable
Petar Kabashki
+1  A: 

To add to GrayWizard's answer, you can use viewstate too, but since it's included in postbacks, make sure you're not persisting something that takes up alot of space or needs to be secure since it's not encrypted by default.

Here's an overview - it's dated, but still relevant.

http://msdn.microsoft.com/en-us/magazine/cc300437.aspx

Steve