经常访问的表数据存入内存的代码示例,此代码只有在第一次使用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;
            }
        }