Skip to content

DefaultCacheDurationSeconds ignored in CachingService instance methods #152

@r-bennett

Description

@r-bennett

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions