Welcome to WindowsClient.net | Sign in | Join

Zuker On Foundations

The realm of .NET (WPF, WCF and all around)

WCF Contrib v2.1 – Customize Output Caching Provider

Via http://wcfcontrib.codeplex.com/wikipage?title=ClientChannelOutputCache

Output Cache Provider Factory
This is the factory class that is responsible for creating the actual output cache provider.
This is where you extensibility point kicks in, you can replace the built-in factory with your own, read about it here.

The factory needs to have a public parameter-less constructor and implement 'IOutputCacheProviderFactory'

public interface IOutputCacheProviderFactory
{
    IOutputCacheProvider CreateProvider();
}

As you can see, this is a simple factory interface for providing the actual provider implementation.

Output Cache Provider

Output cache provider is where the magic really happens.
This is where you should implement your custom output caching solution if you like.
public interface IOutputCacheProvider
{
    void AddItem(OutputCacheItemKey key, OutputCacheInterval interval, object value);
    bool TryGetItem(OutputCacheItemKey key, out object value);
}
  • AddItem - this method is supposed to add a new item to the output cache solution.
  • TryGetItem - this method should look up the output cache solution for the item that was stored.
  • OutputCacheInterval - you're getting the desired interval as a parameter.
    • Consider setting the fields "LastVisitedAt" and "AddedAt" to the proper values at a given time so you can reference it later.
    • You can use "IsActive()" method if you like to check whether the interval is active for the current time according to its policy and time properties.
Note: If the provider implements IDisposable, it will be disposed once the OutputCacheController is disposed.
The built-in provider is called "IntervalBasedCacheProvider", it is important to know this since this too is written in a reusable way where you can plug-in your custom store.
The provider gets two parameters in its constructor:
  1. purgeTimerIntervalInMilliseconds - The interval in milliseconds for the timer that is in charge of iterating the items and purging those which are inactive.
  2. store - An implementation for IOutputCacheStore
    1. AddItem - The provider basically saves the key and interval in its local state, sets the time properties of the interval to the current time and adds the item to the store.
    2. TryGetItem - When an item is requested from the cache, the provider checks if there's an active key in its local state, if so, sets the last visited time to the current time and gets the item from the store.
    3. Removing Items - In the timer callback, it iterates the keys in its local state and removes the inactive ones from the store

Output Cache Store
The store is responsible for storing the items.
public interface IOutputCacheStore
{
    void SetItem(OutputCacheItemKey key, object value);
    void RemoveItem(OutputCacheItemKey key);
    bool TryGetItem(OutputCacheItemKey key, out object value);
}

The store that is provided with the built-in solution is using the SynchronizedDictionary that is a part of WCF Contrib as well.

Implementing your own custom output caching solution
You can go about it in two ways:

  1. Plug-in your own factory and provider and implement a different scenario - you can use this approach to write a provider that uses an interval-based caching solution such as HttpRuntime Caching, Enterprise Library, etc
  2. Plug-in your own factory and still use the built-in IntervalBasedCacheProvider only with your own custom store, such as a AppFabric caching, file-system, database, etc
Output Cache Item Key
It's important to recognize the description of the item keys if you like to implement you own solution since you will need a way a way to correlate between the item and key.
  • The key contains the operation action and parameters and custom equality comparers if any were provided.
  • It can be used as a dictionary key since it overrides Equals and GetHashCode by calculating it over the action and parameters using the appropriate comparer.
  • ToString() is also overriden to return the aggregation of the action and parameters.
Posted: Feb 08 2010, 09:15 PM by Amir Zuker | with no comments
Filed under: ,

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 

Page view counter