Example #1
0
        /// <summary>
        /// Creates a <see cref="LockingCacheDecorator{TKey,TValue}"/> instance that is thread-safe and uses the <see cref="EqualityComparer{T}.Default"/>
        /// <see cref="IEqualityComparer{T}"/>.
        /// </summary>
        /// <typeparam name="TKey">The type of the keys.</typeparam>
        /// <typeparam name="TValue">The type of the values.</typeparam>
        /// <param name="invalidationToken">The <see cref="LockingInvalidationToken"/> that can be used to signal a cache invalidation. Must not be <see langword="null" />.</param>
        /// <returns>
        /// A <see cref="LockingCacheDecorator{TKey,TValue}"/> instances for storing keys and values in a thread-safe way.
        /// </returns>
        /// <remarks>
        /// The created instance uses a single lock (see <see cref="Monitor"/>) to guard the data store against multi-threaded access. It is well-suited
        /// for caches in which the factory delegates passed to <see cref="ICache{TKey,TValue}.GetOrCreateValue"/> only take a short time to
        /// complete. When the factory delegates take a long time to execute, consider using <see cref="CreateWithLazyLocking{TKey,TValue}()"/> instead
        /// to reduce contention.
        /// </remarks>
        public static ICache <TKey, TValue> CreateWithLocking <TKey, TValue> (
            [NotNull] LockingInvalidationToken invalidationToken)
        {
            ArgumentUtility.CheckNotNull("invalidationToken", invalidationToken);

            return(new LockingCacheDecorator <TKey, TValue> (
                       new InvalidationTokenBasedCacheDecorator <TKey, TValue> (new Cache <TKey, TValue>(), invalidationToken)));
        }
Example #2
0
        /// <summary>
        /// Creates a <see cref="LazyLockingCachingAdapter{TKey,TValue}"/> instance that is thread-safe and uses the <see cref="EqualityComparer{T}.Default"/>
        /// <see cref="IEqualityComparer{T}"/>.
        /// </summary>
        /// <typeparam name="TKey">The type of the keys.</typeparam>
        /// <typeparam name="TValue">The type of the values.</typeparam>
        /// <param name="invalidationToken">The <see cref="LockingInvalidationToken"/> that can be used to signal a cache invalidation. Must not be <see langword="null" />.</param>
        /// <returns>
        /// A <see cref="LazyLockingCachingAdapter{TKey,TValue}"/> instances for storing keys and values in a thread-safe way.
        /// </returns>
        /// <remarks>
        /// The created instance uses a single lock (see <see cref="Monitor"/>) to guard the data store against multi-threaded access and additional,
        /// double-checked locks (see <see cref="DoubleCheckedLockingContainer{T}"/>) to protect each single value. It is well-suited for caches
        /// in which the factory delegates passed to <see cref="ICache{TKey,TValue}.GetOrCreateValue"/> take a long time to execute. When the factory
        /// delegates do not take a long time, consider using <see cref="CreateWithLocking{TKey,TValue}()"/> instead to reduce the number of locks used.
        /// </remarks>
        public static ICache <TKey, TValue> CreateWithLazyLocking <TKey, TValue> (
            [NotNull] LockingInvalidationToken invalidationToken)
            where TValue : class
        {
            ArgumentUtility.CheckNotNull("invalidationToken", invalidationToken);

            return(new LazyLockingCachingAdapter <TKey, TValue> (
                       new InvalidationTokenBasedCacheDecorator <TKey, DoubleCheckedLockingContainer <LazyLockingCachingAdapter <TKey, TValue> .Wrapper> > (
                           new Cache <TKey, DoubleCheckedLockingContainer <LazyLockingCachingAdapter <TKey, TValue> .Wrapper> >(),
                           invalidationToken)));
        }