You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm experiencing an unexpected behavior with a Dictionary<(int, int), int> that is intended to act as a cache. Despite implementing a locking mechanism to ensure thread safety, I encounter a duplicate key exception when attempting to add an entry to the dictionary.
Here is the relevant code snippet:
privatereadonlyDictionary<(int,int),int>_hashCodeCausalTypeCache=newDictionary<(int,int),int>();privatereadonlyobject_lockObj=newobject();
.........................lock(_lockObj){if(_hashCodeCausalTypeCache.TryGetValue(key,outvarvalue)){causalType=value;}
else
{var dataTable = Globals.SelectDataTable(@$"SELECT ac.causal_type FROM trans_articles ta LEFT JOIN article_causals ac ON ac.id = ta.causal_id WHERE ta.transaction_id = {transactionId} AND ta.hash_code = {hashCode}");if(dataTable==null||dataTable.Rows==null||dataTable.Rows.Count==0){return;}
causalType = SafeConvert.ToInt32(dataTable.Rows[0]["causal_type"]);
_hashCodeCausalTypeCache.Add(key,causalType);}}
Issue Details:
The first TryGetValue check returns false, allowing the addition of a new entry to the dictionary.
However, immediately afterward, I encounter a duplicate key exception when attempting to add the same key, even though it was not found in the first check.
There are no multi-threading issues involved in this scenario, and the cache is managed in a single thread.
I would appreciate any insights into why this might be occurring and any potential solutions to resolve this issue. Thank you!
The text was updated successfully, but these errors were encountered:
I'm experiencing an unexpected behavior with a
Dictionary<(int, int), int>
that is intended to act as a cache. Despite implementing a locking mechanism to ensure thread safety, I encounter a duplicate key exception when attempting to add an entry to the dictionary.Here is the relevant code snippet:
Issue Details:
TryGetValue
check returnsfalse
, allowing the addition of a new entry to the dictionary.I would appreciate any insights into why this might be occurring and any potential solutions to resolve this issue. Thank you!
The text was updated successfully, but these errors were encountered: