Parallel Collection SortedList or O (log n)

I need to cache a large amount of data from a database in an ASP.NET MVC application and would like to use a SortedList. I know that in .NET 4.0 parallel collections are added, but there is no sorting. I thought of using a SynchronizedCollection, but uses locks intensively even for reading (if I'm not mistaken), so I'm looking for other options. Basically, I need a parallel collection with O (log n) access complexity.

EDIT - code based on Greg's answer

void WrappedAdd(TKey k, TValue v)
{
  var copy = new SortedList<TKey, TValue>(_sortedList);
  copy.Add(k, v);
  _sortedList = copy;
}
+4
source share
1 answer

, , . , ? ?

, , , . NuGet . , , ( ).

(, concurrency , ). , . , , , , .

, log n.

public class ConcurrentWrapper<TKey, T> {
    ImmutableSortedDictionary<TKey, T> _inner;

    public void Add(TKey key, T item) {
        lock (_inner) {
            _inner = _inner.Add(key, item);
        }
    }

    public T Get(TKey key) {
        return _inner[key];
    }
}
+2

Source: https://habr.com/ru/post/1532741/


All Articles