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

MemoryCache stopped working in 5.0.0 #59025

Closed
eocron opened this issue Sep 13, 2021 · 3 comments
Closed

MemoryCache stopped working in 5.0.0 #59025

eocron opened this issue Sep 13, 2021 · 3 comments
Labels
area-Extensions-Caching untriaged New issue has not been triaged by the area owner

Comments

@eocron
Copy link

eocron commented Sep 13, 2021

Description

MemoryCache.CreateEntry not creating anything in cache.

Configuration

Windows 10
.net 50
NUnit project
Microsoft.Extensions.Caching.Memory 5.0.0 from nuget

Other information

    [Test]
    public async Task CacheTest()
    {
        var cache = new MemoryCache(new MemoryCacheOptions());
        cache.CreateEntry("foo").SetValue("bar").SetAbsoluteExpiration(TimeSpan.FromSeconds(100));
        string result;
        Assert.IsTrue(cache.TryGetValue("foo", out result)); //this will fail
    }

Update 1:

Looking in decompiled code I see it requires for entry to be disposed. Is there some reason why it doesn't have more explanatory Commit/Apply/Configure+Build methods?
Also, no documentation has been found by me for this one.

@dotnet-issue-labeler dotnet-issue-labeler bot added area-Extensions-Caching untriaged New issue has not been triaged by the area owner labels Sep 13, 2021
@ghost
Copy link

ghost commented Sep 13, 2021

Tagging subscribers to this area: @eerhardt, @maryamariyan, @michaelgsharp
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

MemoryCache.CreateEntry not creating anything in cache.

Configuration

.net 50
NUnit project

Other information

    [Test]
    public async Task CacheTest()
    {
        var cache = new MemoryCache(new MemoryCacheOptions());
        cache.CreateEntry("foo").SetValue("bar").SetAbsoluteExpiration(TimeSpan.FromSeconds(100));
        string result;
        Assert.IsTrue(cache.TryGetValue("foo", out result));
    }
Author: eocron
Assignees: -
Labels:

untriaged, area-Extensions-Caching

Milestone: -

@huoyaoyuan
Copy link
Member

You can use extension method Set from CacheExtensions:

cache.Set("foo", "bar", TimeSpan.FromSeconds(100));

CreateEntry isn't friendly for direct usage. Use the extension methods.

@eerhardt
Copy link
Member

Looking in decompiled code I see it requires for entry to be disposed.

This is correct. It was not an ideal design from the start, but since it has shipped for multiple versions, we can't make a breaking change any longer.

See #36556

Using the extensions methods, as @huoyaoyuan points out, is the best option.

@ghost ghost locked as resolved and limited conversation to collaborators Nov 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Extensions-Caching untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

3 participants