ASP.NET MVC - 自定义缓存Busting密钥

时间:2016-08-01 16:29:27

标签: c# asp.net asp.net-mvc caching bundling-and-minification

据说,当修改包中包含的文件时,应该更新url键,强制客户端清除其缓存。

但是,我发现这是一个非常不可靠的过程 - 由于某种原因,即使对底层文件进行了很多更改,URL密钥也不会一直更改。

所以我想要做的是在查询字符串中使用汇编版本,这样每当发布一个版本时,所有客户端都会清除它们的缓存,以便它们更新到最新版本。以下是我到目前为止的情况:

自定义转换以修改查询变量:

public class VersionBusterTransform : IBundleTransform
{
    public void Process(BundleContext context, BundleResponse Response)
    {           
        string query = string.Format("{0}.{1}.{2}.{3}", Global.Properties.Version.Major, Global.Properties.Version.Minor, Global.Properties.Version.Release, Global.Properties.Version.Build);
        Array.ForEach(Response.Files.ToArray(), x => x.IncludedVirtualPath = string.Concat(x.IncludedVirtualPath, "?v=", query));
    }
}

注册文件:

 BundleTable.EnableOptimizations = (Global.Properties.Deployment.Environment != DeploymentEnvironment.Development);
 var StyleLibrary = new StyleBundle(ConfigBundles.Styles);
 StyleLibrary.Include("~/Content/Styles/Libraries/core-{version}.css", new CssRewriteUrlTransform());
 StyleLibrary.Include("~/Content/Styles/Libraries/icomoon/style.css", new CssRewriteUrlTransform());
 StyleLibrary.Include("~/Content/Styles/Site.css", new CssRewriteUrlTransform());
 StyleLibrary.Transforms.Add(new VersionBusterTransform());
 BundleTable.Bundles.Add(StyleLibrary);

 var ScriptLibrary = new ScriptBundle(ConfigBundles.Scripts);
 ScriptLibrary.Include("~/Content/Scripts/Libraries/modernizr-{version}.js");
 ScriptLibrary.Include("~/Content/Scripts/Libraries/bootstrap-{version}.js");
 ScriptLibrary.Include("~/Content/Scripts/Framework/yeack.js");
 ScriptLibrary.Transforms.Add(new JsMinify());
 ScriptLibrary.Transforms.Add(new VersionBusterTransform());
 BundleTable.Bundles.Add(ScriptLibrary);

获取网址的方法:

public static string Query(string Path)
{
    if (HttpRuntime.Cache[Path] == null)
    {
        var absolutePath = HostingEnvironment.MapPath(Path);
            HttpRuntime.Cache.Insert(Path, string.Format("{0}?v={1}.{2}.{3}.{4}", Path, Global.Properties.Version.Major, Global.Properties.Version.Minor, Global.Properties.Version.Release, Global.Properties.Version.Build), new CacheDependency(absolutePath));
    }
    return HttpRuntime.Cache[Path] as string;
}

标题模板:

@Styles.Render(ConfigBundles.Styles)
@Scripts.Render(ConfigBundles.Scripts)

目前,当我在开发环境中打开网站时,标题中会打印以下引用:

<link href="/Content/Styles/Libraries/core-3.1.0.css?v=0.3.5.0" rel="stylesheet">
<link href="/Content/Styles/Libraries/icomoon/style.css?v=0.3.5.0" rel="stylesheet">
<link href="/Content/Styles/Site.css?v=0.3.5.0" rel="stylesheet">

<script src="/Content/Scripts/Libraries/modernizr-2.6.2.js?v=0.3.5.0"></script>
<script src="/Content/Scripts/Libraries/bootstrap-3.0.0.js?v=0.3.5.0"></script>
<script src="/Content/Scripts/Framework/yeack.js?v=0.3.5.0"></script>

然而,在制作中,正在打印这个:

<link href="/Content/Styles/css?v=SmKL_qNLRzByCaBc0zE--HPqJmwlxxsS9p8GL7jtFsc1" rel="stylesheet">
<script src="/Content/Scripts/js?v=YvUa47U8N_htaVmoq5u1VzHyRgEH3quFSUYpjRonpbM1"></script>

为什么我的捆绑转换对生产中打印的内容没有任何影响?

0 个答案:

没有答案