解决方案 »

  1.   

    加静态锁,最好用单例模式
            private static volatile pageCache instance = null;
            private static object thislock = new object();//静态锁
            private pageCache()
            {
                cache = SiteCache.GetCacheService();
                db = new DBTool();
            }
            /// <summary>
            /// 单体模式返回当前类的实例
            /// </summary>
            /// <returns></returns>
            public static pageCache GetCacheService()
            {
                if (instance == null)
                {
                    lock (thislock)
                    {
                        if (instance == null)
                        {
                            instance = new pageCache();
                        }
                    }
                }
                return instance;
            }
            /// <summary>
            /// 添除商品信息
            /// </summary>
            public void clearCommodity()
            {
                lock (thislock)
                {
                    cache.RemoveObject(_commodity_cache_path);
                }
            }
            /// <summary>
            /// 获取商品信息
            /// </summary>
            /// <returns></returns>
            public DataTable getCommodity()
            {
                DataTable _list = cache.RetrieveObject(_commodity_cache_path) as DataTable;
                if (_list == null)
                {
                    lock (thislock)
                    {
                        string query = "Select * From v_gl_indexCommodity ";
                        DataTable dt = db.ExecuteTable(query, null);
                        if (dt != null)
                        {
                            _list = dt;
                            cache.AddObject(_commodity_cache_path, _list);
                        }
                    }
                }
                return _list;
            }