r3try
r3try

Reputation: 677

Using MVC SitemapProvider with AppFabric

in our asp.net mvc web-project we are using the mvc sitemapprovider as the solution for breadcrumbs. the basic stuff is working fine but now we looked a bit deeper into it and were wondering about the [SiteMapCacheRelease] attribute. The documentation states, that this attribute clears the cache for the given cache-key.

For our application we are using AppFabric as the caching-solution. The question is, how can i make mvc sitemapProvider force to use AppFabric too?

I feel like the following line is responsible for controlling which cache should be used (inside MvcSiteMapContainerInitializer).

container.RegisterSingle<System.Runtime.Caching.ObjectCache>(() => System.Runtime.Caching.MemoryCache.Default);

How can i bring those two together? (AppFabric uses DataCache whereas the SiteMapProvider seems to expect ObjectCache...)

Upvotes: 1

Views: 109

Answers (1)

NightOwl888
NightOwl888

Reputation: 56909

There are actually 2 extensibility points for caching in the default setup - System.Runtime.Caching.ObjectCache and MvcSiteMapProvider.Caching.ICacheProvider. There are also 2 implementations of ICacheProvider included in the box, AspNetCacheProvider (System.Web.Caching) and RuntimeCacheProvider (System.Runtime.Caching), but at this time there isn't a DataCacheProvider. This would make a great thing to contribute to MvcSiteMapProvider if you decide to implement one.

There is a post here that goes into how you can implement your own cache provider. You can for the most part just copy and paste the code from RuntimeCacheProvider and then edit it to accept a DataCache instead of an ObjectCache in the constructor. Of course, you will need to take care of wiring up the methods to the underlying provider, but there are only 5 methods and 1 event to deal with. The thread locking code is already handled by the MicroCache and LazyLock, so all you need to implement are the operations themselves.

Note that you will also need to implement your own cache dependencies to make it automatically reload when files change, which is a slightly more ambiguous process. The ICacheDependency returns an object datatype. Your cache provider must be implemented in such a way that ICacheProvider can return null to indicate no cache dependencies by configuring the NullCacheProvider, which your implementation should be compatible with.

There is also the matter of attaching multiple dependencies - you should implement a DataCompositeCacheDependency class that is similar to the RuntimeCompositeCacheDependency class. This means all of your dependency providers should return an IList (or some other collection type) and guard against null so they can be plugged into each other seamlessly. I don't know enough about DataCache to know how to implement that, but if you look at the existing cache dependencies, it should give you food for thought. If all else fails, you can just use the NullCacheDependency and rely entirely on the CacheReleaseAttribute to reload your SiteMap when the underlying data changes.

There is also a request-caching decorator here that you might want to use to keep the SiteMap object from loading multiple times per request. You might need it until version 4.7 is released to prevent exceptions from being thrown depending on how you configure the cache settings (see the link for full details).

Upvotes: 1

Related Questions