Skip to content

MemoryCache Docs Missing Details #7323

@tomwarner13

Description

@tomwarner13

As far as I can tell, the only public-facing documentation provided by Microsoft about Microsoft.Extensions.Caching.Memory is this article. I have several questions that are not covered in this article, and it would be nice to have canonical answers there (or even anywhere!).

When does an expired item get removed?

My best understanding of the implementation is that there are two reasons (other than explicit removal) that an item might get removed from the cache:

  • Expiration Scan: there's apparently a MemoryCacheOptions.ExpirationScanFrequency that configures the minimum interval between these scans. The memory cache docs mention that any activity on the cache can "trigger a background scan" though they do not mention this option at all. What does this scan do? Does it remove all expired items, or just some arbitrary portion of them? Is it an expensive operation? Can it block execution of the call to the MemoryCache method which triggered it? Are there any considerations around performance if we set this frequency higher or lower?
  • Compact(): Good news: the docs do explain, very briefly, what calling compact actually does. Bad news: they also don't mention whether it's a blocking operation, any performance considerations around calling it, etc.

Also, how does .Compact() interact with a set size? If it is called on a cache with a set size, will it remove based on a percentage of the total size limit, the current size of the cache, or as a percentage of the total number of entries in the cache (disregarding size)? If it is called on a cache without a set size, does it do anything? Does it use the total number of entries in its percentage calculation? Are there any callbacks available to respond if an entry exceeded the size limit?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-aiMicrosoft.Extensions.AI librariesuntriaged

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions