ASP.NET Bundling caching - where and how long?

六眼飞鱼酱① 提交于 2019-12-10 09:55:29

问题


While troubleshooting a performance issue in an ASP.NET app using the new bundling and minification features, and I noticed quite a bit of file activity accessing the javascript files used in the bundles.

After a bit of testing on a clean MVC app, I noticed that after the first request, where I would expect it to read the files to build up the bundle, it didn't read the files on subsequent requests for about a minute or so. Then it would read them in again, and then go quiet for another minute or so.

Obviously there's some kind of caching going on here, but where are the bundle contents getting cached and for how long? And can I control that amount of time through configuration?


回答1:


The responses are cached inside of the HttpContext.Cache via a call to Insert with a CacheDepedency setup against the VirtualPathProvider with the files that were used to generate the bundle.

/// <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:


Turns out I was using pre-release versions of the entire MVC4 stack, including System.Web.Optimization. Upgrading to RTM resolved the issue.



来源:https://stackoverflow.com/questions/15928336/asp-net-bundling-caching-where-and-how-long

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!