Esempio n. 1
0
 private void TryCreateTeardown(TKey key, ConstructionState state)
 {
     if (States.TryRemove(key, out state))
     {
         state.Set();
         state.Dispose();
     }
 }
Esempio n. 2
0
        public bool TryCreate(TKey key, Func <TValue> creator)
        {
            ConstructionState state;

            if (Storage.ContainsKey(key))
            {
                return(false);
            }

            state = new ConstructionState();
            if (States.TryAdd(key, state))
            {
                try {
                    if (Storage.ContainsKey(key))
                    {
                        return(false);
                    }

                    var result = creator();

                    if (!Storage.TryAdd(key, result))
                    {
                        throw new InvalidOperationException("Cache entry was created by someone else while construction lock was held");
                    }

                    return(true);
                } finally {
                    States.TryRemove(key, out state);
                    state.Set();
                    state.Dispose();
                }
            }
            else
            {
                state.Dispose();
            }

            return(false);
        }