Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expiring Concurrent Dictionary

Tags:

c#

Hi I'm doing some research after a concurrent dictionary with expiration features.

We have a list of objects that are accessed with adds and removes from a lot different threads. We also want an expiration time on the items for like lets say 60 secconds. (It would be nice if we can listen on an event that tells us which items that has been expired and removed from the list).

We also need the lookup to be really fast like an Dictionary.

The list will contain houndred of thousands objects.

So its like an ConcurrentDictionary but with expiration features.

Can MemoryCache be something?

Or are they other things to look on?

like image 857
NPehrsson Avatar asked Sep 26 '13 10:09

NPehrsson


People also ask

Do you need to lock Concurrent Dictionary?

ConcurrentDictionary<TKey,TValue> is designed for multithreaded scenarios. You do not have to use locks in your code to add or remove items from the collection. However, it is always possible for one thread to retrieve a value, and another thread to immediately update the collection by giving the same key a new value.

What is a concurrent Dictionary?

adjective. occurring or existing simultaneously or side by side: concurrent attacks by land, sea, and air. acting in conjunction; cooperating: the concurrent efforts of several legislators to pass the new law. having equal authority or jurisdiction: two concurrent courts of law.

What is the difference between Dictionary and concurrent Dictionary?

The biggest reason to use ConcurrentDictionary over the normal Dictionary is thread safety. If your application will get multiple threads using the same dictionary at the same time, you need the thread-safe ConcurrentDictionary , this is particularly true when these threads are writing to or building the dictionary.

What is the purpose of the ConcurrentDictionary TKey TValue class?

Represents a thread-safe collection of key/value pairs that can be accessed by multiple threads concurrently.


1 Answers

MemoryCache looks ideal, especially as

This type is thread safe.

like image 162
spender Avatar answered Oct 20 '22 12:10

spender