Asp.net中全局缓存的几种方式

本文介绍了ASP.NET中几种常见的缓存实现方式,包括HttpContext.Current.Application、HttpRuntime.Cache及静态变量方式,并对比了它们的特点与适用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class StaticCacheTest

    {

        private static IDictionary<string, object> _dic; 

        private static object locker = new object(); 

        

        private static IDictionary<string, object> CachedDic

        {

            get

            {

                if (_dic == null)

                {

                    lock (locker)

                    {

                        if (_dic == null)

                        {

                            _dic = new Dictionary<string, object>();

                        }

                    }

                }



                return _dic;

            }

        }



        public static object GetObject(string key)

        {

            return CachedDic[key];

        }



        public static void SetObject(string key, object obj)

        {

            lock (locker)

            {

                CachedDic[key] = obj;

            }

        }

    }



    public partial class _Default : System.Web.UI.Page

    {

        private const string KEY = "CurrentTime";



        protected void Page_Load(object sender, EventArgs e)

        {

            String curTime = System.DateTime.Now.ToString();

            if (HttpContext.Current.Application[KEY] == null)

            {

                HttpContext.Current.Application.Lock();
                HttpContext.Current.Application[KEY] = curTime;
                HttpContext.Current.Application.UnLock();
            }

            if (HttpContext.Current.Cache[KEY] == null)

            {

                HttpContext.Current.Cache.Insert(KEY, curTime);

            }

            if (StaticCacheTest.GetObject(KEY) == null)   //本质上就是HttpRuntime.Cache的实现方式

            {

                StaticCacheTest.SetObject(KEY, curTime);

            }

            if (HttpRuntime.Cache[KEY] == null)

            {

                HttpRuntime.Cache.Insert(KEY, curTime);   //HttpRuntime.Cache与HttpContext.Current.Cache是同一对象,建议使用HttpRuntime.Cache                

            }



            TextBox1.Text = HttpContext.Current.Application[KEY].ToString();

            TextBox2.Text=HttpContext.Current.Cache[KEY].ToString();

            TextBox3.Text=StaticCacheTest.GetObject(KEY).ToString();

        }

    }
  1. HttpContext.Current.Application:整个应用程序都可以共享的,当然存储的时候应该加锁的。
  2.  HttpRuntime.Cache与HttpContext.Current.Cache:二者其实是指向的同一对象,区别在于HttpContext与HttpRuntime的实现上。HttpContext必需是Asp.net的上下文中使用,而HttpRuntime则可以在任何上下文中使用,例如可以在控制台程序中利用HttpRuntime作为缓存。
  3. 静态变量的方式:本质上与HttpRuntime.Cache的实现原理是一样的(Cache的也是静态变量)。

    第1与第3只适合存储少量数据(小于1M),否则性能上会有损失。而HttpRuntime.Cache则在内存的使用效率上和功能上(例如缓存依赖、过期策略等)都有所增强,所以应该在实际应用中尽量使用它。

转载于:https://www.cnblogs.com/lonlywaiting/p/4185998.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值