Beispiel #1
0
        protected async Task <BackendResponse> GetFromCacheThenFromBackendAsync(
            string format,
            IRestClient restClient,
            Func <IRestRequest> createBackendRequestFunc,
            string cacheKey,
            RequestHeaders requestHeaders,
            Action <HttpStatusCode> handleNotOkResponseAction,
            CancellationToken cancellationToken)
        {
            var acceptType = requestHeaders.DetermineAcceptType(format);

            if (_redis != null)
            {
                var key = $"{cacheKey}.{acceptType.ToString()}".ToLowerInvariant();

                try
                {
                    var db = _redis.GetDatabase();

                    var cachedValues =
                        await db.HashGetAllAsync(
                            key,
                            CommandFlags.PreferSlave);

                    if (cachedValues.Length > 0)
                    {
                        var cachedValue        = cachedValues.FirstOrDefault(x => x.Name.Equals(ValueKey));
                        var cachedHeaders      = cachedValues.FirstOrDefault(x => x.Name.Equals(HeadersKey));
                        var cachedLastModified = cachedValues.FirstOrDefault(x => x.Name.Equals(LastModifiedKey));

                        var headers = JsonConvert.DeserializeObject <Dictionary <string, string[]> >(cachedHeaders.Value);
                        headers.TryGetValue(AddVersionHeaderMiddleware.HeaderName, out var downstreamVersion);

                        return(new BackendResponse(
                                   cachedValue.Value,
                                   downstreamVersion?.First(),
                                   DateTimeOffset.ParseExact(
                                       cachedLastModified.Value,
                                       "O",
                                       CultureInfo.InvariantCulture),
                                   acceptType.ToMimeTypeString(),
                                   true));
                    }

                    _logger.LogError("Failed to retrieve record {Record} from Redis, no cached values.", key);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Failed to retrieve record {Record} from Redis.", key);
                }
            }

            return(await GetFromBackendAsync(
                       restClient,
                       createBackendRequestFunc,
                       acceptType,
                       handleNotOkResponseAction,
                       cancellationToken));
        }
 protected async Task <BackendResponse> GetFromBackendWithBadRequestAsync(
     string format,
     Func <IRestRequest> createBackendRequestFunc,
     RequestHeaders requestHeaders,
     Action <HttpStatusCode> handleNotOkResponseAction,
     CancellationToken cancellationToken)
 => await GetFromBackendWithBadRequestAsync(
     _restClient,
     createBackendRequestFunc,
     requestHeaders.DetermineAcceptType(format),
     handleNotOkResponseAction,
     cancellationToken);