❔ Filter a ConcurrentDictionary
I have a
ConcurrentDictionary
that I'm using for a small in-memory cache and I'm wondering what the best way to filter items out of it. one of the fields on the value stored in the dictionary is a DateTimeOffset
expiration time and I'm still new to using it.
Currently, I'm using .Select()
to filter out the expired items and casting it back to the Dict, overwriting the existing variable with a new one:
Is there a better way to do this?10 Replies
That will throw an exception, because the thing returned by Select isn't a ConcurrentDictionary
Also if you're re-assigning
_cache
, then you need to be careful about thread-safety. You might need another lock around that field, and consumers might need to take that lock before accessing _cache
, etc, and you start to defeat the point of using a concurrent collection in the first place
Life is going to be a lot easier if you make _cache
readonly
, and never assign it. Your approach to removing stale items is then to iterate over it, and remove any values which have expired.
... or you can use a MemoryCache
: https://learn.microsoft.com/en-us/dotnet/api/system.runtime.caching.memorycache?view=dotnet-plat-ext-7.0My initial code did two
foreach
loops, one to get the expired items and one to remove them, but thought I should do a Select
to cut down on the number of iterations:
Yep, that's how I'd do it
Thanks for confirming! I recently started C# after being a front-end dev so I'm still learning 😁
You might consider using a MemoryCache which is thread safe and has built-in support for evicting expired entries.
I'll have a look, thanks!
Wow, I've never heard of this type. Thanks for sharing this
☝️
no one told you to send long answers
(jk)
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.