Can null be inserted into Cache?

风流意气都作罢 提交于 2019-12-24 00:06:56

问题


I actively use caching in my ASP.NET web site.

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. In fact, I can't even put null object into Cache, when I try to call:

HttpContext.Current.Cache.Insert("name...", null);

i receive:

Value cannot be null.

P.P.S. Why MSDN tells nothing about this behavior?


回答1:


For your case, you could use DBNull.Value as the 'no data' marker:

HttpContext.Current.Cache.Insert("name...", DBNull.Value);



回答2:


No you can't put a null in the cache, what would be the point? If the discount is a numeric value then the lack of a discount (null in your table) really indicates a discount of 0% - so why not store a 0?



来源:https://stackoverflow.com/questions/10378161/can-null-be-inserted-into-cache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!