0

I'm storing data in cache as not to hit the database constantly (doesn't matter if the data is a little stale), the dataset isn't particularly large but the operation can take some time due to the complexity of the query (lot's of joins and sub queries). I have a static helper class and the data is used for binding on individual pages. The page calls it like so:

public static List<MyList> MyDataListCache
        {
            get
            {
                var myList = HttpContext.Current.Cache["myList"];

                if (myList == null)
                {


                    var result = MyLongRunningOperation();

                        HttpContext.Current.Cache.Add("myList", result, null, DateTime.Now.AddMinutes(3),
                            Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);



                        return result;
                    }
                }
                else
                {
                    return (List<MyList>)myList;
                }
            }
        }

This works fine unless lot's of people hit the page at the same time when the item is out of cache. Hundreds of the LongRunningOperations are spun up and cause the application to crash. How do I avoid this problem? I've tried using async tasks to subscribe to if the task is currently running but had no luck in getting it to work.

1 Answer 1

1

Upon starting this service, you should call LongRunningOperation() immediately to warm up your cache.

Second, you always want something to be returned, so I would consider a background task to refresh this cache prior to it's expiry.

Doing these two things will void the situation you described. The cache will be refreshed by a backgroundworker, and so everyone is happy :)

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

4 Comments

Thanks for the pre-fetch idea on app start, I'll definitely implement that. How would I implement a background task in ASP? I'm used to working with background workers in winforms. Also, would this keep the application constantly alive or would the worker pool process still get shut down after user inactivity?
You're welcome :) There are some good tips on background workers in asp.net on Scott Hanselman's blog: hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx -
The HttpContext.Cache is not available to background threads. Can you use the HttpRuntime.Cache instead?
Thanks, the examples on Hanselman's blog seemed a little overkill so I've implemented a timer control that updates the cache from global.asax and it seems to be working perfectly :)

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.