Basically the data structure I want would mirror a MSMQ but would be in memory, because it is being used in the one process. By mirroring MSMQ I mean you would enqueue objects, then you could either dequeue objects or retrieve them using a key. Here is me initial attempt. My main problem with this attempt is that the Get by id would be used frequently and therefore the queue would end up having a lot of "dead" objects in it.
public class QueueDictionary<TKey, TValue>
{
private readonly Queue _queue = new Queue();
private readonly Dictionary<TKey, TValue> _dictionary = new Dictionary<TKey, TValue>();
private readonly object _syncRoot = new object();
public TValue Dequeue()
{
lock (_syncRoot)
{
TKey key = (TKey)_queue.Dequeue();
while (!_dictionary.ContainsKey(key))
key = (TKey)_queue.Dequeue();
return _dictionary[key];
}
}
public TValue Get(TKey key)
{
lock (_syncRoot)
{
TValue result = _dictionary[key];
_dictionary.Remove(key);
return result;
}
}
public void Enqueue(TKey key, TValue value)
{
lock (_syncRoot)
{
_dictionary.Add(key, value);
_queue.Enqueue(key);
}
}
}