12

What is the correct way of using the following in a vNext application on an async method:

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]

I see it is part of System.Web.Caching, but the only place I could add that would be in the aspnet50 -> frameworkAssemblies section of my project.json file, which is incorrect.

Mihai Dinculescu
  • 19,743
  • 8
  • 55
  • 70

2 Answers2

29

ASP.NET Core 1.1/2.0 Answer

Add the response caching middleware like so:

public void Configure(IApplicationBuilder application)
{
    application
        .UseResponseCaching()
        .UseMvc();
}

This middleware caches content based on the caching HTTP headers you set in your response. You can take a look at the rest of the answer to see how to use ResponseCache.

ASP.NET Core 1.0 Answer

Use the new ResponseCache attribute instead. ResponseCache is not a direct replacement of OutputCache as it only controls client and proxy caching using the Cache-Control HTTP header.

If you want to use server side caching, see this StackOverflow question discussing how to use IMemoryCache or IDistributedCache.

// Add this to your controller action.
[ResponseCache(Duration = 3600)]

Here is an example using the new cache profiles:

// Add this to your controller action.
[ResponseCache(CacheProfile="Cache1Hour")]

// Add this in Startup.cs
services.AddMvc(options =>
{
    options.CacheProfiles.Add(
        new CacheProfile() 
        {
             Name = "Cache1Hour",
             Duration = 3600,
             VaryByHeader = "Accept"
        });
});

Gotchas

The response caching middleware stops working in a variety of situations which you can learn more about in the docs. Two common ones you will probably hit are that it stops working if it sees an Authorization or Set-Cookie HTTP header.

Bonus Comment

In ASP.NET 4.6, we could represent cache profiles in the web.config and change the settings without recompiling the code. For more information about how you can move your cache profiles to the new appsettings.json, rather than hard coding it in Startup.cs see this question.

Muhammad Rehan Saeed
  • 35,627
  • 39
  • 202
  • 311
  • 1
    Are you sure this is the same as OutputCache? ResponseCache seems to affect HTTP headers, but it doesn't actually cache anything server-side – Matthew Groves Aug 30 '17 at 23:18
  • 1
    It does if you use the `UseResponseCaching()` middleware. That said, there are certain restrictions on it at the moment. One being that it stops caching when it sees an `Authorization` HTTP header. More info in the ASP.NET Docs. – Muhammad Rehan Saeed Aug 31 '17 at 07:50
7

Update
As AndersNS was kind to point out, it will be available in RC1 most likely: https://github.com/aspnet/Mvc/issues/536.

To put it simply there's no OutputCache or equivalent in ASP.NET 5 currently.

However, please note that OutputCache is just an attribute with minimal logic that talks to a cache provider. You can easily implement your own such attribute, using Memory Cache for example. Or you can use third party solutions.

I am sure that when ASP.NET 5 will ship there will be plenty of solutions out on the market. And I'm quite sure that we will have an official OutputCache equivalent too.

Here's the basic MemoryCache usage in case someone finds it useful

MemoryCache cache = MemoryCache.Default;
string cacheName = "MyCache";

if (cache.Contains(cacheName) == false || cache[cacheName] == null)
{
    var data = ... get data

    cache.Set(cacheName, data, new CacheItemPolicy() { SlidingExpiration = DateTime.Now.AddDays(1).TimeOfDay });
}

return cache[cacheName];
Mihai Dinculescu
  • 19,743
  • 8
  • 55
  • 70
  • 2
    https://github.com/aspnet/Mvc/issues/536 Also see the discussion in this issue, should be in for RC1 of mvc6 – AndersNS Dec 05 '14 at 09:49
  • 2
    Thank you for the info! I'll keep my eye out for it in RC1. –  Dec 05 '14 at 14:00