How do I apply the OutputCache attribute on a method in a vNext project?

前端 未结 2 764
情深已故
情深已故 2020-12-10 04:40

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

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


        
2条回答
  •  隐瞒了意图╮
    2020-12-10 05:01

    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];
    

提交回复
热议问题