asp.net DataCache缓存

弘思聪
2023-12-01

获取缓存           

string CacheKey = "BrandsModel-" + BrandId;
            object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(BrandId);
                    if (objModel != null)//
                    {
                        int ModelCache = Globals.SafeInt(BLL.SysManage.ConfigSystem.GetValueByCache("ModelCache"), 30);
                        Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);//如果无法通过缓存找到对象则添加进缓存中
                    }
                }
                catch { }
            }
            return (Maticsoft.Model.Shop.Products.BrandInfo)objModel;


清空全部缓存          

IDictionaryEnumerator de = Cache.GetEnumerator();
            ArrayList list = new ArrayList();
            StringBuilder str = new StringBuilder();

            while (de.MoveNext())
            {
                list.Add(de.Key.ToString());
            }
            foreach (string key in list)
            {
                Cache.Remove(key);
            }

可以在web.config中设置缓存过期时间<clientCache cacheControlMaxAge="168.00:00:00" cacheControlMode="UseMaxAge"/>

 类似资料: