Skip to content

Revisit MemoryCache design #36556

Closed as not planned
Closed as not planned

Description

@JunTaoLuo

The current design of MemoryCache is awkward due to the IMemoryCache interface where ICacheEntry CreateEntry(object key) exists instead of a more intuitive Set(object key, object value). The expected usage pattern is to call Dispose() on the ICacheEntry to "commit" the entry to the cache but this is unintuitive as well. This proved to be an issue when attempting to implement a strategy based eviction trigger and strategy in aspnet/Caching#280. We should revisit this design and see if there are possible improvements.

cc @davidfowl @DamianEdwards

Activity

  1. muratg commented on Jan 3, 2018

    @muratg

    Putting this tentatively in 2.2.0.

  2. muratg commented on Aug 31, 2018

    @muratg

    This is not currently in scope. We'll revisit in a future release.

    cc @DamianEdwards

  3. analogrelay commented on May 15, 2020

    @analogrelay
    Contributor

    The fact that you have to CreateEntry and then Dispose the returned value to add it to the cache has been very non-intuitive to users. It'd be nice to take a look at a better API here.

  4. added this to the Future milestone on May 15, 2020
  5. removed
    untriagedNew issue has not been triaged by the area owner
    on Jul 1, 2020
  6. maryamariyan commented on Sep 30, 2020

    @maryamariyan
    Contributor

    @JunTaoLuo is this still needed? (Referring to aspnet/Caching#280 (comment))

    Also since we have a Set extension method here:

    public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value)
    {
    using ICacheEntry entry = cache.CreateEntry(key);
    entry.Value = value;
    return value;
    }

  7. JunTaoLuo commented on Oct 13, 2020

    @JunTaoLuo
    ContributorAuthor

    The point of the issue is that the pattern of using CreateEntry and then calling Dispose to add the entry to the cache is unintuitive. We do have more convenient helpers like Set so that most people don't need to directly use that pattern when interacting with a cache. However, the underlying issue of the unintuitive API remains. That said, given that there hasn't been much traffic on this issue, maybe it's not a problem for most users, in which case we can either backlog or close this issue.

  8. adamsitnik commented on Nov 15, 2023

    @adamsitnik
    Member

    The current design is far from perfect. I agree that we need a re-design. Pplease follow #48567 which includes your proposal and some other good ideas. Thank you!

  9. locked and limited conversation to collaborators on Dec 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions