A New Approach to HttpRuntime.Cache Management
page 6 of 9
by David Penton
Average Rating: 
Views (Total / Last 10 Days): 76657/ 197


The biggest consideration for this pattern is what can and should be put into the loaderDelegate on TCache<T>.Get(). Since that method can be called from a background thread, plenty of things that are normally available from a typical ASPX page are not available there. For instance, anything that requires HttpContext is not available from a background thread. This includes things like QueryString and Form variables. If you have some sort of class level variable (that is not static) it will not be available either. The way to get around this is to set a local variable and make your call.

Listing 7

string <span class=Bold>name</span> = Request.QueryString[ "name" ];
CustomObject obj = TCache<CustomObject>.Get(
    "myCustomObjKey" // cache key we are using
    , 5 // number of seconds to keep in the cache
    , delegate() // this is the callback that populates the cache
        return new CustomObject( <span class=Bold>name</span> );

The background refresh option is enabled by default. If you want to disable that, there is a public static property ShouldEnableBackgroundRefresh to do this. The best place to set this would be in your global.asax in the Application_Start event. See the "Future" section for other plans concerning this setting.

Also, there is a CacheLoaderErrorHandler delegate as well. This is so that any errors that occur while executing the loaderDelegate will be able to be handled in any way you deem fit. This should also be set from the global.asax in the Application_Start event as well.

View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2020 ASPAlliance.com  |  Page Processed at 2020-03-31 12:42:02 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search