0

I've got quite a lot of code on my site that looks like this;

Item item;

if(Cache["foo"] != null)
{
  item = (Item)Cache["foo"];
}
else
{
  item = database.getItemFromDatabase();
  Cache.insert(item, "foo", null, DateTime.Now.AddDays(1), ...
}

One such instance of this has a rather expensive getItemFromDatabase method (which is the main reason it's cached). The problem I have is that with every release or restart of the application, the cache is cleared and then an army of users come online and hit the above code, which kills our database server.
What is the typical method of dealing with these sorts of scenarios?

undone
  • 7,857
  • 4
  • 44
  • 69
Mikey Hogarth
  • 4,672
  • 7
  • 28
  • 44

1 Answers1

2

You could hook into the Application OnStart event in the global.asax file and call a method to load the expensive database calls in a seperate thread when the application starts.

It may also be an idea to use a specialised class for accessing these properties using a locking pattern to avoid multiple database calls when the initial value is null.

Andy Rose
  • 16,770
  • 7
  • 43
  • 49
  • Thank you, your comments got me searching in the right direction, ended up here; http://stackoverflow.com/questions/39112/what-is-the-best-way-to-lock-cache-in-asp-net – Mikey Hogarth Nov 21 '11 at 16:43