HttpContext.Cache 详解

提到HttpContext.Cache必然会想到Application,他们有什么共性和不同点呢,我们一一道来

  • 相同点:

1、两者都是使用键值对来存储对象

2、两者都是应用程序同生命周期(在cache不设置缓存策略的情况下)

  • 不同点

1、锁的方式:application采用lock,unlock来控制脏操作

                    Cache内部自动完成锁定高效率

我们做一个最简单的比较,测试读取999999次,这只是单线程的

 

  public void ApplicationTime()

        {

            for (int i = 0; i < 999999; i++)

            {

                var s = HttpContext.Application["Name"];

            }

        }



        public void CacheTime()

        {



            for (int i = 0; i < 999999; i++)

            {

                var s = HttpContext.Cache["Name"];

            }

        }





        //

        // GET: /Home/

        public ActionResult Index()

        {

            

            HttpContext.Application["Name"] = "张三";

            HttpContext.Cache["Name"] = "张三";

            Stopwatch swApplication= new Stopwatch();

            swApplication.Start();

            ApplicationTime();

            swApplication.Start();

           var applicationLong= swApplication.ElapsedMilliseconds;



            Stopwatch swCache = new Stopwatch();

            swCache.Start();

            CacheTime();

            swCache.Start();

            var cacheLong = swCache.ElapsedMilliseconds;

 applicationLong:1373  cacheLong:925

单线程无法说明太多的问题,我们来看一个多线程的

   public  List<Thread> applicationThreads = new List<Thread>();

       public List<Thread> cacheThreads = new List<Thread>();



        public void ApplicationTime()

        {

            for (int i = 0; i < 19999; i++)

            {

                var s = HttpContext.Application["Name"];

            }

            applicationThreads.Remove(Thread.CurrentThread);

        }



        public void CacheTime()

        {

            for (int i = 0; i < 19999; i++)

            {

                var s = HttpContext.Cache["Name"];

            }

            cacheThreads.Remove(Thread.CurrentThread);

        }



        public void CachePool()

        {

            Stopwatch  stopwatch=new Stopwatch();

            stopwatch.Start();

            while (cacheThreads.Count > 0)

            {



            }

            stopwatch.Stop();

            var time = stopwatch.ElapsedMilliseconds;

        }



        public void ApplictionPool()

        {



            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            while (applicationThreads.Count > 0)

            {



            }

            stopwatch.Stop();

            var time = stopwatch.ElapsedMilliseconds;

        }





        //

        // GET: /Home/

        public ActionResult Index()

        {

            HttpContext.Application["Name"] = "张三";

            HttpContext.Cache["Name"] = "张三";

            for (int i = 0; i < 100; i++)

            {

                Thread threadApplication = new Thread(ApplicationTime);

                threadApplication.Name = "Application" + i;

               applicationThreads.Add(threadApplication);

                threadApplication.Start();

            }



            Thread applicationPool = new Thread(ApplictionPool);

            applicationPool.Start();



            for (int i = 0; i < 100; i++)

            {

                Thread threadCache = new Thread(CacheTime);

                threadCache.Name = "Cache" + i;

                cacheThreads.Add(threadCache);

                threadCache.Start();

            }



            Thread cachePool = new Thread(CachePool);

            cachePool.Start();



            return View();

        }

 我起了100个线程,每个线程读取19999次缓存,Application:37,Cache:8;如果读取次数增大,差距就更大

2、可以设置不同的缓存依赖策略

 string path = Server.MapPath("/Test/Power.xml");

            CacheDependency  cacheDependency=new CacheDependency(path);

            HttpContext.Cache.Insert("power", "权限", cacheDependency);

Power.xml文件变化,缓存就失效了!

 

 

 

你可能感兴趣的:(context)