alastairtree / LazyCache

An easy to use thread safe in-memory caching service with a simple developer friendly API for c#
https://nuget.org/packages/LazyCache
MIT License
1.72k stars 159 forks source link

Separate instances sharing memory #198

Open sebbarg opened 1 week ago

sebbarg commented 1 week ago

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

Additional context Culprit seems to be this static: https://github.com/alastairtree/LazyCache/blob/633b5702ddbc3e013aa9a3893d7b4dc0c807943f/LazyCache/CachingService.cs#L42