Skip to content

Commit 2365273

Browse files
committed
cache only if http status is OK
1 parent e51e19f commit 2365273

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,19 @@ public async Task Invoke(DownstreamContext context)
6464
return;
6565
}
6666

67-
cached = await CreateCachedResponse(context.DownstreamResponse);
67+
if (context.DownstreamResponse.StatusCode == System.Net.HttpStatusCode.OK)
68+
{
69+
cached = await CreateCachedResponse(context.DownstreamResponse);
6870

69-
_outputCache.Add(downStreamRequestCacheKey, cached, TimeSpan.FromSeconds(context.DownstreamReRoute.CacheOptions.TtlSeconds), context.DownstreamReRoute.CacheOptions.Region);
71+
_outputCache.Add(downStreamRequestCacheKey, cached, TimeSpan.FromSeconds(context.DownstreamReRoute.CacheOptions.TtlSeconds), context.DownstreamReRoute.CacheOptions.Region);
7072

71-
Logger.LogDebug($"finished response added to cache for {downstreamUrlKey}");
73+
Logger.LogDebug($"finished response added to cache for {downstreamUrlKey}");
74+
}
75+
else
76+
{
77+
78+
Logger.LogDebug($"http request failed, could not create cache for {downstreamUrlKey}");
79+
}
7280
}
7381

7482
private void SetHttpResponseMessageThisRequest(DownstreamContext context,

0 commit comments

Comments
 (0)