Skip to content

Why it is not possible to add null items in the cache via IAppCache.Add ? #155

@EnricoMassone

Description

@EnricoMassone

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 ?

class Program
{
    static void Main(string[] args)
    {
      var options = new MemoryCacheOptions();
      var cache = new MemoryCache(options);

      cache.Set<string>("the-key", null, DateTimeOffset.UtcNow.AddDays(1));

      var item = cache.Get<string>("the-key");
      var otherItem = cache.Get<string>("other-key");

      // both item and otherItem are null references, but item has been retrieved from the cache
    }
}

Thanks for the clarification.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions