MVC 控制器操作中的输出缓存密钥格式 ASP.NET



控制器操作上输出缓存的确切键格式是什么?

[OutputCache(CacheProfile = "Games")]
public virtual ActionResult EventGames(int? id, string slug)

我想不出您需要使用OutputCache键的任何情况。

无论如何,密钥是使用以下因素生成的:
- 键前缀,由 OutputCacheAttribute 类预定义。
- 控制器操作的唯一 ID。
- VaryBy自定义参数。
- VaryByParam 参数。

这些因素将被连接起来,然后使用 SHA256Cng 进行哈希处理

详细的实现可以在这里找到:OutputCacheAttribute.cs

> https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/improving-performance-with-output-caching-cs

在 web.config 缓存部分添加

<caching>
   <outputCacheSettings>
      <outputCacheProfiles>
         <add name="Cache1Hour" duration="3600" varyByParam="none"/>
     </outputCacheProfiles>
  </outputCacheSettings>
</caching>

然后在操作结果上添加缓存配置文件

using System;
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
  public class ProfileController : Controller
  {
    [OutputCache(CacheProfile="Cache1Hour")]
    public string Index()
    {
        return DateTime.Now.ToString("T");
    }
  }
}

最新更新