I’m looking for the most efficient way to store key value pairs in a static Synchronized Dictionary (.NET 3.5, so not ConcurrentDictionary) while being able to access them at the same time.
Dictionary.Add(key, value);
if (Dictionary.Count >= 200)
{
foreach (KeyValuePair<string, Info> pair in Dictionary)
{
Info entry = pair.Value;
StoreInDatabase(entry);
}
Dictionary.Clear();
}
This is where the problem lies. If one user is adding to the dictionary while another is accessing and storing to the database it breaks.
lock (Dictionary)
{
//Same Code Above
}
I put a lock in, and it seems to work fine, but I’m wondering if there is a more efficient way of doing this. It’s not as efficient as I’d like it to be. Any suggestions would be much appreciated!
Note: I have to use the StoreInDatabase method to store the values.
REVISED CODE:
private static SynchronizedDictionary<string, Info> Dictionary = new SynchronizedDictionary<string, Info>();
...
Dictionary.Add(key, value);
if (Dictionary.Count >= 200)
{
SynchronizedDictionary<string, Info> temporaryDictionary = new SynchronizedDictionary<string, Info>();
lock (Dictionary)
{
temporaryDictionary = Dictionary;
Dictionary.Clear();
}
lock(temporaryDictionary)
{
foreach (KeyValuePair<string, Info> pair in temporaryDictionary)
{
Info entry = pair.Value;
StoreInDatabase(entry);
}
}
}
This greatly improved performance. Thanks flq!
You will have a lock around the DB operations which in comparison to in-memory activities take ages.
Within a lock, you should copy the values you want to store in the DB and clear the dictionary. Then you can release the dictionary and some other thread writes the stuff to DB.
Also it may make sense to use some private locking object in order to minimize the potential of deadlocks.