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

DefaultCacheDurationSeconds ignored in CachingService instance methods #152

Open
r-bennett opened this issue Apr 16, 2021 · 2 comments · May be fixed by #190
Open

DefaultCacheDurationSeconds ignored in CachingService instance methods #152

r-bennett opened this issue Apr 16, 2021 · 2 comments · May be fixed by #190
Labels

Comments

@r-bennett
Copy link

r-bennett commented Apr 16, 2021

Describe the bug
DefaultCacheDurationSeconds is being ignored in some code paths. It only appears to be used in AppCacheExtensions e.g. extension

public static Task<T> GetOrAddAsync<T>(this IAppCache cache, string key, Func<Task<T>> addItemFactory)
but not in the almost equivalent instance method
public virtual Task<T> GetOrAddAsync<T>(string key, Func<ICacheEntry, Task<T>> addItemFactory)

To Reproduce

            var cache = new CachingService
            {
                DefaultCachePolicy = {DefaultCacheDurationSeconds = 1}
            };

            var res1 = await cache.GetOrAddAsync("foo", async x => DateTime.UtcNow.Second);

            // wait for the item to expire
            await Task.Delay(TimeSpan.FromSeconds(2));

            // same key
            var res2 = await cache.GetOrAddAsync("foo", async x => DateTime.UtcNow.Second);
            // new key
            var res3 = await cache.GetOrAddAsync("bar", async x => DateTime.UtcNow.Second);
            
            Console.WriteLine(res1);
            Console.WriteLine(res2);
            Console.WriteLine(res3);

Expected behavior
A new value generated for each call, with output e.g.

23
25
25

Actual behavior
Same value reused for "foo", with output e.g.

23
23
25

Framework and Platform

  • OS: Windows 10
  • Framework net5.0
  • LazyCache Version 2.1.3

Additional context
Similar to the report in #121, although this isn't because of lazy eviction

@r-bennett r-bennett added the bug label Apr 16, 2021
@alastairtree
Copy link
Owner

Yeah I think the line

return GetOrAddAsync(key, addItemFactory, null);

which is currently

return GetOrAddAsync(key, addItemFactory, null);

should be

return cache.GetOrAddAsync(key, addItemFactory, cache.DefaultCachePolicy.BuildOptions());

Could you submit a failing test like your example and a PR to fix please?

@neozhu
Copy link

neozhu commented Aug 17, 2023

@alastairtree can you merge this PR#190 and update the version to support dotnet 7?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants