0

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

3
  • 1
    Any reasoning on the fact to avoid sessions? Commented Oct 21, 2013 at 19:58
  • This seems like a perfect time to use session? Commented Oct 21, 2013 at 19:59
  • 2
    It's not a use case fir session since he doesn't inficate the state changes per user Commented Oct 21, 2013 at 20:00

2 Answers 2

3

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.
0

Use a static class, it (along with all it's static properties / fields) will only exist once per applucation domain (so, unless you do a few very specific thibgs, once per website)

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.