示例#1
0
        /// <summary>
        /// Invoke the passed <see cref="IEntryProcessor"/> against the
        /// specified <see cref="IInvocableCacheEntry"/>.
        /// </summary>
        /// <remarks>
        /// The invocation is made thread safe by locking the corresponding key
        /// on the cache.
        /// </remarks>
        /// <param name="cache">
        /// The <see cref="IConcurrentCache"/> that the
        /// <b>IEntryProcessor</b> works against.
        /// </param>
        /// <param name="entry">
        /// The <b>IInvocableCacheEntry</b> to process; it is not required to
        /// exist within the cache.
        /// </param>
        /// <param name="agent">
        /// The <b>IEntryProcessor</b> to use to process the specified key.
        /// </param>
        /// <returns>
        /// The result of the invocation as returned from the
        /// <b>IEntryProcessor</b>.
        /// </returns>
        public static object InvokeLocked(IConcurrentCache cache,
                                          IInvocableCacheEntry entry, IEntryProcessor agent)
        {
            var key = entry.Key;

            cache.Lock(key, -1);
            try
            {
                return(agent.Process(entry));
            }
            finally
            {
                cache.Unlock(key);
            }
        }
示例#2
0
        private R InvokeHelper <R>(K key, IEntryProcessor <K, V, R> entryProcessor)
        {
            V    value;
            bool isPresent = dict.TryGetValue(key, out value);
            var  entry     = new InMemoryEntry <K, V>(key, value, isPresent);
            var  result    = entryProcessor.Process(entry);

            if (entry.IsDirty || entry.IsRemoved)
            {
                if (entry.IsRemoved)
                {
                    V removedValue;
                    dict.TryRemove(key, out removedValue);
                }
                else
                {
                    dict[key] = entry.Value;
                }
            }
            return(result);
        }