Exemplo n.º 1
0
        public TValue GetOrCreate <TUserData> (TKey key, TUserData userData, CreatorFunction <TUserData> creator)
        {
            bool createSuccess = false;

            while (true)
            {
                bool waitFailed = TryWaitForConstruction(key);

                TValue result;
                if (Storage.TryGetValue(key, out result))
                {
                    return(result);
                }
                else if (waitFailed)
                {
                    throw new ObjectDisposedException("Cache", "The cache was cleared or disposed.");
                }
                else if (createSuccess)
                {
                    throw new ThreadStateException("Failed to retrieve cache element after creating it");
                }

                createSuccess |= TryCreate(key, userData, creator);
            }
        }
Exemplo n.º 2
0
        public bool TryCreate <TUserData> (TKey key, TUserData userData, CreatorFunction <TUserData> creator, Predicate <TValue> shouldAdd = null)
        {
            ConstructionState state;

            if (TryCreateSetup(key, out state))
            {
                try {
                    var result = creator(key, userData);

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

                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                } finally {
                    TryCreateTeardown(key, state);
                }
            }

            return(false);
        }
Exemplo n.º 3
0
        public TValue GetOrCreate(TKey key, CreatorFunction creator)
        {
            while (true)
            {
                bool waitFailed = TryWaitForConstruction(key);

                TValue result;
                if (Storage.TryGetValue(key, out result))
                {
                    return(result);
                }
                else if (waitFailed)
                {
                    throw new ObjectDisposedException("Cache", "The cache was cleared or disposed.");
                }

                TryCreate(key, creator);
            }
        }
Exemplo n.º 4
0
        public bool TryCreate(TKey key, CreatorFunction creator)
        {
            ConstructionState state;

            if (TryCreateSetup(key, out state))
            {
                try {
                    var result = creator(key);

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

                    return(true);
                } finally {
                    TryCreateTeardown(key, state);
                }
            }

            return(false);
        }