Пример #1
0
        protected override void ExitLock()
        {
            if (CacheLock.IsWriteLockHeld)
            {
                CacheLock.ExitWriteLock();
            }

            if (CacheLock.IsUpgradeableReadLockHeld)
            {
                CacheLock.ExitUpgradeableReadLock();
            }
        }
Пример #2
0
        /// <summary>
        /// Gets or adds a key/value pair from the memoizer if it already exists.  Adds a key/value pair to the memoizer if it does not already exist.
        /// </summary>
        /// <param name="key">The key of the element to get or add.</param>
        /// <returns>The value for the key.  This will be either the existing value for the key if the key is already in the memoizer, or the new value if the key was not in the memoizer.</returns>
        public TValue GetOrAdd(TKey key)
        {
            TValue result;

            CacheLock.EnterUpgradeableReadLock();
            try
            {
                if (ContainsKey(key))
                {
                    return(GetValue(key));
                }
                else
                {
                    Func <TValue> func = () => GetOrSetValue(key);
                    result = func.InvokeWithWriteLock(CacheLock);
                }
            }
            finally
            {
                CacheLock.ExitUpgradeableReadLock();
            }

            return(result);
        }