示例#1
0
        internal static async Task <TResult> ImplementationAsync <TResult>(
            IAsyncCacheProvider <TResult> cacheProvider,
            ITtlStrategy ttlStrategy,
            Func <Context, string> cacheKeyStrategy,
            Func <Context, CancellationToken, Task <TResult> > action,
            Context context,
            CancellationToken cancellationToken,
            bool continueOnCapturedContext,
            Action <Context, string> onCacheGet,
            Action <Context, string> onCacheMiss,
            Action <Context, string> onCachePut,
            Action <Context, string, Exception> onCacheGetError,
            Action <Context, string, Exception> onCachePutError)
        {
            cancellationToken.ThrowIfCancellationRequested();

            string cacheKey = cacheKeyStrategy(context);

            if (cacheKey == null)
            {
                return(await action(context, cancellationToken).ConfigureAwait(continueOnCapturedContext));
            }

            TResult valueFromCache;

            try
            {
                valueFromCache = await cacheProvider.GetAsync(cacheKey, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext);
            }
            catch (Exception ex)
            {
                valueFromCache = default(TResult);
                onCacheGetError(context, cacheKey, ex);
            }
            if (valueFromCache != null && !valueFromCache.Equals(default(TResult)))
            {
                onCacheGet(context, cacheKey);
                return(valueFromCache);
            }
            else
            {
                onCacheMiss(context, cacheKey);
            }

            TResult result = await action(context, cancellationToken).ConfigureAwait(continueOnCapturedContext);

            Ttl ttl = ttlStrategy.GetTtl(context);

            if (ttl.Timespan > TimeSpan.Zero)
            {
                try
                {
                    await cacheProvider.PutAsync(cacheKey, result, ttl, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext);

                    onCachePut(context, cacheKey);
                }
                catch (Exception ex)
                {
                    onCachePutError(context, cacheKey, ex);
                }
            }

            return(result);
        }
示例#2
0
        internal static TResult Implementation <TResult>(
            ISyncCacheProvider <TResult> cacheProvider,
            ITtlStrategy <TResult> ttlStrategy,
            Func <Context, string> cacheKeyStrategy,
            Func <Context, CancellationToken, TResult> action,
            Context context,
            CancellationToken cancellationToken,
            Action <Context, string> onCacheGet,
            Action <Context, string> onCacheMiss,
            Action <Context, string> onCachePut,
            Action <Context, string, Exception> onCacheGetError,
            Action <Context, string, Exception> onCachePutError)
        {
            cancellationToken.ThrowIfCancellationRequested();

            string cacheKey = cacheKeyStrategy(context);

            if (cacheKey == null)
            {
                return(action(context, cancellationToken));
            }

            bool    cacheHit;
            TResult valueFromCache;

            try
            {
                (cacheHit, valueFromCache) = cacheProvider.TryGet(cacheKey);
            }
            catch (Exception ex)
            {
                cacheHit       = false;
                valueFromCache = default(TResult);
                onCacheGetError(context, cacheKey, ex);
            }
            if (cacheHit)
            {
                onCacheGet(context, cacheKey);
                return(valueFromCache);
            }
            else
            {
                onCacheMiss(context, cacheKey);
            }

            TResult result = action(context, cancellationToken);

            Ttl ttl = ttlStrategy.GetTtl(context, result);

            if (ttl.Timespan > TimeSpan.Zero)
            {
                try
                {
                    cacheProvider.Put(cacheKey, result, ttl);
                    onCachePut(context, cacheKey);
                }
                catch (Exception ex)
                {
                    onCachePutError(context, cacheKey, ex);
                }
            }

            return(result);
        }
 /// <summary>
 /// Puts the specified value in the cache.
 /// </summary>
 /// <param name="key">The cache key.</param>
 /// <param name="value">The value to put into the cache.</param>
 /// <param name="ttl">The time-to-live for the cache entry.</param>
 public void Put(string key, object value, Ttl ttl)
 {
     _wrappedCacheProvider.Put(key, _serializer.Serialize(value), ttl);
 }
 Task IAsyncCacheProvider <TCacheFormat> .PutAsync(string key, TCacheFormat value, Ttl ttl, CancellationToken cancellationToken, bool continueOnCapturedContext)
 {
     return(_wrappedCacheProvider.PutAsync(key, value, ttl, cancellationToken, continueOnCapturedContext));
 }