コード例 #1
0
        /// <inheritdoc cref="IAsyncCachedQuery{TContext,TCacheEntryOptions,TResult}.Execute"/>
        protected virtual async Task <TCachedResult> GetCachedResult(TContext context, IAsyncCacheStore <TCacheEntryOptions> cacheStore, CacheOption cacheOption, CancellationToken cancellationToken)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (cacheStore == null)
            {
                throw new ArgumentNullException(nameof(cacheStore));
            }

            State.SetContext(context);

            if (cacheOption == CacheOption.Default)
            {
                var cacheEntry = await cacheStore.GetEntryAsync <TCachedResult>(State.CacheKey, cancellationToken).ConfigureAwait(false);

                if (cacheEntry != null)
                {
                    return(State.SetCachedResult(cacheEntry.Value));
                }
            }

            var result = await Query(context, cancellationToken).ConfigureAwait(false);

            await cacheStore.SetEntryAsync(State.CacheKey, result.ToCacheEntry(), State.CacheEntryOptions, cancellationToken).ConfigureAwait(false);

            return(State.SetCachedResult(result));
        }
コード例 #2
0
        /// <inheritdoc cref="IAsyncCachedQuery{TCacheEntryOptions}.UpdateCachedResult"/>
        public virtual Task UpdateCachedResult(IAsyncCacheStore <TCacheEntryOptions> cacheStore, CancellationToken cancellationToken)
        {
            if (cacheStore == null)
            {
                throw new ArgumentNullException(nameof(cacheStore));
            }

            return(cacheStore.SetEntryAsync(State.CacheKey, State.CachedResult.ToCacheEntry(), State.CacheEntryOptions, cancellationToken));
        }