Skip to content

Commit 9153aa5

Browse files
committed
Added method which check is this request must be cached.
1 parent 2322c25 commit 9153aa5

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

graphql-java-servlet/src/main/java/graphql/kickstart/servlet/BatchedQueryResponseWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public void write(HttpServletRequest request, HttpServletResponse response, Grap
4242
String responseContent = responseBuilder.toString();
4343
byte[] contentBytes = responseContent.getBytes(StandardCharsets.UTF_8);
4444

45-
if (responseCache != null) {
45+
if (responseCache != null && responseCache.isCacheable(request, invocationInput)) {
4646
try {
47-
responseCache.cacheResponse(request, invocationInput, CachedResponse.ofContent(contentBytes));
47+
responseCache.put(request, invocationInput, CachedResponse.ofContent(contentBytes));
4848
} catch (Throwable t) {
4949
log.warn(t.getMessage(), t);
5050
log.warn("Ignore read from cache, unexpected error happened");

graphql-java-servlet/src/main/java/graphql/kickstart/servlet/ErrorQueryResponseWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class ErrorQueryResponseWriter implements QueryResponseWriter {
2020

2121
@Override
2222
public void write(HttpServletRequest request, HttpServletResponse response, GraphQLResponseCache responseCache) throws IOException {
23-
if (responseCache != null) {
23+
if (responseCache != null && responseCache.isCacheable(request, invocationInput)) {
2424
try {
25-
responseCache.cacheResponse(request, invocationInput, CachedResponse.ofError(statusCode, message));
25+
responseCache.put(request, invocationInput, CachedResponse.ofError(statusCode, message));
2626
} catch (Throwable t) {
2727
log.warn(t.getMessage(), t);
2828
log.warn("Ignore read from cache, unexpected error happened");

graphql-java-servlet/src/main/java/graphql/kickstart/servlet/SingleQueryResponseWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public void write(HttpServletRequest request, HttpServletResponse response, Grap
2929
String responseContent = graphQLObjectMapper.serializeResultAsJson(result);
3030
byte[] contentBytes = responseContent.getBytes(StandardCharsets.UTF_8);
3131

32-
if (responseCache != null) {
32+
if (responseCache != null && responseCache.isCacheable(request, invocationInput)) {
3333
try {
34-
responseCache.cacheResponse(request, invocationInput, CachedResponse.ofContent(contentBytes));
34+
responseCache.put(request, invocationInput, CachedResponse.ofContent(contentBytes));
3535
} catch (Throwable t) {
3636
log.warn(t.getMessage(), t);
3737
log.warn("Ignore read from cache, unexpected error happened");

graphql-java-servlet/src/main/java/graphql/kickstart/servlet/cache/GraphQLResponseCache.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,23 @@ public interface GraphQLResponseCache {
1414
* @param invocationInput input data
1515
* @return cached response if something available in cache or {@literal null} if nothing cached
1616
*/
17-
CachedResponse getCachedResponse(HttpServletRequest request, GraphQLInvocationInput invocationInput);
17+
CachedResponse get(HttpServletRequest request, GraphQLInvocationInput invocationInput);
1818

1919
/**
2020
* Decide to cache or not this response. It depends on the implementation.
2121
*
2222
* @param request the http request
2323
* @param invocationInput input data
24+
*/
25+
boolean isCacheable(HttpServletRequest request, GraphQLInvocationInput invocationInput);
26+
27+
/**
28+
* Cache this response. It depends on the implementation.
29+
*
30+
* @param request the http request
31+
* @param invocationInput input data
2432
* @param cachedResponse response to cache
2533
*/
26-
void cacheResponse(HttpServletRequest request, GraphQLInvocationInput invocationInput, CachedResponse cachedResponse);
34+
void put(HttpServletRequest request, GraphQLInvocationInput invocationInput, CachedResponse cachedResponse);
2735

2836
}

0 commit comments

Comments
 (0)