Budda
Budda

Reputation: 18353

Can null be inserted into Cache?

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?

Upvotes: 2

Views: 3327

Answers (2)

Gonzalo
Gonzalo

Reputation: 21175

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

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

Upvotes: 5

blowdart
blowdart

Reputation: 56540

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?

Upvotes: 1

Related Questions