✅ IMemoryCache missing Contains?
Why is there no Contains in IMemoryCache? I feel like I'm missing something really obvious here.
7 Replies
you can use tryget and discard the value if you really need it
True, I suppose I could do that. Doesn't seem very efficient, but that would work
it is identical
the implementation of ContainsKey on ConcurrentDictionary is TryGetValue(key, out _)
(and, MemoryCache is essentially a clumsy wrapper over ConcurrentDictionary)
Okay, cool. Funny, I'm using MemoryCach to store ConcurrentDictionaries 😄
i think it isn't provided because it does not really make sense for a cache
what would you do with the return value of
Contains
I see what you mean. I was confused because when I google it, everything says "just use contains", but I can't find contains. Nonetheless, I think I can see how I can just use TryGetValue or GetOrCreate to do what needs to be done. I suppose that check isn't necessary
Thank you for your help 🙂
right. the useful, atomic, operations are already provided. Contains used in a naive way is pretty much always a race condition