public Task <TVal> GetOrAdd(TKey key, Func <TKey, TVal> func) { //hmmm with immutable dictionary i could first check here var getOrAddCommand = new GetOrAddCommand(key, func); _ = commandBlock.Post(getOrAddCommand); return(getOrAddCommand.Result.Task); }
private void GetOrAdd(GetOrAddCommand getOrAddCommand) { if (cache.TryGetValue(getOrAddCommand.Key, out var existing)) { getOrAddCommand.Result.SetResult(existing); } else { try { TVal val = getOrAddCommand.Func(getOrAddCommand.Key); getOrAddCommand.Result.SetResult(val); cache.Add(getOrAddCommand.Key, val); } catch (Exception ex) { getOrAddCommand.Result.TrySetException(ex); } } }