1

NET and C# and writing an application that retrieves data from a database. I have attempted to cache the database locally in an attempt to speed up the application when doing a search. I have used the following lines of code to retrieve and save the data from the database in cache.

cached_database = HttpRuntime.Cache.Get(cacheID) as Entities;
if (cached_database == null)
{
    cached_database = InitializeDatabase(cacheID);
}



private Entities InitializeDatabase(string cacheID)
{
    var database = this.database;
    HttpRuntime.Cache.Insert(cacheID, database, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero);

    return database;
}

I can see that I am able to return the correct cached data by looking at the debugger and the application still runs however it does not have any performance improvements. Am I doing this wrong, or is there possibility a better way to accomplish what I would like to do? Thanks.

5
  • How did you identify the performance bottleneck in the first place? Were you just assuming it was with the database interaction? Don't just guess why your application is slow, measure it. Commented Dec 30, 2013 at 16:38
  • 1
    I wouldn't try to cache the database object. I would only cache results. Commented Dec 30, 2013 at 16:39
  • I have created a local database using the same data to test if there would be any difference and I can see a tremendous increase in speed the results are returned. Commented Dec 30, 2013 at 16:55
  • @user2683804, you are correct, networks and cross boundary calls are slow. Commented Dec 30, 2013 at 17:01
  • Is how I'm storing it correct? Because I don't feel like it is getting cached locally on my machine. Is there anyway I could check to see if it does? Commented Dec 30, 2013 at 18:00

1 Answer 1

4

I think you are caching the database object, an abstraction of the database. This object does the actual querying into the database when you access it's methods and properties. This is the expensive part - and this is why you don't get any effect from caching the database.

Cache the results, as suggested in the comments.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm not sure this would be possible for me since I would like to allow the user to also change their search criteria and go back to searching through the entire results at any point, which would include everything from the database. If not I may be understanding how I would do this wrong then.
You need to cache the results using a key that uniquely identifies the result set. You also need to think about what to cache, how, and how to invalidate the cache.
Alright well thanks, I feel like I have tried just about everything I could think of at this point but I'll see if I can come up with anything else that might work better.
One other thing. Is how I'm storing it correct? Because I don't feel like it is getting cached locally on my machine. Is there anyway I could check to see if it does?

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.