zoukankan      html  css  js  c++  java
  • 实现asp.net mvc页面二级缓存,提高访问性能


    实现的mvc二级缓存的类

      //Asp.Net MVC视图页面二级缓存
        public class TwoLevelViewCache : IViewLocationCache
        {
            private readonly static object SKey = new object();
            private readonly IViewLocationCache _cache;
    
            public TwoLevelViewCache(IViewLocationCache cache)
            {
                _cache = cache;
            }
    
            private static IDictionary<string, string> GetRequestCache(HttpContextBase httpContext)
            {
                var d = httpContext.Items[SKey] as IDictionary<string, string>;
                if (d == null)
                {
                    d = new Dictionary<string, string>();
                    httpContext.Items[SKey] = d;
                }
                return d;
            }
    
            public string GetViewLocation(HttpContextBase httpContext, string key)
            {
                var d = GetRequestCache(httpContext);
                string location;
                if (!d.TryGetValue(key, out location))
                {
                    location = _cache.GetViewLocation(httpContext, key);
                    d[key] = location;
                }
                return location;
            }
    
            public void InsertViewLocation(HttpContextBase httpContext, string key, string virtualPath)
            {
                _cache.InsertViewLocation(httpContext, key, virtualPath);
            }
    
        }

    在Global.asax.cs类中Application_Start()方法调用缓存方法,实现二级页面缓存

     public class MvcApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                ////其余省略
                ViewEngines.Engines.Clear();//清除aspx和razor模式,阻断查找两种页面模式所消耗的时间
                var ve = new RazorViewEngine();
                ve.ViewLocationCache = new TwoLevelViewCache(ve.ViewLocationCache);//创建二级页面缓存,提高性能
                ViewEngines.Engines.Add(ve);
    
            }
        }
  • 相关阅读:
    简单线程池的设计
    MFC开发--截图工具
    Win32小游戏--蜘蛛纸牌
    C++语言动态创建对象
    C++语言类之间的关系
    c++语言虚函数实现多态的原理(更新版)
    5-24 c++语言之【基础知识】
    小细节--关于printf的输出问题
    Win32小游戏--贪吃蛇
    2020北大夏令营 终末游记
  • 原文地址:https://www.cnblogs.com/david1989/p/3674411.html
Copyright © 2011-2022 走看看