当前位置:编程学习 > C#/ASP.NET >>

缓存问题?

我现在用System.Web.Caching想实现一个缓存的功能,但我并不是页面下的缓存,而是想在数据访问时实现缓存处理 
即:每次执行数据库查询前, 先判断缓存中有没有数据,如果有直接取缓存中的数据,如果没有则执行数据库查询,并将所获得的结果放到缓存中,这么一个操作
我原本用的
            Cache EnterpriseCodeCache = HttpRuntime.Cache;

            CacheItemRemovedCallback onRemove = null;
            string cid = defEnterpriseEnCod.Cid;
         
            if (EnterpriseCodeCache[cid] != null)
            {
                IList<DefEnterpriseEnCod> DefEnterpriseEnCodList = new List<DefEnterpriseEnCod>();
                DefEnterpriseEnCodList = (IList<DefEnterpriseEnCod>)EnterpriseCodeCache[cid];
                return DefEnterpriseEnCodList;
            }
            else
            {
                 IList<DefEnterpriseEnCod> DefEnterpriseEnCodList =GetData();
                  EnterpriseCodeCache.Add(cid, DefEnterpriseEnCodList, null, DateTime.Now.AddSeconds(60),     Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove);
                 return DefEnterpriseEnCodList
            }
以上是在一个数据库底层方法中用缓存,但现在我在底层的很多方法中都想实现缓存操作,即:返回的数据集也是不同类型的时便报错
System.InvalidCastException: 无法将类型为“System.Collections.Generic.List`1[PTM.Model.DefEnterpriseEnCod]”的对象强制转换为类型“System.Collections.Generic.IList`1[PTM.Model.DefProduct]”。 
请问这一块我到底该怎么实现缓存啊 --------------------编程问答-------------------- ding --------------------编程问答-------------------- 不是实现缓存有问题而是IList(System.Collections) 变为List(System.Collections.Generic.list)的问题

我估计是你的    DefEnterpriseEnCodList = (IList <DefEnterpriseEnCod>)EnterpriseCodeCache[cid]; 
这句话有问题 --------------------编程问答-------------------- System.Collections.Generic.List`1[PTM.Model.DefEnterpriseEnCod]
System.Collections.Generic.IList`1[PTM.Model.DefProduct]

这两种类型是不能转换的哦!!DefProduct实体和DefEnterpriseEnCod实体!在好好看看程序吧 --------------------编程问答-------------------- 我想请问一下
EnterpriseCodeCache[cid]  通另一个方法中的    ProductCache[cid]
如果cid相同会出现错误码? --------------------编程问答-------------------- 我想请问一下如果
System.Web.Caching cache1=httpruntime.cache
System.Web.Caching cache2=httpruntime.cache

cache1[Key]     cache2[Key]
如果两者Key相同会报错吗?
cache1.Add(cid, DefEnterpriseEnCodList, null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove);
cache2.Add(cid, DefProductList, null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove);

然后获取值时
DefEnterpriseEnCodList = (IList <DefEnterpriseEnCod>)cache1[cid]; 
DefProductList= (IList <DefProductList>)cache2[cid]; 
就报类型转换的问题,如上,请问该怎么处理这个问题为好啊?
   --------------------编程问答-------------------- 路过。。。。。。 --------------------编程问答-------------------- System.Collections.Generic.List`1 这个东西是好像是你模型层定义了List<DefEnterpriseEnCod>? 这种形式的 是不是?
我以前碰见这问题好几次 --------------------编程问答-------------------- 这是个类型为DefEnterpriseEnCod的泛型集合 --------------------编程问答--------------------
引用 5 楼 williamwang8187 的回复:
我想请问一下如果
System.Web.Caching cache1=httpruntime.cache
System.Web.Caching cache2=httpruntime.cache

cache1[Key]     cache2[Key]
如果两者Key相同会报错吗?
cache1.Add(cid, DefEnterpriseEnCodList, null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove);
cache2.Add(cid, DefProductList, null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, …


两者KEY相同,
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,