Reputation: 9397
I have asp net website with a very slow query on a remote database. To address that problem I use caching in Application_Start.
HttpRuntime.Cache["Folders"] = (from f in db.Folders select f).ToList();
in the controller
var folderList = (List<Folder>)HttpRuntime.Cache["Folders"];
It takes quite an amount of time to load the website the first time but when it's up it's fast. I also use the new serverAutoStart="true" feature of IIS so the website is always running with the Cache loaded. Even if the application pool restart IIS will load the website in a new W3wp process and switch the processes when the new instance is loaded. Resulting in no downtime or slow starting up.
But now I would like to reload the cache when some controller action occurs. So is it possible to reload it asynchronously without blocking all the website or the session that triggered the action while reloading it ? Also I would like that the current Cache["Folders"] still works during the operation.
Upvotes: 2
Views: 216
Reputation: 20674
You can make use of Parallel Tasks
var repopulateCache = new System.Action(() => RepopulateCache(someParameter);
Task.Factory.StartNew(repopulateCache);
It is recommended to know what you are doing before using this, try reading some background information.
That said, I've had this in production for a while and it works very well and means you don't have to worry (too much) about the usual perils of threading. It willl create background work on your web server but in your case it doesn't sound like a big risk.
Upvotes: 1