2

I know that OutputCache is not ready for ASP.NET Core, but I have read about OutputCache and you can configure it in the web.config like this:

<configuration> 
     <location path="showStockPrice.asp">     
       <system.webserver>        
         <caching>         
           <profiles>
             <add varybyquerystring="*"location="Any"
               duration="00:00:01" policy="CacheForTimePeriod"            
               extension=".asp">
           </profiles>
         </caching>
       </system.webserver>
     </location>
</configuration>

Can I confifure my web.config for using OutputCache Web.Config for MVC routes?

For example:

http://www.example.com/View/Index/123562

Where the varyByParam parameter is 123562.

Thanks.

chemitaxis
  • 13,889
  • 17
  • 74
  • 125

1 Answers1

0

You could use the IMemoryCache class to store results. An example usage from Microsoft can be found here.

Here's a simple example:

public class HomeController : Controller
{
    private readonly IMemoryCache _cache;

    public HomeController(IMemoryCache cache)
    {
        _cache = cache;
    }

    public IActionResult About(string id)
    {
        AboutViewModel viewModel;

        if (!_cache.TryGetValue(Request.Path, out result))
        {
            viewModel= new AboutViewModel();
            _cache.Set(Request.Path, viewModel, new MemoryCacheEntryOptions()
            {
                AbsoluteExpiration = DateTime.Now.AddHours(1)
            });
        }
        return View(viewModel);
    }
}
Will Ray
  • 10,621
  • 3
  • 46
  • 61
  • Thanks will, but I need to cache the output, not a DTO or object ;) – chemitaxis Apr 19 '16 at 16:30
  • 1
    Have you looked at the [ResponseCacheAttribute](https://github.com/aspnet/Mvc/blob/df4b92b1c12dcea122a730b618b33a0b39496561/src/Microsoft.AspNet.Mvc.Core/Filters/ResponseCacheAttribute.cs)? This would at least cache the result on the client side. – Will Ray Apr 19 '16 at 16:49