惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
Engineering at Meta
Engineering at Meta
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 聂微东
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
T
Tailwind CSS Blog
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
爱范儿
爱范儿
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
Jina AI
Jina AI
博客园 - 叶小钗
雷峰网
雷峰网
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - Franky
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
阮一峰的网络日志
阮一峰的网络日志
量子位

博客园 - 吴东雷

配置NuGet自动下载丢失的Dll SharePoint2010站点在IE下的操作异常 ExtJS的store.sync向Asp.net MVC的Action提交时引发System.Reflection.AmbiguousMatchException异常 ReportViewerWebWebpart显示报表时显示的脚本错误:Uncaught Sys.ArgumentNullException: Sys.ArgumentNullException: 值不能为 null。参数名: panelsCreated[0] 【转载】javascript getComputedStyle,getPropertyValue,CurrentStyle说明 【原创】关于SPSecurity.RunWithElevatedPrivileges的一个问题[A problem about SPSecurity.RunWithElevatedPrivileges] 【原创】如何让SharePoint2010的内联代码支持.Net framework 3.5[How to embed inline code in aspx with .net framework 3.5 syntax] 【半原创】SharePoint CAML 查询生成器[SharePoint 2010 CAML Query Builder] 【原创】在DataFormWebPart中将列表附件显示为图片(二)[How to display list item attachments as image in DFWP Part 2] 【原创】如何在DataFormWebPart中嵌入自定义控件[How to embeded custom control in DFWP] 【原创】SharePoint2010的SaveButton如何实现在跳转前给用户以提示[How to implement SaveButton display an alert when Redirect action] 【原创】在DataFormWebPart中将列表附件显示为图片(一)[How to display list item attachments as image in DFWP Part1] 修改Model中hasMany中自动生成的属性值 ExtJS中自定义组件并将事件暴露给调用者 从控制台测试SharePoint遇到的两个问题 The Sencha Class System(理解ExtJs定义类的过程) IIS7中Sites(站点), Applications(应用程序), Virtual Directories(虚拟目录)的粗浅理解 按钮的ajax请求时,一次点击两次提交的问题 MVC3中Razor模板引擎如何修改View的基类
Asp.net中全局缓存的几种方式
吴东雷 · 2012-07-15 · via 博客园 - 吴东雷
    

public class StaticCacheTest
    {
        private static IDictionary<stringobject> _dic; 
        private static object locker = new object(); 
        
        private static IDictionary<stringobject> CachedDic
        {
            get
            {
                if (_dic == null)
                {
                    lock (locker)
                    {
                        if (_dic == null)
                        {
                            _dic = new Dictionary<stringobject>();
                        }
                    }
                }

                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();
        }
    }

两年多没有搞Asp.net了,对于全局存储一些东西的机制居然也都搞不清了,今天特意做个测试验证一下,做个记录供自己以后参考,记录如下: 

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

一点说明:为什么在标题中要嵌入英文?原因是为了能够让国外的网友能查询到这篇文章。平常在Google上查资料的时候,经常参考国外网友的博客,帮助我解决了很多问题,所以我也想让他们能够参考我写的内容。当然文中我不可能全部译为英文,所以我尽量把代码粘全,靠代码说话吧。