Skip to content

[Breaking change]: Extend MemoryCacheOptions with TrackLinkedCacheEntries #31739

Closed

Description

@maryamariyan

Description

Background and Motivation

MemoryCache by default tracks linked cache entries to allow for propagating options.

In the following example, the expirationToken added for child is also applied to parent:

using (var parent = cache.CreateEntry(key))
{
    parent.SetValue(obj);

    using (var child = cache.CreateEntry(key1))
    {
        child.SetValue(obj);
        child.AddExpirationToken(expirationToken);
    }
}

Tracking internally uses AsyncLocal<T>, which is expensive and adds non-trivial overhead. The cost of using it has popped out in the performance reports that we have received from our customers (#45436).

Before this feature this behavior could not be disabled.

New API

Here we extend the existing MemoryCacheOptions with a flag that disables this behaviour.

namespace Microsoft.Extensions.Caching.Memory
{
    public class MemoryCacheOptions
    {
+        public bool TrackLinkedCacheEntries { get ; set; }
    }
}

Usage Examples

var options = new MemoryCacheOptions
{
    TrackLinkedCacheEntries = false
};

var cache = new MemoryCache(options);

Risks

We have changed the defaults, it's now disabled by default.

Version

.NET 7 Preview 1

Previous behavior

MemoryCache by default tracks linked cache entries to allow for propagating options.

New behavior

Using MemoryCacheOptions.TrackLinkedCacheEntries this behavior can be configured properly.

Type of breaking change

  • Binary incompatible: Existing binaries may encounter a breaking change in behavior, such as failure to load/execute or different run-time behavior.
  • Source incompatible: Source code may encounter a breaking change in behavior when targeting the new runtime/component/SDK, such as compile errors or different run-time behavior.

Reason for change

Performance improvement

Recommended action

Refer to usage sample.

Feature area

C#, Core .NET libraries, Extensions

Affected APIs

namespace Microsoft.Extensions.Caching.Memory
{
    partial class MemoryCacheOptions
    {
        public bool TrackLinkedCacheEntries { get ; set; } = false;
    }
}

Activity

  1. added
    breaking-change
    on Oct 12, 2022
  2. added
    ⌚ Not Triaged
    🏁 Release: .NET 7
    source incompatible
    on Oct 12, 2022
  3. adamsitnik commented on Oct 13, 2022

    @adamsitnik
    Member

    Introducing this API has no risks as long as we don't change the default settings (enabled by default).

    We have changed the defaults, it's now disabled by default.

  4. maryamariyan commented on Oct 13, 2022

    @maryamariyan
    ContributorAuthor

    Updated the description. Thanks @adamsitnik

  5. ghost added
    in-pr
    on Oct 13, 2022
  6. Repository owner moved this from 🔖 Ready to ✅ Done in dotnet/docs October Sprinton Oct 17, 2022
  7. ghost removed
    in-pr
    on Oct 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

🏁 Release: .NET 7breaking-changesource incompatible

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions