In my new job I’ve been not-so-gently introduced to the world of ASP.NET. Coming from a Ruby on Rails background, ASP.NET is like a slap in the face. Ouch! I ran into some major gotchas today while adding caching to an RSS feed (generally a good idea, BTW), via the ASP.NET global cache, datasets, and SqlCacheDependency. I thought I would share them with you in case you are in the same boat.
- What in the world is SqlCacheDependency? See this article.
- Most of the tutorials for caching on MSDN deal with output caching. You can’t do output caching with a generic web handler (among other things, these were used to provide RESTful interfaces in ASP.NET before WCF came along to save the day). So you will have to build up your output string on your own and cache it with a dependency on your model.
- Make sure you are using SQL Server 2005 or later. Anything earlier requires lots of obligatory sit-ups to get things working, and uses polling behind the scenes (so it is much less efficient).
- Make sure you use a unique key when adding items to the cache that includes query parameters and anything else you are referencing when you query your model to build the response.
- If you are doing multiple queries, you will need to cache each one. If one query depends on a previous querie’s results, you need to use AggregateCacheDependency to make your cache expire not only when the main query’s results change, but also when the dependent query’s results change.
- You can’t create a SqlCacheDependency object in isolation. You must use the one that was associated with the exact same SqlCommand object you used to load up your dataset.
- You must create the SqlCacheDependency object before executing the command.
- SqlCacheDependency does not work on views! But, you can still load a “view” into a typed dataset by opening the view in you designer and copying the TSQL into the query string in you code.
Random