Exemple #1
0
        /// <summary>
        /// <para>Builds a <see cref="Policy" /> that will function like a result cache for delegate executions returning a <typeparamref name="TResult"/>.</para>
        /// <para>Before executing a delegate, checks whether the <paramref name="cacheProvider" /> holds a value for the cache key determined by applying the <paramref name="cacheKeyStrategy"/> to the execution <see cref="Context"/>.
        /// If the <paramref name="cacheProvider" /> contains a value, returns that value and does not execute the governed delegate.  If the <paramref name="cacheProvider" /> does not provide a value, executes the governed delegate, stores the value with the <paramref name="cacheProvider" />, then returns the value.
        /// </para>
        /// </summary>
        /// <param name="cacheProvider">The cache provider.</param>
        /// <param name="ttl">Duration (ttl) for which to cache values.</param>
        /// <param name="cacheKeyStrategy">The cache key strategy.</param>
        /// <param name="onCacheError">Delegate to call if an exception is thrown when attempting to get a value from or put a value into the cache, passing the execution context, the cache key, and the exception.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="ArgumentNullException">cacheProvider</exception>
        /// <exception cref="ArgumentNullException">cacheKeyStrategy</exception>
        public static CachePolicy <TResult> Cache <TResult>(ISyncCacheProvider cacheProvider, TimeSpan ttl, Func <Context, string> cacheKeyStrategy, Action <Context, string, Exception> onCacheError = null)
        {
            if (cacheProvider == null)
            {
                throw new ArgumentNullException(nameof(cacheProvider));
            }

            return(Cache <TResult>(cacheProvider.For <TResult>(), new RelativeTtl(ttl), cacheKeyStrategy, onCacheError));
        }
Exemple #2
0
        /// <summary>
        /// <para>Builds a <see cref="Policy"/> that will function like a result cache for delegate executions returning a <typeparamref name="TResult"/>.</para>
        /// <para>Before executing a delegate, checks whether the <paramref name="cacheProvider"/> holds a value for the cache key specified by <see cref="M:Context.ExecutionKey"/>.
        /// If the <paramref name="cacheProvider"/> contains a value, returns that value and does not execute the governed delegate.  If the <paramref name="cacheProvider"/> does not provide a value, executes the governed delegate, stores the value with the <paramref name="cacheProvider"/>, then returns the value.
        /// </para>
        /// </summary>
        /// <param name="cacheProvider">The cache provider.</param>
        /// <param name="ttlStrategy">A strategy for specifying ttl for values to be cached.</param>
        /// <param name="onCacheError">Delegate to call if an exception is thrown when attempting to get a value from or put a value into the cache, passing the execution context, the cache key, and the exception.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="ArgumentNullException">cacheProvider</exception>
        /// <exception cref="ArgumentNullException">ttlStrategy</exception>
        public static CachePolicy <TResult> Cache <TResult>(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Action <Context, string, Exception> onCacheError = null)
        {
            if (cacheProvider == null)
            {
                throw new ArgumentNullException(nameof(cacheProvider));
            }

            return(Cache <TResult>(cacheProvider.For <TResult>(), ttlStrategy, DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheError));
        }
Exemple #3
0
 internal override TResult ExecuteInternal <TResult>(Func <Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
 {
     return(CacheEngine.Implementation <TResult>(
                _syncCacheProvider.For <TResult>(),
                _ttlStrategy.For <TResult>(),
                _cacheKeyStrategy,
                action,
                context,
                cancellationToken,
                _onCacheGet,
                _onCacheMiss,
                _onCachePut,
                _onCacheGetError,
                _onCachePutError));
 }
Exemple #4
0
        internal override TResult ExecuteInternal <TResult>(Func <Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
        {
            if (_syncCacheProvider == null)
            {
                throw new InvalidOperationException("Please use the synchronous-defined policies when calling the synchronous Execute (and similar) methods.");
            }

            return(CacheEngine.Implementation <TResult>(
                       _syncCacheProvider.For <TResult>(),
                       _ttlStrategy.For <TResult>(),
                       _cacheKeyStrategy,
                       action,
                       context,
                       cancellationToken,
                       _onCacheGet,
                       _onCacheMiss,
                       _onCachePut,
                       _onCacheGetError,
                       _onCachePutError));
        }