【问题标题】:ASP.NET Bundling caching - where and how long?ASP.NET 捆绑缓存 - 在哪里以及多长时间?
【发布时间】:2013-04-10 14:13:15
【问题描述】:

在使用新的捆绑和缩小功能对 ASP.NET 应用程序中的性能问题进行故障排除时,我注意到有相当多的文件活动访问捆绑中使用的 javascript 文件。

在对一个干净的 MVC 应用程序进行一些测试后,我注意到在第一个请求之后,我希望它读取文件以构建捆绑包,但它没有在后续请求中读取文件大约分钟左右。然后它会再次读取它们,然后再安静一分钟左右。

显然这里有某种缓存,但是捆绑内容在哪里被缓存以及缓存了多长时间?我可以通过配置来控制这个时间吗?

【问题讨论】:

    标签: asp.net bundling-and-minification


    【解决方案1】:

    响应被缓存在 HttpContext.Cache 中,通过调用 Insert 并针对 VirtualPathProvider 设置 CacheDepedency 以及用于生成包的文件。

    /// <summary>
    /// Stores the response for the bundle in the cache, also sets up cache depedencies for the virtual files
    /// used for the response
    /// </summary>
    public void Put(BundleContext context, Bundle bundle, BundleResponse response) {
        List<string> paths = new List<string>();
        paths.AddRange(response.Files.Select(f => f.VirtualFile.VirtualPath));
        paths.AddRange(context.CacheDependencyDirectories);
        string cacheKey = bundle.GetCacheKey(context);
        CacheDependency dep = context.VirtualPathProvider.GetCacheDependency(context.BundleVirtualPath, paths, DateTime.UtcNow);
        context.HttpContext.Cache.Insert(cacheKey, response, dep);
        bundle.CacheKeys.Add(cacheKey);
    }
    

    【讨论】:

      【解决方案2】:

      原来我使用的是整个 MVC4 堆栈的预发布版本,包括 System.Web.Optimization。升级到 RTM 解决了这个问题。

      【讨论】:

        猜你喜欢
        • 2014-03-29
        • 1970-01-01
        • 2011-11-19
        • 2013-07-13
        • 2015-02-16
        • 2011-12-26
        • 2020-05-02
        • 2019-12-16
        • 1970-01-01
        相关资源
        最近更新 更多