Example #1
0
        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);
        }
Example #2
0
 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);
         }
     }
 }