/// <summary>
        /// Try to get the cache from url and httpmethod and build the response if cache is available
        /// </summary>
        /// <param name="cacheControl"></param>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <HttpResponseMessage> HandleAsync(CacheControlHeaderValue cacheControl, HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (ShouldIgnoreCache(cacheControl, request))
            {
                return(null);
            }

            var cacheKey = request.GetUrlBaseCacheKey();

            foreach (var cacheStore in Global.CacheStoreProvider.AllAsyncCacheStores)
            {
                var originalKey = await cacheStore.GetAsync(cacheKey).ConfigureAwait(false) as string;

                if (originalKey == null)
                {
                    continue;
                }

                var cacheItem = await cacheStore.GetAsync(originalKey).ConfigureAwait(false) as WebApiCacheItem;

                if (cacheItem != null)
                {
                    return(_builder.GetResponse(cacheControl, cacheItem, request));
                }
            }
            return(null);
        }
示例#2
0
        /// <summary>
        /// Try to get the cache from etag and build the response if cache is available
        /// </summary>
        /// <param name="cacheControl"></param>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <HttpResponseMessage> HandleAsync(CacheControlHeaderValue cacheControl, HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (request.Headers.IfNoneMatch != null)
            {
                // Etag format: fw-StoreId-HashedKey-Checksum
                var requestEtags = request.Headers.IfNoneMatch.Where(t => t.Tag != null && t.Tag.StartsWith("\"fw-")).ToList();
                if (requestEtags.Count > 0)
                {
                    foreach (var etag in requestEtags)
                    {
                        var etagString    = etag.Tag.Trim('"');
                        var index         = etagString.IndexOf("-", 4, StringComparison.Ordinal);
                        var storeIdString = index > 0 ? etagString.Substring(3, index - 3) : "0";
                        index = etagString.LastIndexOf("-", StringComparison.Ordinal);

                        int storeId;
                        IAsyncCacheStore cacheStore = null;
                        if (int.TryParse(storeIdString, out storeId))
                        {
                            cacheStore = Global.CacheStoreProvider.GetAsyncCacheStore(storeId) ?? Global.CacheStoreProvider.GetAsyncCacheStore();
                        }

                        if (cacheStore != null && index > 0)
                        {
                            var hashedKey = etagString.Substring(0, index);
                            var checkSum  = etagString.Substring(index + 1);
                            var cacheItem = (await cacheStore.GetAsync(hashedKey)) as WebApiCacheItem;

                            if (cacheItem != null && cacheItem.Checksum == checkSum)
                            {
                                request.Properties[WebApiExtensions.__webApi_etag_matched] = true;
                            }
                            return(_builder.GetResponse(cacheControl, cacheItem, request));
                        }
                    }
                }
            }
            return(null);
        }