可空插入缓存? [英] Can null be inserted into Cache?
问题描述
我积极参与我的ASP.NET网站上使用缓存。
I actively use caching in my ASP.NET web site.
不过,从数据库请求某些对象真的是不存在的。举例来说,我检查,如果有一个折扣
通过寻找纪录 =产品编号@产品编号
特定产品在 ProductsDiscount
表。记录缺位意味着没有折扣。
Though, some objects requested from db are really absent. For instance, I'm checking if there is a Discount
for the particular product by looking record with ProductId=@ProductId
in ProductsDiscount
table. Absence of the record means no discount.
你觉得这是一个好主意,把空
优惠对象到缓存?
Do you think it is a good idea to put null
discount objects into Cache?
或者,我会更好地创造更好的东西(使用空对象模式,例如)。其实,我真的不喜欢主意,开始使用空对象模式,因为这将需要大量的重新设计的,我想避免,至少现在。
Or I would better invent something better (using null-object pattern, for instance). In fact, I don't really like idea to start using null-object pattern as it will require a lot of redesign that I would like to avoid at least right now.
感谢。任何想法都欢迎。
Thanks. Any thoughts are welcome.
P.S。事实上,我甚至不能把空物体进入高速缓存,当我尝试拨打:
P.S. In fact, I can't even put null object into Cache, when I try to call:
HttpContext.Current.Cache.Insert("name...", null);
我收到:
值不能为空。
P.P.S。为什么MSDN不能说明这种行为?
P.P.S. Why MSDN tells nothing about this behavior?
推荐答案
有关你的情况,你可以使用 DBNull.Value
作为无数据标记:
For your case, you could use DBNull.Value
as the 'no data' marker:
HttpContext.Current.Cache.Insert("name...", DBNull.Value);
这篇关于可空插入缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!