zoukankan      html  css  js  c++  java
  • Asp.net中全局缓存的几种方式

    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则在内存的使用效率上和功能上(例如缓存依赖、过期策略等)都有所增强,所以应该在实际应用中尽量使用它。

  • 相关阅读:
    python基础(三)python数据类型
    python基础(二)条件判断、循环、格式化输出
    postman测试上传文件
    postman添加权限验证
    postman添加cookie
    postman发送json格式的post请求
    postman发送post请求
    如果json中的key需要首字母大写怎么解决?
    fastjson转jackson
    git初识
  • 原文地址:https://www.cnblogs.com/lonlywaiting/p/4185998.html
Copyright © 2011-2022 走看看