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.