Skip to content

Update IDistributedCache to account for nullability #52148

@JunTaoLuo

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-Extensions-Cachinghelp wanted[up-for-grabs] Good issue for external contributors

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions