首页 > 其他 > 详细

数据存储于内存实例

时间:2015-04-10 19:39:00      阅读:261      评论:0      收藏:0      [点我收藏+]

经常访问的表数据存入内存的代码示例,此代码只有在第一次使用ProductList时才会加载数据,

然后一直存储于内存中,想更新缓存数据只有重启网站或IIS且使用该变量时才会更新数据,所以适用于数据更新频率不高的数据存储。

private static readonly object _objProductList = new object();

private static List<ENT_TM_ThreeMProduct> _productList;

        public List<ENT_TM_ThreeMProduct> ProductList
        {
            get
            {
                if (_productList == null)
                {
                    lock (_objProductList)
                    {
                        //防止多个线程并发读取数据时多次访问数据库表
                        if (_productList == null)
                        {
                            using (ProductEntityContext context = new ProductEntityContext())
                            {
                                _productList = context.ENT_TM_ThreeMProduct.ToList();
                            }
                        }
                    }
                }
                return _productList;
            }
        }

数据存储于内存实例

原文:http://www.cnblogs.com/itjeff/p/4415199.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!