[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


Deep Learning, Simulation and HPC Applications with #Docker and #Azure Batch https://t.co/pwpvriYfV4

September 24, 2016 16:56

RT @aurelienmaury: Les devs de @kodokojo seront au prochain #Mesos UG Paris le 5/10 au palais des Congrès. Merci @microsoftfrance de nous p…

September 23, 2016 17:38

RT @FranmerMS: Enterprise ready: Power BI for iOS now supports Intune MAM capabilities https://t.co/SICRhrt3LK via @MSPowerBI

September 23, 2016 17:35

#experiences - retour d'expérience sur #aspnet core et #Azure par @SantinFlo, @SebastienOll et @m_dab ->… https://t.co/j7kvY3srPH

September 23, 2016 12:01

#experiences : retrouvez moi avec @benjiiim pour un tour d'horizon d'#aspnet core ! https://t.co/QxgfpYhf7v #dotnethttps://t.co/m4Fbxy3xOl

September 22, 2016 13:13

@dgageot @nclerc https://t.co/nJS7W6cTMl

September 22, 2016 07:27

#experiences - Quelles architectures pour vos applications Cloud ? De la VM au conteneur, ça #PaaS ou ça #CaaS :… https://t.co/MLbqRRIItD

September 22, 2016 07:00

After UK, Microsoft #Azure is now availble in Germany -> that means there are two more regions in Europe https://t.co/9cm5hF7Efw #HyperScale

September 21, 2016 17:52

Le "#serverless computing" expliqué par @tomconte c'est à Microsoft #experiences ! https://t.co/iyEY33cFmS #Azurehttps://t.co/o2RFzR6lpm

September 21, 2016 13:13

Microsoft experiences’16 – Architectes, développeurs ne manquez pas la journée technique du 5 octobre ! https://t.co/WfRFzojh96 #experiences

September 21, 2016 07:38

Microsoft wants to crack the cancer code using artificial intelligence https://t.co/bmsr5AYZrf

September 21, 2016 04:39

#DevOps and Continuous Delivery Reference Architectures Using #Docker https://t.co/wOgkDmyOb0

September 20, 2016 08:50

Refreshing user logins in @Azure #AppService Mobile Apps https://t.co/rGpj0TIRvP

September 19, 2016 19:04

RT @OpenAtMicrosoft: KK announcing #Jenkins Project moving to @Azure at #JenkinsWorld during morning keynote https://t.co/Qyl87qpuWe

September 14, 2016 20:58

Announcing September 2016 Updates for .NET Core 1.0 https://t.co/qhJcGYkoTI #dotnet #dotnetcore

September 14, 2016 05:49