Virtual Path Providers and Cache Dependencies

by Administrator 8. Juni 2011 07:31

Virtual Path Providers allow you to remap physical files when developing ASP.NET applications. You can plug in multiple providers into a pipeline and then intercept access to files and folders and return something else. This comes in handy in situations where you want to decouple the physical location from the virtual one or where there simple is no physical file.

However when there is no physical file, you usually want to take caching into your own hands. There is an overload on the base class that can be used to affect caching:

public override CacheDependency GetCacheDependency( string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart )

A common expectation (according to some articles I found on google) is, that returning null will disable caching, however this is not the case.

My solution to disable caching was to create a derived cache dependency class that immediately signals, that the dependency has expired:

public class NullCacheDependency : CacheDependency
{
  public NullCacheDependency()
  {
     SetUtcLastModified( DateTime.UtcNow );
     NotifyDependencyChanged( this, EventArgs.Empty );
  }
}

It seems to work fine so far. Give me some feedback, whether this helps you too.

Tags: ,

ASP.NET | ASP.NET MVC

About the author

for comments and suggestions contact:

 

Month List