[EN] [ASP.NET MVC] Custom output cache provider

September 13, 2011 -

Since ASP.NET 4 was released it’s possible to write custom output cache providers to change the way data are cached. For example, one of my customer wanted that some actions were cached on the file system to avoid loss of cache during an application pool reboot.

Be careful by using the following code because in many scenarios it can’t be used “as it is” (in a web farm, for example).

A custom output cache provider is a simple class that inherits from OutputCacheProvider and overrides these methods :

  • Add : adds an entry in the cache
  • Get : gets an entry from the cache
  • Remove : removes an entry from the cache
  • Set : updates an entry in the cache

Cached items must be serializable. In this sample, the following class will be used :

[Serializable]
public class CachedItem
{
    public object Item { get; set; }
    public DateTime UtcExpiry { get; set; }
}

In order to save an item on the file system the two following methods will be used. The first allows to get a file path from the cache key (generated by asp.net) and the second allows to save a CachedItem on the disk :

private string GetFilePathFromKey(string key)
{
    foreach (var invalidChar in Path.GetInvalidFileNameChars())
        key = key.Replace(invalidChar, '_');

    return Path.Combine(CacheDirectory, key);
}

private void SaveCachedItem(CachedItem cachedItem, string filePath)
{
    if (File.Exists(filePath))
        File.Delete(filePath);

    using (var stream = File.OpenWrite(filePath))
    {
        var binaryFormatter = new BinaryFormatter();
        binaryFormatter.Serialize(stream, cachedItem);
    }
}

The Add method :

public override object Add(string key, object entry, DateTime utcExpiry)
{
    string filePath = GetFilePathFromKey(key);
    var cachedItem = GetCachedItem(filePath);

    if (cachedItem != null && cachedItem.UtcExpiry.ToUniversalTime() <= DateTime.UtcNow)
    {
        Remove(key);
    }
    else if (cachedItem != null)
    {
        return cachedItem.Item;
    }

    SaveCachedItem(new CachedItem()
    {
        Item = entry,
        UtcExpiry = utcExpiry
    }, filePath);

    return entry;
}

The Get method :

public override object Get(string key)
{
    string filePath = GetFilePathFromKey(key);
    var cachedItem = GetCachedItem(filePath);
    if (cachedItem != null)
    {
        if (cachedItem.UtcExpiry.ToUniversalTime() <= DateTime.UtcNow)
        {
            Remove(key);
        }
        else
        {
            return cachedItem.Item;
        }
    }

    return null;
}

The Remove method :

public override void Remove(string key)
{
    string filePath = GetFilePathFromKey(key);
    if (File.Exists(filePath))
        File.Delete(filePath);
}

The Set Method :

public override void Set(string key, object entry, DateTime utcExpiry) {
    string filePath = GetFilePathFromKey(key);
    var cachedItem = new CachedItem() {Item = entry, UtcExpiry = utcExpiry};
    SaveCachedItem(cachedItem, filePath);
}

Now that the output cache provider is created, it can be registered in the web.config file of the application, in the caching section :

<system.web>
<caching>
  <outputCache>
    <providers>
      <add name="FileSystemOutputCacheProvider"
           type="Samples.FileSystemOutputCacheProvider, Samples"/>
    </providers>
  </outputCache>
</caching>

The last step consists to override the GetOutputCacheProviderName method in the Global.asax file. Actually this method is called by the ASP.NET Framework when an OutputCache attribute (or an OutputCache directive) is used. The method returns the name of the output cache provider to use in the current context. For example, if you want to use the FileSystemOutputCacheProvider for the Details action of the Products controller, you can use this code :

public override string GetOutputCacheProviderName(HttpContext context)
{
    if (context.Request.RawUrl.ToUpper().Contains("PRODUCTS/DETAILS")) {
        return "FileSystemOutputCacheProvider";
    }
    return base.GetOutputCacheProviderName(context);
}

As mentioned previously you just have to set an OutputCache attribute on the action Details of the Products controller to use the custom output cache provider :

public class ProductsController : Controller
{
    [OutputCache(Duration = 3600, VaryByParam = "id")]
    public ActionResult Details(int id)
    {
        //long operation
        Thread.Sleep(2000);

        ViewBag.ProductId = id;
        return View();
    }
}

Download the source code here.

Hope this helps Winking smile

Comments

Share

Tags


Twitter


#Docker Container Images are now out for #WindowsServer version 1709! https://t.co/T6J0w1kkOW

October 20, 2017 16:42

Getting started with #Traefik and #Kubernetes using #Azure Container Service https://t.co/eW8g370GiS

October 20, 2017 16:40

What’s new in the #Windows 10 Fall Creators Update https://t.co/zkSIsl8anJ

October 17, 2017 15:36

#DockerCon: #Kubernetes orchestration also comes with the latest beta of #Docker for Mac and Windows https://t.co/JYGqa6RTka

October 17, 2017 09:13

RT @sferquel: Docker supports the real #kubernetes orchestrator. So it does not lock you in. Kubectl with docker ee ? Yes we can ! #DockerC

October 17, 2017 09:11

#DockerCon: @Docker Platform and #Moby Project add #Kubernetes https://t.co/LnHeUxzApB

October 17, 2017 09:07

Whoa! Big announcement today at #DockerCon: Extending #Docker Enterprise Edition to Support #Kubernetes https://t.co/lgf2jec1Bs

October 17, 2017 09:04

#Nodejs dev? Learn how to host your apps on #Azure with @John_Papa https://t.co/Z0i7PGcxQc

October 17, 2017 06:28

Cloud Service Map for AWS and Azure Available Now https://t.co/5KZmItRpbI

October 17, 2017 06:19

Check this video to discover how to use #Jenkins Plugins for #Kubernetes w/ @Azure #ContainerService https://t.co/kkcTu5K9li

October 17, 2017 06:06

Edge Device Discovery - an Unfinished Project https://t.co/CyjjjHXscL

October 17, 2017 05:57

Zip Push Deployment for #Azure Web Apps, #Functions and #WebJobs https://t.co/SDurPqdK8P

October 17, 2017 05:41

Migration tips from IaaS to Web App for #Containers on @Azure https://t.co/T03FXCIqIr

October 16, 2017 08:14

Microsoft Cognitive Services - How Content Moderator helps to boost online safety https://t.co/6VtyV28zhe

October 16, 2017 08:13

@mandinette77 Il a plutôt parlé d'info sorties de leurs contexte avec comme exemple Twitter. Ce tweet en est une belle illustration btw :-)

October 15, 2017 22:20