Pattern for ASP.NET Cache and the new Cache.Insert overload in 3.5 SP1

Posted by: Jotekes Blog, on 24 Aug 2008 | View original | Bookmarked: 0 time(s)

David Penton, a fellow AspInsider, has published an excellent article on AspAlliance how to take pattern-like approach to ASP.NET cache

A New Approach to HttpRuntime.Cache Management
http://aspalliance.com/1705_A_New_Approach_to_HttpRuntimeCache_Management

In the article David covers how to approach the two most common issues with ASP.NET Cache, that is how to deal with the first hit in thread-safe, CPU-safe and database friendly manner and the other is how to do repopulation in the background. It is important to realize that populating the cache on the first hit can easily be a performance hog if it isn't understood how ASP.NET actually runs multiple threads as it handles simultaneous requests. You might end up with multiple threads trying to do the initial populating of cache which basically is a waste of resources, and depending on your app, might be unacceptable.

After the data is in the cache and expires for the first time, comes the background populating task/issue and to complement the options,  ASP.NET 3.5 SP1 brings an exciting new feature which is a new overload in Cache.Insert[1] which makes it possible for user code to be notified before item will be removed from cache. Note that till pre-SP1 you've got the notification after the item is already expired. The new overload looks like

void Cache.Insert( String key,

                   Object expensiveObject,

                   CacheDependency dependencies,

                   DateTime absoluteExpiration,

                   TimeSpan slidingExpiration,

                   CacheItemUpdateCallback onUpdateCallback

                 );

and the signature of callback method dictated by CacheItemUpdateCallback

void CacheItemUpdateCallback( String key,

                              CacheItemUpdateReason reason,
                              out Object expensiveObject,

                              out CacheDependency dependencies,

                              out DateTime absoluteExpiration,

                              out TimeSpan slidingExpiration

                            );

There's a few things to pay attention to when using this approach and I suggest you have a look at the referenced blog post by Thomas Marquardt to understand all the bells and whistles.

 [1] Source and reference: http://blogs.msdn.com/tmarq/archive/2008/07/22/asp-net-cache-can-notify-you-before-an-entry-is-removed.aspx

Share this post: email it! | bookmark it! | digg it! | reddit! | kick it! | live it!

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: ASP.NET | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 1482 | Hits: 30

Similar Posts

  • Oredev Wrap-Up more
  • Interviewing Javier Lozano 7 Nov 2009 (tonight) at 8pm PST more
  • StackOverflow DevDays in London more
  • Using SqlBulkCopy To Perform Efficient Bulk SQL Operations more
  • Principles, Patterns, and Practices of Mediocre Programming more
  • O# or The Fall Conference Speaking Schedule more
  • Business Apps Example for Silverlight 3 RTM and .NET RIA Services July Update: Part 25: ViewModel more
  • Using Distributed Cache with Telerik OpenAccess ORM more
  • Adding Syntactic Sugar to the Spark View Engine more
  • Reporting Release History : Q2 2009 SP1 (version 3.1.9.807) more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD