问题描述
每当部署一个MVC Web应用程序我的服务器必须重新缓存所有的JS和CSS包。
Every time I deploy an MVC web application my server has to re-cache all js and css bundles.
由于这个原因它可能需要几秒钟的第一个视图部署后呈现。
Because of this it can take several seconds for the first view to render after deploying.
有没有办法来pre-缓存包?毕竟,这些文件是在编译时静态的。
Is there a way to pre-cache bundles? After all, the files are static at compile time.
推荐答案
要解决,我们更换了默认的内存缓存,缓存是持续超越应用程序池的使用寿命。
Solution
To fix we replaced the default memory cache with caching that persisted beyond the App Pool life.
要做到这一点,我们从 ScriptBundle
继承和压倒 CacheLookup()
和 UpdateCache( )
。
To do this we inherited from ScriptBundle
and overrode CacheLookup()
and UpdateCache()
.
/// <summary>
/// override cache functionality in ScriptBundle to use
/// persistent cache instead of HttpContext.Current.Cache
/// </summary>
public class ScriptBundleUsingPersistentCaching : ScriptBundle
{
public ScriptBundleUsingPersistentCaching(string virtualPath)
: base(virtualPath)
{ }
public ScriptBundleUsingPersistentCaching(string virtualPath, string cdnPath)
: base(virtualPath, cdnPath)
{ }
public override BundleResponse CacheLookup(BundleContext context)
{
//custom cache read
}
public override void UpdateCache(BundleContext context, BundleResponse response)
{
//custom cache save
}
}
并发症
其他唯一的扳手值得注意曾与我们持之以恒的缓存工具来做到。为了缓存,我们必须有一个序列化对象。不幸的是, BundleResponse
没有被标记为序列化
。
我们的解决方案是创建一个小的实用工具类解构 BundleResponse
到它的值类型。一旦我们这样做,我们能够序列化的实用工具类。然后,从缓存中检索时,我们重建 BundleResponse
。
Our solution was to create a small utility class to deconstruct BundleResponse
into its value types. Once we did this we were able to serialize the utility class. Then, when retrieving from the cache, we reconstruct the BundleResponse
.
这篇关于你能pre-缓存ASP.NET捆绑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!