ASP.NET 捆绑缓存 - 在哪里以及多长时间?

标签 asp.net bundling-and-minification

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

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

显然这里进行了某种缓存,但是包内容被缓存在哪里以及缓存多长时间?我可以通过配置控制该时间量吗?

最佳答案

响应缓存在 HttpContext.Cache 中,通过使用用于生成包的文件对 VirtualPathProvider 调用带有 CacheDepedency 设置的 Insert。

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

关于ASP.NET 捆绑缓存 - 在哪里以及多长时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15928336/

相关文章:

mysql - asp.net 数据库仅返回一个值而不是整个集合

asp.net - ASP.NET 4 的 IIS 和服务器操作系统要求

c# - multiple 不是元素 fileupload 的有效属性

ASP.NET MVC(二)log4net日志记录模式

asp.net - 从 ASP.NET 调用 WCF 服务时身份验证失败

asp.net - MVC4 捆绑和压缩与 CDN

asp.net-mvc - 布局页面上呈现脚本的奇怪错误

javascript - 防止规避 ASP.NET 缩小

javascript - 通过 CDN 或 minifiy/concat 加载资源并为自己服务?

javascript - VS2013 - Web Essentials - "Update All Bundles"不工作