Skip to content
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

Update IDistributedCache to account for nullability #52148

Closed
Tracked by #64015
JunTaoLuo opened this issue May 1, 2021 · 11 comments · Fixed by #64018
Closed
Tracked by #64015

Update IDistributedCache to account for nullability #52148

JunTaoLuo opened this issue May 1, 2021 · 11 comments · Fixed by #64018
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-Extensions-Caching help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@JunTaoLuo
Copy link
Contributor

Background and Motivation

The Get and GetAsync methods of the IDistributedCache interface do not match the intended nullability in terms of return type. As per the API documentation:

/// <returns>The located value or null.</returns>

I noticed this when we were working on adding community contributed APIs, see: dotnet/aspnetcore#32266 (comment).

The request here is to update the API in the IDistributedCache interface.

Proposed API

namespace Microsoft.Extensions.Caching.Distributed
{
  public interface IDistributedCache
  {
-    byte[] Get(string key);
+    byte[]? Get(string key);
-    Task<byte[]> GetAsync(string key, CancellationToken token = default(CancellationToken));
+    Task<byte[]?> GetAsync(string key, CancellationToken token = default(CancellationToken));
  }

Usage Examples

The return type will be update:

- byte[] data = cache.Get(key);
+ byte[]? data = cache.Get(key);

Alternative Designs

The alternative is to keep the current API. This presents a difficulty when adding nullability checks in implementations of IDistributedCache see dotnet/aspnetcore#32266.

Risks

Implementations of the IDistributedCache interface will need to be updated to account for the nullability changes proposed here.

@JunTaoLuo JunTaoLuo added api-suggestion Early API idea and discussion, it is NOT ready for implementation area-Extensions-Caching labels May 1, 2021
@JunTaoLuo JunTaoLuo added this to the 6.0.0 milestone May 1, 2021
@ghost
Copy link

ghost commented May 1, 2021

Tagging subscribers to this area: @eerhardt, @maryamariyan, @michaelgsharp
See info in area-owners.md if you want to be subscribed.

Issue Details

Background and Motivation

The Get and GetAsync methods of the IDistributedCache interface do not match the intended nullability in terms of return type. As per the API documentation:

/// <returns>The located value or null.</returns>

I noticed this when we were working on adding community contributed APIs, see: dotnet/aspnetcore#32266 (comment).

The request here is to update the API in the IDistributedCache interface.

Proposed API

namespace Microsoft.Extensions.Caching.Distributed
{
  public interface IDistributedCache
  {
-    byte[] Get(string key);
+    byte[]? Get(string key);
-    Task<byte[]> GetAsync(string key, CancellationToken token = default(CancellationToken));
+    Task<byte[]?> GetAsync(string key, CancellationToken token = default(CancellationToken));
  }

Usage Examples

The return type will be update:

- byte[] data = cache.Get(key);
+ byte[]? data = cache.Get(key);

Alternative Designs

The alternative is to keep the current API. This presents a difficulty when adding nullability checks in implementations of IDistributedCache see dotnet/aspnetcore#32266.

Risks

Implementations of the IDistributedCache interface will need to be updated to account for the nullability changes proposed here.

Author: JunTaoLuo
Assignees: -
Labels:

api-suggestion, area-Extensions-Caching

Milestone: 6.0.0

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label May 1, 2021
@JunTaoLuo
Copy link
Contributor Author

FYI @ericstj @maryamariyan, it seems like nullability is not enabled for Microsoft.Extensions.Caching.Abstractions package. Although only the IDistributedCache API is blocking the merging of the PR in aspnetcore, maybe it's a good opportunity to review the other APIs in the package and enable nullability for the package?

@eerhardt
Copy link
Member

eerhardt commented May 3, 2021

See #43605. Almost none of the Microsoft.Extensions.* libraries have nullability enabled.

@JunTaoLuo
Copy link
Contributor Author

Is there an estimate of which preview when Caching.Abstractions will be annotated? We can't enable nullability in our package until it's enabled here first.

@JunTaoLuo
Copy link
Contributor Author

FYI, we don't need all of Caching.Abstractions to be updated with nullability enabled. To unblock us, we just need to make the tactical API changes I proposed.

@ericstj
Copy link
Member

ericstj commented May 3, 2021

cc @jeffhandley
Annotating Extensions (as well as other dotnet/runtime packages) isn't committed for 6.0 but we would like to see progress on it, it doesn't look like you're blocked on it, so I'm not seeing a need to raise priority or track as an independent issue. Let me know if I'm misunderstanding.

@maryamariyan
Copy link
Member

maryamariyan commented May 3, 2021

Is there an estimate of which preview when Caching.Abstractions will be annotated?

For Caching.Abstractions, looks like it depends on primitives. So it would be only a matter of enabling nullables for these two libraries

@JunTaoLuo
Copy link
Contributor Author

JunTaoLuo commented May 3, 2021

it doesn't look like you're blocked on it

It is blocking us from enabling nullability in Caching.SqlServer and Caching.StackExchangeRedis as part of dotnet/aspnetcore#5680.

@ericstj
Copy link
Member

ericstj commented May 3, 2021

What's the nullable goal for ASP.NET in 6.0? Are you planning to enable it for all apps? You'll need more than just these assemblies annotated. Aren't there other assemblies that ship in the ASP.NET ref-pack which aren't annotated yet?

@maryamariyan maryamariyan removed the untriaged New issue has not been triaged by the area owner label May 3, 2021
@eerhardt
Copy link
Member

We've reached feature complete for .NET 6. Moving to 7.

@eerhardt eerhardt modified the milestones: 6.0.0, 7.0.0 Jul 19, 2021
@maryamariyan
Copy link
Member

Looking again at #43605, all dependencies to Microsoft.Extensions.Caching.Abstractions are nullable annotated. So would be good to fix this next.

@maryamariyan maryamariyan added the help wanted [up-for-grabs] Good issue for external contributors label Dec 23, 2021
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jan 19, 2022
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Feb 7, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Mar 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-Extensions-Caching help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
4 participants