1

I have an ASP.NET Web Forms Application.

I have to display search results through a GridView control by using as DataSource a List<T> provided by my Repository layer.

Since I use the autopostback technique everytime the user changes a search filter, in order to reduce the amount of accesses to the database, I would like to store the initial List<T> result set in the cache in order to perform further filtering there.

I found many articles about caching in ASP.NET but all of them were embracing more complex scope than mine. Anybody knows a fast and clear way to do it?

2
  • If you ever want to modify the list, that won't work. Commented Jun 12, 2012 at 12:14
  • 1
    Did you check ASP.NET's built-in Cache? Commented Jun 12, 2012 at 12:33

2 Answers 2

1

I'm guessing each user will have their own unique resultset, so you could simply store the List<> in session.

Session[SessionKeys.MyResultsetKey] = myList;
Sign up to request clarification or add additional context in comments.

4 Comments

at the moment the list can give a maximum of 280.000 elements. Isn't it too much to be stored in the session?
Since I have no clue what you're storing in your List or why, it's very difficult to give you advise on how to retrieve or store your dataset. You asked for advise on how to cache a List, and you have two very good answers, Session or Cache. If you need further assistance on your overall design, I suggest asking a different question with more detail.
thanks, yes you are right. The list is composed by T objects with 1 int and 6 strings. May you please enlighten me with the main differences between using Cache and Session?
Cache is global - all sessions have access to the same data. This space is primarily used for application wide settings or datasets, like a list of States. Session is private to the current user and used for settings or datasets that are specific to each user, like a shopping cart.
0

To store it in Cache you can simply do like this

Cache["CacheKeyName"] = myList;

For checking if that key already exists on postback you can do like this

if(!IsPostBack)
{
 if(Cache["CacheKeyName"] == null)
 {
  Cache["CacheKeyName"] = myList;
 }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.