Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Breaking change]: Extend MemoryCacheOptions with TrackLinkedCacheEntries #31739

Closed
1 of 2 tasks
maryamariyan opened this issue Oct 12, 2022 · 2 comments · Fixed by #31770
Closed
1 of 2 tasks

[Breaking change]: Extend MemoryCacheOptions with TrackLinkedCacheEntries #31739

maryamariyan opened this issue Oct 12, 2022 · 2 comments · Fixed by #31770
Assignees
Labels
breaking-change Indicates a .NET Core breaking change 🏁 Release: .NET 7 Work items for the .NET 7 release doc-idea Indicates issues that are suggestions for new topics [org][type][category] Pri1 High priority, do before Pri2 and Pri3 source incompatible Source code may encounter a breaking change in behavior when targeting the new version.

Comments

@maryamariyan
Copy link
Member

maryamariyan commented Oct 12, 2022

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;
    }
}
@maryamariyan maryamariyan added doc-idea Indicates issues that are suggestions for new topics [org][type][category] breaking-change Indicates a .NET Core breaking change Pri1 High priority, do before Pri2 and Pri3 labels Oct 12, 2022
@dotnet-bot dotnet-bot added ⌚ Not Triaged Not triaged 🏁 Release: .NET 7 Work items for the .NET 7 release source incompatible Source code may encounter a breaking change in behavior when targeting the new version. labels Oct 12, 2022
@gewarren gewarren removed the ⌚ Not Triaged Not triaged label Oct 12, 2022
@adamsitnik
Copy link
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.

@maryamariyan
Copy link
Member Author

Updated the description. Thanks @adamsitnik

@ghost ghost added the in-pr This issue will be closed (fixed) by an active pull request. label Oct 13, 2022
@ghost ghost removed the in-pr This issue will be closed (fixed) by an active pull request. label Oct 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change Indicates a .NET Core breaking change 🏁 Release: .NET 7 Work items for the .NET 7 release doc-idea Indicates issues that are suggestions for new topics [org][type][category] Pri1 High priority, do before Pri2 and Pri3 source incompatible Source code may encounter a breaking change in behavior when targeting the new version.
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants