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

Separate instances sharing memory #198

Open
sebbarg opened this issue Oct 23, 2024 · 1 comment
Open

Separate instances sharing memory #198

sebbarg opened this issue Oct 23, 2024 · 1 comment
Labels

Comments

@sebbarg
Copy link

sebbarg commented Oct 23, 2024

Describe the bug
Creating two separate instances of LazyCache, using default constructor will share the same underlying memory.
I'm not sure if this is a bug or by design, but it clearly violates the principle of least surprise :)

To Reproduce

using LazyCache;

// First cache

var cache1 = new CachingService();
cache1.GetOrAdd("key1", () => "value1");

// Another cache

var cache2 = new CachingService();
var value = cache2.Get<string>("key1");

Console.WriteLine(value);

// Expected value: null

// Actual value: "value1"

Expected behavior
Creating two instances of LazyCache using default constructors, should not share the underlying cache.

** Framework and Platform **

  • OS: [Everything]
  • Framework [NET8]
  • LazyCache Version [e.g. 2.4.0]

Additional context
Culprit seems to be this static:

public static Lazy<ICacheProvider> DefaultCacheProvider { get; set; }

@sebbarg sebbarg added the bug label Oct 23, 2024
@olsonpm
Copy link

olsonpm commented Jan 21, 2025

I agree this is an unexpected choice - but I think this is by design. See in the readme

// By default it uses a single shared cache under the hood so cache is shared out of the box (but you can configure this)
IAppCache cache = new CachingService();

However I don't see where to configure this which is a bummer. Going to look for an alternative library.

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

No branches or pull requests

2 participants