i have a web application with multiple pages. Each page is calling a class method to fetch data from database. Some of these calls are redundant, meaning we are firing queries multiple times to database to fetch same data. I want to query database once and store the result in some global variable, not sessions, so that pages can access those variables. I have already tried singleton pattern design, but it doesn't accept parameters while object creation. Please help
-
1Any reasoning on the fact to avoid sessions?Steve– Steve2013-10-21 19:58:43 +00:00Commented Oct 21, 2013 at 19:58
-
This seems like a perfect time to use session?Jason Renaldo– Jason Renaldo2013-10-21 19:59:02 +00:00Commented Oct 21, 2013 at 19:59
-
2It's not a use case fir session since he doesn't inficate the state changes per userRonan Thibaudau– Ronan Thibaudau2013-10-21 20:00:48 +00:00Commented Oct 21, 2013 at 20:00
Add a comment
|
2 Answers
Use Application.Cache to store the result and then access it in multiple pages. But remember that this information will be shared by all the users of your site.
DataTable dataTable = GetDataFromDatabase();
HttpContext.Current.Cache["CahcedTable"] = dataTable;
To access it:
DataTable dataTable = HttpContext.Current.Cache["CachedTable"] as DataTable;
if(dataTable != null)
{
//your code e.g. gridView1.DataSource = dataTable;
}
1 Comment
Tigran
also, if you are using SQL Server, it is possible enable (if it's not enabled by default) a query caching on server side, so the result of the query will be cached for some time and on the next same query, no IO will be executed, but returned result stored in memory: much faster.