Can you pre-cache ASP.NET Bundles?

亡梦爱人 提交于 2019-12-20 12:33:29

问题


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.

Is there a way to pre-cache bundles? After all, the files are static at compile time.


回答1:


Solution

To fix we replaced the default memory cache with caching that persisted beyond the App Pool life.

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
    }
}

Complications

The only other wrench worth noting had to do with our persistent caching tool. In order to cache we had to have a serializable object. Unfortunately, BundleResponse is not marked as Serializable.

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.



来源:https://stackoverflow.com/questions/17663030/can-you-pre-cache-asp-net-bundles

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