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.71k stars 159 forks source link

Bug in Wiki: Why does the Cancellation Tokens example use two CancellationTokenSource? #185

Open schittli opened 1 year ago

schittli commented 1 year ago

Hello, thank you for sharing your great work!

Describe the bug

'bug' is the best matching issue type because the doc in Wiki is very useful, but it has still one open question:

Here: https://github.com/alastairtree/LazyCache/wiki/API-documentation-(v-2.x) in Chapter: Method 2 - Cancellation Tokens (could also be used to dispose a range of items)

The doc explains how to add two items to the cache using a Cancellation Token. But why does the example calculates options2 to add the 2nd item to the cache? Why is it not possible to just re-use options1 like this:?

var expireToken1 = new CancellationChangeToken(sharedExpiryTokenSource.Token);
var options1 = new MemoryCacheEntryOptions()
                   .AddExpirationToken(expireToken)
var product1 = cache.GetOrAdd($"Products-1", () => dbContext.Products.GetAsync(1), options1);
var product2 = cache.GetOrAdd($"Products-2", () => dbContext.Products.GetAsync(2), options1);

Thanks a lot, kind regards, Thomas