Пример #1
0
        protected override async Task <TResponse> Process(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate <TResponse> next)
        {
            // cache only if implements interface
            var cacheRequest = request as ICacheQueryResult;

            if (cacheRequest == null || !cacheRequest.IsCacheable())
            {
                return(await next().ConfigureAwait(false));
            }

            var cacheKey = cacheRequest.GetCacheKey();

            // check cache
            var cachedBuffer = await _distributedCache
                               .GetAsync(cacheKey, cancellationToken)
                               .ConfigureAwait(false);

            if (cachedBuffer != null)
            {
                var cachedItem = await _distributedCacheSerializer
                                 .FromByteArrayAsync <TResponse>(cachedBuffer)
                                 .ConfigureAwait(false);

                Logger.LogTrace("Cache Hit; Key: '{cacheKey}'.", cacheKey);

                return(cachedItem);
            }

            Logger.LogTrace("Cache Miss; Key: '{cacheKey}'.", cacheKey);

            // continue if not found in cache
            var result = await next().ConfigureAwait(false);

            if (result == null)
            {
                return(default(TResponse));
            }

            // save to cache
            var itemBuffer = await _distributedCacheSerializer
                             .ToByteArrayAsync(result)
                             .ConfigureAwait(false);

            var options = new DistributedCacheEntryOptions
            {
                SlidingExpiration  = cacheRequest.SlidingExpiration(),
                AbsoluteExpiration = cacheRequest.AbsoluteExpiration()
            };

            await _distributedCache
            .SetAsync(cacheKey, itemBuffer, options, cancellationToken)
            .ConfigureAwait(false);

            Logger.LogTrace("Cache Insert; Key: '{cacheKey}'.", cacheKey);

            return(result);
        }
    protected override async Task <TResponse> Process(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate <TResponse> next)
    {
        if (next is null)
        {
            throw new ArgumentNullException(nameof(next));
        }

        if (next is null)
        {
            throw new ArgumentNullException(nameof(next));
        }

        // cache only if implements interface
        var cacheRequest = request as ICacheQueryResult;

        if (cacheRequest?.IsCacheable() != true)
        {
            return(await next().ConfigureAwait(false));
        }

        var cacheKey = cacheRequest.GetCacheKey();

        // check cache
        var cachedBuffer = await _distributedCache
                           .GetAsync(cacheKey, cancellationToken)
                           .ConfigureAwait(false);

        if (cachedBuffer != null)
        {
            var cachedItem = await _distributedCacheSerializer
                             .FromByteArrayAsync <TResponse>(cachedBuffer)
                             .ConfigureAwait(false);

            LogCacheAction(Logger, "Hit", cacheKey);

            return(cachedItem);
        }

        LogCacheAction(Logger, "Miss", cacheKey);

        // continue if not found in cache
        var result = await next().ConfigureAwait(false);

        if (result == null)
        {
            return(result);
        }

        // save to cache
        var itemBuffer = await _distributedCacheSerializer
                         .ToByteArrayAsync(result)
                         .ConfigureAwait(false);

        var options = new DistributedCacheEntryOptions
        {
            SlidingExpiration  = cacheRequest.SlidingExpiration(),
            AbsoluteExpiration = cacheRequest.AbsoluteExpiration()
        };

        await _distributedCache
        .SetAsync(cacheKey, itemBuffer, options, cancellationToken)
        .ConfigureAwait(false);

        LogCacheAction(Logger, "Insert", cacheKey);

        return(result);
    }