예제 #1
0
        public async void GetAsyncCallsCacheAndSerializer()
        {
            await _distributedResponseCache.GetAsync(Key);


            A.CallTo(() => _fakeDistributedCache.GetAsync(Key, A <CancellationToken> ._))
            .MustHaveHappenedOnceExactly();

            A.CallTo(() => _fakeCacheSerializer.Deserialize(_serializedValue))
            .MustHaveHappenedOnceExactly();
        }
        internal async Task <bool> TryServeFromCacheAsync(ResponseCachingContext context)
        {
            context.BaseKey = _keyProvider.CreateBaseKey(context);
            var cacheEntry = await _cache.GetAsync(context.BaseKey);

            var cachedVaryByRules = cacheEntry as CachedVaryByRules;

            if (cachedVaryByRules != null)
            {
                // Request contains vary rules, recompute key(s) and try again
                context.CachedVaryByRules = cachedVaryByRules;

                foreach (var varyKey in _keyProvider.CreateLookupVaryByKeys(context))
                {
                    if (await TryServeCachedResponseAsync(context, await _cache.GetAsync(varyKey)))
                    {
                        return(true);
                    }
                }
            }
            else
            {
                if (await TryServeCachedResponseAsync(context, cacheEntry))
                {
                    return(true);
                }
            }

            if (HeaderUtilities.ContainsCacheDirective(context.HttpContext.Request.Headers[HeaderNames.CacheControl], CacheControlHeaderValue.OnlyIfCachedString))
            {
                _logger.LogGatewayTimeoutServed();
                context.HttpContext.Response.StatusCode = StatusCodes.Status504GatewayTimeout;
                return(true);
            }

            _logger.LogNoResponseServed();
            return(false);
        }
        public async void CacheGetAsyncNullReturnsNull()
        {
            var result = await _distributedResponseCache.GetAsync(Key);

            Assert.Null(result);
        }