Search code examples
azureiisweb-configasp.net-coreoutputcache

Outputcache IIS with MVC and ASP.NET Core in Azure


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.


Solution

  • 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);
        }
    }