Skip to content

Update IDistributedCache to account for nullability #52148

Closed

Description

@JunTaoLuo

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.

Activity

  1. added
    api-suggestion
    on May 1, 2021
  2. added this to the 6.0.0 milestone on May 1, 2021
  3. ghost added
    untriaged
    on May 1, 2021
  4. JunTaoLuo commented on May 1, 2021

    @JunTaoLuo
    ContributorAuthor

    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?

  5. eerhardt commented on May 3, 2021

    @eerhardt
    Member

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

  6. JunTaoLuo commented on May 3, 2021

    @JunTaoLuo
    ContributorAuthor

    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.

  7. JunTaoLuo commented on May 3, 2021

    @JunTaoLuo
    ContributorAuthor

    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.

  8. ericstj commented on May 3, 2021

    @ericstj
    Member

    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.

  9. maryamariyan commented on May 3, 2021

    @maryamariyan
    Contributor

    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

  10. JunTaoLuo commented on May 3, 2021

    @JunTaoLuo
    ContributorAuthor

    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.

  11. ericstj commented on May 3, 2021

    @ericstj
    Member

    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?

  12. removed
    untriaged
    on May 3, 2021
  13. eerhardt commented on Jul 19, 2021

    @eerhardt
    Member

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

  14. modified the milestones: 6.0.0, 7.0.0 on Jul 19, 2021
  15. maryamariyan commented on Dec 23, 2021

    @maryamariyan
    Contributor

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

  16. added
    help wanted
    on Dec 23, 2021
  17. ghost added
    in-pr
    on Jan 19, 2022
  18. ghost removed
    in-pr
    on Feb 7, 2022
  19. ghost locked as resolved and limited conversation to collaborators on Mar 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionarea-Extensions-Cachinghelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions