问题
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