public async void FetchWithMultipleEntries() { using (var memoryCache = new MemoryCache(new MemoryCacheOptions())) { var cache = new ConcurrentMemoryCache(memoryCache); var tasks = new List <Task>(); for (var i = 0; i < 10; i++) { tasks.Add(Task.Run(() => { var res = cache.GetOrCreate($"key1-{i}", entry => i); Assert.Equal(i, res); })); } await Task.WhenAll(tasks); } }
public async void FetchWithSingleEntry() { using (var memoryCache = new MemoryCache(new MemoryCacheOptions())) { var cache = new ConcurrentMemoryCache(memoryCache); var results = new List <int>(); var tasks = new List <Task>(); for (var i = 0; i < 10; i++) { tasks.Add(Task.Run(() => results.Add(cache.GetOrCreate <int>("key1", entry => i)))); } await Task.WhenAll(tasks); var cachedItem = results[0]; Assert.All(results, res => Assert.Equal(cachedItem, res)); } }
public void It_works() { var memoryCache = new MemoryCache(new MemoryCacheOptions()); var wrapper = new ConcurrentMemoryCache(memoryCache, 5 * 60); var counter = 0; var operations = new List <Action>(); for (var i = 0; i < 10000; ++i) { operations.Add(() => wrapper.GetOrCreate("ConcurrentMemoryCacheTests.It_works", () => counter += 1)); } Parallel.Invoke(operations.ToArray()); Assert.Equal(1, counter); }