Hello guys,
Is possible to add a filter to do caching based in a request and parameters?
- If the request is cached then return the response. The response can be returned with exchange.getResponse().setComplete() in the filter but I found problems to modify the response body with my cached data.
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("testcache",
route -> route
.path("/testcache")
.filters(conf -> conf.filter(new CacheFilter()))
.uri("http://myurl.com")).build();
}
public class CacheFilter implements GatewayFilter {
private static final Log log = LogFactory.getLog(CacheFilter.class);
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//ServerHttpRequest request = exchange.getRequest();
//if (Cache.exists(request.getBody(),request.getPath(),request.getQueryParams())) {
//String reponseBody = Cache.get(request.getBody(),request.getPath(),request.getQueryParams());
String reponseBody = "test cache";
// The response is not mutated?
exchange.mutate().response(changeResponseBody(exchange,reponseBody)).build();
exchange.getResponse().setStatusCode(HttpStatus.OK);
return exchange.getResponse().setComplete();
//}
//return chain.filter(exchange);
}
public ServerHttpResponseDecorator changeResponseBody (ServerWebExchange exchange, String reponseBody) {
ServerHttpResponse originalResponse = exchange.getResponse();
ServerHttpResponseDecorator responseDecorator = new ServerHttpResponseDecorator (originalResponse);
byte[] bytes = reponseBody.getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = exchange.getResponse().bufferFactory().wrap(bytes);
responseDecorator.writeWith(Flux.just(buffer));
return responseDecorator;
}
}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Welcome Cache Customers | Versona
As a valued Cache customer, we invite you to shop our exclusive boutiques offering unique fashion clothing, jewelry and accessories. Boutique styles you...
Read more >Cache Definition & Meaning - Merriam-Webster
Cache primarily refers to a thing that is hidden or stored somewhere, or to the place where it is hidden. It has recently...
Read more >Cache (computing) - Wikipedia
In computing, a cache is a hardware or software component that stores data so that future requests for that data can be served...
Read more >What is Cache (Computing)? - TechTarget
A cache -- pronounced CASH -- is hardware or software that is used to store something, usually data, temporarily in a computing environment....
Read more >Cache Definition & Meaning - Dictionary.com
Cache definition, a hiding place, especially one in the ground, for ammunition, food, treasures, etc.: She hid her jewelry in a little cache...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The main problem with cache is that is a blocking operation. Since, Spring Cloud Gateway is based Reactor project it needs a cache non-blocking (see RxCache). Currently there is not an official support.
I created 2
ServerHttpResponseDecorator
instances one to do the actual job and another one to use cached data and it works.I need to refactor and clean up the code but I’ll paste a part of it here:
hope this helps