加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

ASP.NET MVC捆绑缓存. (检测css文件更改)(内部行为)

发布时间:2020-12-15 23:15:57 所属栏目:asp.Net 来源:网络整理
导读:我一直潜入ASP.NET MVC内部功能(不同的原因),但仍然无法涵盖所有??的行为.其中一个我不是subj. 它的工作原理如下: 如果我捆绑一些文件(例如css文件),框架会检测到这些更改,并为新的软件包生成新的ID(使浏览器可以轻松刷新更改),如href =“/ Content / css?
我一直潜入ASP.NET MVC内部功能(不同的原因),但仍然无法涵盖所有??的行为.其中一个我不是subj.

它的工作原理如下:

如果我捆绑一些文件(例如css文件),框架会检测到这些更改,并为新的软件包生成新的ID(使浏览器可以轻松刷新更改),如href =“/ Content / css?v = qartPE4jGe- l1U0I7kNDZPZzVTdh0kT8VBZZA_uURjI1” .

我实际上想了解什么:

>框架(这可能不是MVC但.NET东西)检测到文件是如何改变的(因为没有目录观察者活动(因为我可以更改文件,即使在Web服务器离线),以查看文件改变生活,并且系统实际上检测到文件内容的变化(我尝试只是重新保存文件而不改变它们的内容,包编号也没有改变))?
(我认为,显然系统无法比较每个文件的内容来检测其每次请求的变化).
>哪里(和如何)框架存储当前的捆绑ID,以及它如何存储以前的版本(以前的捆绑包仍然可用,当他们的urls)?

非常感谢!

解决方法

ASP.NET Optimization框架缓存HttpContext.Cache中的bundle响应,并使用 CacheDependency监控bundle中的每个文件以进行更改.这就是为什么更新文件直接使缓存失效并重新生成捆绑包.

捆绑文件名是捆绑内容的哈希值,用于确保任何捆绑文件被修改时URL改变.软件包的虚拟路径用作缓存密钥.

图书馆的相关代码(注意这个稍微过时,但我认为逻辑仍然是一样的):

internal BundleResponse GetBundleResponse(BundleContext context)
{
    // check to see if the bundle response is in the cache
    BundleResponse bundleResponse = Bundle.CacheLookup(context);
    if (bundleResponse == null || context.EnableInstrumentation)
    {
        // if not,generate the bundle response and cache it
        bundleResponse = this.GenerateBundleResponse(context);
        if (context.UseServerCache)
        {
            this.UpdateCache(context,bundleResponse);
        }
    }
    return bundleResponse;
}

private void UpdateCache(BundleContext context,BundleResponse response)
{
    if (context.UseServerCache)
    {
        // create a list of all the file paths in the bundle
            List<string> list = new List<string>();
        list.AddRange(
            from f in response.Files
            select f.FullName);
        list.AddRange(context.CacheDependencyDirectories);
        string cacheKey = Bundle.GetCacheKey(context.BundleVirtualPath);
        // insert the response into the cache with a cache dependency that monitors
        // the bundle files for changes
        context.HttpContext.Cache.Insert(cacheKey,response,new CacheDependency(list.ToArray()));
        context.HttpContext.Response.AddCacheItemDependency(cacheKey);
        this._cacheKeys.Add(cacheKey);
    }
}

最后,对于旧的bundle URL,我想你会发现它们是从浏览器缓存返回的,或者实际上返回最新版本的捆绑包,因为捆绑路径没有改变,只有版本查询字符串.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读