question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
esacacerescommented, Jul 16, 2018

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.

2reactions
bidadhcommented, Jun 9, 2019

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:

if(cachedResponse.isEmpty()) {
      return setPathFilter.filter(exchange.mutate().response(responseDecorator).build(), chain);
}

return chain.filter(exchange.mutate().response(cacheDecorator).build());

hope this helps

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found