-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why it is not possible to add null items in the cache via IAppCache.Add ? #155
Comments
Exactly as you describe - if you allow someone to cache If you really want to do weird things with null then you should cache a |
Thanks for the clarification, now your design decision is clear to me. Sometimes caching null items can be useful. I know that using nulls as return values from methods is quite controversial, but in real-world code bases there are plenty of methods such as the following one:
Maybe you want to cache the result of calling Maybe there is an alternative. The IAppCache interface defines the following method:
By using this method an item is fetched from the cache by key and the bool value returned indicates whether the item was available in the cache. In this case the semantic is clearer than the one in the So a possible change can be allowing to store null values via The main point for this idea is that caching null values is disallowed when using To summarize it's a tradeoff between having a safer API which tries to avoid working with |
Good point about being able to cache a null with the GetOrAdd methods, that is also inconsistent. As you say managing nulls is always imperfect in c#due to the language design although recent null annotations do make it better. Part of me wonders if it's better to give up and let developers just cache a null if they really want to, and remove the null check in Add. What do you think? I have to admit I never use the Get, TryGet or the Add methods, I only ever use the GetOrAdd flavour since I want my operation to only execute once which is the main benefit of the package, so that's probably why I haven't run into it before. |
Unfortunately the whole concept of null reference is deeply rooted into the language. I have seen various examples of functional libraries attempting (among the other things) to eradicate the null reference from the language, but all of them add a lot of burden. I tend to prefer simplicity. I wrote myself a simple wrapper class attempting to remove the usage of null as a way to express the absence of a result. But again this adds burden: to be honest after migrating to .NET core 3.1 I prefer using nullable reference types instead of my own wrapper. To summarize, I would allow null values to be cached via You can also consider a couple of changes to
All of these is a breaking change of the public API, so I think it goes to the roadmap for the next major release. Maybe I can help with a pull request if you think that all of this makes sense. |
Yeah that all makes a lot of sense to me and would tidy things up - I would accept a PR based on those changes with supporting unit tests. Although technically breaking API changes, they are all actually very minor breaking changes - I doubt any of them would break users apps, and so I am not even sure they would warrant a major version. |
Hello, I have manged to fork the repo. I'll produce a single pull request containing all of the changes, is it fine for you ? |
Hi @alastairtree did you take a chance to evaluate my pull request ? |
Hi any chance this can get some more priority? Just want to re-iterate NULLs are not going anywhere in C# as a representation for the absence of data. The new nullability features in newer c# versions are geared towards more null safety, not for any alteration in their meaning or frequency of use. They only ask for more consideration as to whether you really want to use NULL or should expect NULL at any given point. |
Hi, what is the status of this feature? I see there is a PR for almost three years now but no updates of its status. |
Can you please explain why adding a null item to the cache via
IAppCache.Add
is disallowed ?I'm asking because the underlying ASP.NET core memory cache allows to add null items to the cache via
IMemoryCache.Set
.Is this design choice intended to avoid a situation like the following one, where the semantic of the null reference is somewhat ambiguous ?
Thanks for the clarification.
The text was updated successfully, but these errors were encountered: