fast-cache icon indicating copy to clipboard operation
fast-cache copied to clipboard

Question: is FastCache saving null values for a given key?

Open guillaumeagile opened this issue 1 year ago • 1 comments

or do I have to store a NullObject (using the Null Object pattern) ?

guillaumeagile avatar Jul 04 '24 09:07 guillaumeagile

Hi Guillaume,

There is where K : notnull constraint on keys. If you are not seeing a compiler error/warning when being passed a nullable reference type, please make sure you have <Nullable>enabled</Nullable> in your project file and optionally <WarningsAsErrors>nullable<WarningsAsErrors> for the best experience.

If you need a key value that represents None, you can wrap the key into a struct which can internally have a nullable reference/value representing that:

var key = new OptionalKey(null);
Cached<int>.Save(key, 42, TimeSpan.FromMinutes(10));

readonly record struct OptionalKey(Key? Value);

neon-sunset avatar Jul 04 '24 17:07 neon-sunset