concurrentdictionary

Get KeyValuePair by Key from a ConcurrentDictionary (in O(1) time)

这一生的挚爱 提交于 2020-04-30 02:17:11
问题 As per this solution (https://stackoverflow.com/a/18923091/529618) I am using a ConcurrentDictionary<T,byte> as a workaround for the lack of ConcurrentHashSet<T> . However, I'm struggling to see how I can get the original T Key back out of the dictionary in O(1) time. var cache = new ConcurrentDictionary<MyEquatableClass, byte>()); //... if(!cache.TryAdd(classInstance, Byte.MinValue)) return /* Existing cache entry */; return classInstance; Is there any way to get the KeyValuePair<K,V> (or

Get KeyValuePair by Key from a ConcurrentDictionary (in O(1) time)

偶尔善良 提交于 2020-04-30 02:13:20
问题 As per this solution (https://stackoverflow.com/a/18923091/529618) I am using a ConcurrentDictionary<T,byte> as a workaround for the lack of ConcurrentHashSet<T> . However, I'm struggling to see how I can get the original T Key back out of the dictionary in O(1) time. var cache = new ConcurrentDictionary<MyEquatableClass, byte>()); //... if(!cache.TryAdd(classInstance, Byte.MinValue)) return /* Existing cache entry */; return classInstance; Is there any way to get the KeyValuePair<K,V> (or

Safely removing list mapping from ConcurrentDictionary

☆樱花仙子☆ 提交于 2020-03-19 06:06:39
问题 I have a ConcurrentDictionary which maps a simple type to a list: var dict = new ConcurrentDictionary<string, List<string>>(); I can use AddOrUpdate() to cater for both initialization of the list when the first value is added, and addition of subsequent values to the list. However, the same isn't true for removal. If I do something like: public void Remove(string key, string value) { List<string> list; var found = dict.TryGetValue(key, out list); if (found) { list.Remove(value); if (list

ConcurrentDictionary TryGetValue vs []. Is [] still thread-safe?

坚强是说给别人听的谎言 提交于 2020-03-18 03:37:07
问题 I have the following ConcurrentDictionary : ConcurrentDictionary<Guid, Session> sessions; I know that sessions.TryGetValue(key, out session) is thread-safe, but my question is if sessions[key] is also thread-safe? sessions.TryGetValue(key, out session) returns true or false depending on whether it was able to get the value or not. Will sessions[key] return null if it is unable to get the value? I would think so. Can anyone confirm or shed more light on this? Thanks. 回答1: It is thread-safe,

.NET ConcurrentDictionary.ToArray() ArgumentException

落花浮王杯 提交于 2020-01-02 01:46:47
问题 Sometimes I get the error below when I call ConcurrentDictionary.ToArray. Error Below: System.ArgumentException: The index is equal to or greater than the length of the array, or the number of elements in the dictionary is greater than the available space from index to the end of the destination array. at System.Collections.Concurrent.ConcurrentDictionary 2.System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.CopyTo(KeyValuePair 2[] array, Int32 index)

Slow azure queue webjob performance (local dev)

折月煮酒 提交于 2019-12-24 08:09:51
问题 My webapi must perform a set of heavy operations in order to fullfil a request. To minimize processing time I am offload the "view counter increment" to a webjob. The way I am doing it currently is by enqueueing a message with userId and productId to azure queue storage at the end of each request. The webjob function triggers on new queue messages and after parsing the message it adds values (increments or adds new) to a static concurrent dictionary. I am not incrementing and writing to azure

How to store the result of an async method in a .NET ConcurrentDictionary when calling GetOrAdd?

这一生的挚爱 提交于 2019-12-24 07:34:50
问题 I have a private ConcurrentDictionary that is a simple lookup table of some DB keys. I'm trying to leverage the ConcurrentDictionary so that it will only do one call to the db when 2+ requests to the same line of code, are made at the same time. (Which is why i'm using a ConcurrentDictionary .) How can I do this please? This is what I was trying to do .. but I think it's storing the Task in the dictionary ... not the result of the task.... private readonly ConcurrentDictionary<string, Task

Is ConcurrentDictionary, a “concurrent” version of SortedList?

不羁岁月 提交于 2019-12-24 02:20:11
问题 I would like to understand the Computational Complexity of a ConcurrentDictionary vers SortedList (Which is O(logarithmic(n)) ), is a ConcurrentDictionary just a concurrent synchronized implementation of a SortedList ? or do these data structures vary? among one another? 回答1: ConcurrentDictionary<T,U> is a concurrent version of a Dictionary<T,U> . It does not sort by keys like a SortedList<T,U> . The complexity is closely related to a Dictionary<T,U> 's complexity, so fetches approach O(1).

How do you convert a dictionary to a ConcurrentDictionary?

╄→尐↘猪︶ㄣ 提交于 2019-12-23 07:48:59
问题 I have seen how to convert a ConcurrentDictionary to a Dictionary, but I have a dictionary and would like to convert to a ConcurrentDictionary. How do I do that?... better yet, can i set the link statement to be a ConcurrentDictionary? var customers = _customerRepo.Query().Select().ToDictionary(x => x.id, x => x); 回答1: Use ConcurrentDictionary<TKey, TValue> Constructor (IEnumerable<KeyValuePair<TKey, TValue>>) constructor which can accept a dictionary object like: Dictionary<int, string>

How can I convert a ConcurrentDictionary to a Dictionary?

99封情书 提交于 2019-12-21 03:14:14
问题 I have a ConcurrentDictionary object that I would like to set to a Dictionary object. Casting between them is not allowed. So how do I do it? 回答1: The ConcurrentDictionary<K,V> class implements the IDictionary<K,V> interface, which should be enough for most requirements. But if you really need a concrete Dictionary<K,V>... var newDictionary = yourConcurrentDictionary.ToDictionary(kvp => kvp.Key, kvp => kvp.Value, yourConcurrentDictionary.Comparer); // or... // substitute your actual key and