Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce caching for functional routing #29985

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.web.reactive.function.server;

import org.springframework.core.io.Resource;
import org.springframework.http.CacheControl;

/**
* Default lookup that performs no caching.
* @author Jakob Fels
*/
public class DefaultResourceCacheLookupStrategy implements ResourceCacheLookupStrategy {

@Override
public CacheControl lookupCacheControl(Resource resource) {
return CacheControl.empty();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.web.reactive.function.server;

import org.springframework.core.io.Resource;
import org.springframework.http.CacheControl;

/**
* Strategy interface to allow for looking up cache control for a given resource.
*
* @author Jakob Fels
*/
public interface ResourceCacheLookupStrategy {


static ResourceCacheLookupStrategy noCaching() {
return new DefaultResourceCacheLookupStrategy();
}

CacheControl lookupCacheControl(Resource resource);

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import reactor.core.publisher.Mono;

import org.springframework.core.io.Resource;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.lang.Nullable;
Expand All @@ -45,23 +46,35 @@ class ResourceHandlerFunction implements HandlerFunction<ServerResponse> {


private final Resource resource;
private final CacheControl cacheControl;


public ResourceHandlerFunction(Resource resource) {
this.resource = resource;
this.cacheControl = CacheControl.empty();
}


public ResourceHandlerFunction(Resource resource, ResourceCacheLookupStrategy strategy) {
this.resource = resource;
this.cacheControl = strategy.lookupCacheControl(resource);
}


@Override
public Mono<ServerResponse> handle(ServerRequest request) {
HttpMethod method = request.method();
if (HttpMethod.GET.equals(method)) {
return EntityResponse.fromObject(this.resource).build()
return EntityResponse.fromObject(this.resource)
.cacheControl(this.cacheControl)
.build()
.map(response -> response);
}
else if (HttpMethod.HEAD.equals(method)) {
Resource headResource = new HeadMethodResource(this.resource);
return EntityResponse.fromObject(headResource).build()
return EntityResponse.fromObject(headResource)
.cacheControl(this.cacheControl)
.build()
.map(response -> response);
}
else if (HttpMethod.OPTIONS.equals(method)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,21 @@ public RouterFunctions.Builder resources(String pattern, Resource location) {
return add(RouterFunctions.resources(pattern, location));
}

@Override
public RouterFunctions.Builder resources(String pattern, Resource location, ResourceCacheLookupStrategy resourceCacheLookupStrategy) {
return add(RouterFunctions.resources(pattern,location,resourceCacheLookupStrategy));
}

@Override
public RouterFunctions.Builder resources(Function<ServerRequest, Mono<Resource>> lookupFunction) {
return add(RouterFunctions.resources(lookupFunction));
}

@Override
public RouterFunctions.Builder resources(Function<ServerRequest, Mono<Resource>> lookupFunction, ResourceCacheLookupStrategy resourceCacheLookupStrategy) {
return add(RouterFunctions.resources(lookupFunction,resourceCacheLookupStrategy));
}

@Override
public RouterFunctions.Builder nest(RequestPredicate predicate,
Consumer<RouterFunctions.Builder> builderConsumer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ public static <T extends ServerResponse> RouterFunction<T> nest(
* @see #resourceLookupFunction(String, Resource)
*/
public static RouterFunction<ServerResponse> resources(String pattern, Resource location) {
return resources(resourceLookupFunction(pattern, location));
return resources(resourceLookupFunction(pattern, location), ResourceCacheLookupStrategy.noCaching());
}
public static RouterFunction<ServerResponse> resources(String pattern, Resource location, ResourceCacheLookupStrategy lookupStrategy) {
return resources(resourceLookupFunction(pattern, location), lookupStrategy);
}

/**
Expand Down Expand Up @@ -186,7 +189,10 @@ public static Function<ServerRequest, Mono<Resource>> resourceLookupFunction(Str
* @return a router function that routes to resources
*/
public static RouterFunction<ServerResponse> resources(Function<ServerRequest, Mono<Resource>> lookupFunction) {
return new ResourcesRouterFunction(lookupFunction);
return new ResourcesRouterFunction(lookupFunction, ResourceCacheLookupStrategy.noCaching());
}
public static RouterFunction<ServerResponse> resources(Function<ServerRequest, Mono<Resource>> lookupFunction, ResourceCacheLookupStrategy lookupStrategy) {
return new ResourcesRouterFunction(lookupFunction, lookupStrategy);
}

/**
Expand Down Expand Up @@ -652,6 +658,7 @@ public interface Builder {
* @return this builder
*/
Builder resources(String pattern, Resource location);
Builder resources(String pattern, Resource location, ResourceCacheLookupStrategy resourceCacheLookupStrategy);

/**
* Route to resources using the provided lookup function. If the lookup function provides a
Expand All @@ -661,6 +668,7 @@ public interface Builder {
* @return this builder
*/
Builder resources(Function<ServerRequest, Mono<Resource>> lookupFunction);
Builder resources(Function<ServerRequest, Mono<Resource>> lookupFunction, ResourceCacheLookupStrategy resourceCacheLookupStrategy);

/**
* Route to the supplied router function if the given request predicate applies. This method
Expand Down Expand Up @@ -1142,14 +1150,22 @@ private static class ResourcesRouterFunction extends AbstractRouterFunction<Ser

private final Function<ServerRequest, Mono<Resource>> lookupFunction;

private final ResourceCacheLookupStrategy lookupStrategy;

public ResourcesRouterFunction(Function<ServerRequest, Mono<Resource>> lookupFunction) {
this(lookupFunction, ResourceCacheLookupStrategy.noCaching());
}

public ResourcesRouterFunction(Function<ServerRequest, Mono<Resource>> lookupFunction, ResourceCacheLookupStrategy lookupStrategy) {
Assert.notNull(lookupFunction, "Function must not be null");
Assert.notNull(lookupStrategy, "Strategy must not be null");
this.lookupFunction = lookupFunction;
this.lookupStrategy = lookupStrategy;
}

@Override
public Mono<HandlerFunction<ServerResponse>> route(ServerRequest request) {
return this.lookupFunction.apply(request).map(ResourceHandlerFunction::new);
return this.lookupFunction.apply(request).map(resource -> new ResourceHandlerFunction(resource, this.lookupStrategy));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.web.reactive.function.server;

import java.io.IOException;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand All @@ -28,6 +29,8 @@

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -130,6 +133,28 @@ public void resources() {
.verifyComplete();
}

@Test
public void resourcesCaching() {
Resource resource = new ClassPathResource("/org/springframework/web/reactive/function/server/");
assertThat(resource.exists()).isTrue();

RouterFunction<ServerResponse> route = RouterFunctions.route()
.resources("/resources/**", resource, resource1 -> CacheControl.maxAge(Duration.ofSeconds(60)))
.build();

MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://localhost/resources/response.txt").build();
ServerRequest resourceRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());

Mono<String> responseMono = route.route(resourceRequest)
.flatMap(handlerFunction -> handlerFunction.handle(resourceRequest))
.map(ServerResponse::headers)
.mapNotNull(HttpHeaders::getCacheControl);

StepVerifier.create(responseMono)
.expectNext("max-age=60")
.verifyComplete();
}

@Test
public void nest() {
RouterFunction<?> route = RouterFunctions.route()
Expand Down