Switch to Spring Boot String to Duration conversion.

This commit is contained in:
Olga Maciaszek-Sharma
2019-11-22 16:49:28 +01:00
parent bf6b31ee8a
commit 1ddeca4a8e
3 changed files with 11 additions and 10 deletions

View File

@@ -809,7 +809,7 @@ implementation.
To make use of it, you need to have `com.github.ben-manes.caffeine:caffeine` in the classpath.
The default setup includes `expireAfterWrite` set to 30 seconds and records set to soft references.
You can set your own `TTL` value (the time after write after which entries should be expired), expressed as `Duration`, by passing a `String` compliant with the https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/Duration.html#parse(java.lang.CharSequence)[`Duration` API]
You can set your own `TTL` value (the time after write after which entries should be expired), expressed as `Duration`, by passing a `String` compliant with the https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-conversion-duration[Spring Boot `String` to `Duration` converter syntax].
as the value of `spring.cloud.loadbalancer.cache.ttl` property.
You can also override the default Caffeine Cache setup for the LoadBalancer by passing your own https://static.javadoc.io/com.github.ben-manes.caffeine/caffeine/2.2.2/com/github/benmanes/caffeine/cache/CaffeineSpec.html[Caffeine Specification]

View File

@@ -24,8 +24,8 @@ import org.springframework.util.StringUtils;
import static org.springframework.cloud.loadbalancer.core.CachingServiceInstanceListSupplier.SERVICE_INSTANCE_CACHE_NAME;
/**
* A Spring Cloud LoadBalancer specific implementation of {@link CaffeineCacheManager} that
* implements the {@link LoadBalancerCacheManager} marker interface.
* A Spring Cloud LoadBalancer specific implementation of {@link CaffeineCacheManager}
* that implements the {@link LoadBalancerCacheManager} marker interface.
*
* @author Olga Maciaszek-Sharma
* @since 2.2.0

View File

@@ -34,8 +34,10 @@ public class LoadBalancerCacheProperties {
/**
* Time To Live - time counted from writing of the record, after which cache entries
* are expired, expressed as a {@link Duration}. The property {@link String} has to be
* in keeping with the appropriate syntax as specified in
* {@link Duration#parse(CharSequence)}.
* in keeping with the appropriate syntax as specified in Spring Boot
* <code>StringToDurationConverter</code>.
* @see <a href=
* "https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/convert/StringToDurationConverter.java">StringToDurationConverter.java</a>
*/
private Duration ttl = Duration.ofSeconds(30);
@@ -51,14 +53,13 @@ public class LoadBalancerCacheProperties {
return ttl;
}
public void setTtl(String ttl) {
this.ttl = Duration.parse(ttl);
public void setTtl(Duration ttl) {
this.ttl = ttl;
}
/**
* Caffeine-specific LoadBalancer cache properties.
* NOTE: Passing your own Caffeine specification will override any other LoadBalancerCache settings,
* including TTL.
* Caffeine-specific LoadBalancer cache properties. NOTE: Passing your own Caffeine
* specification will override any other LoadBalancerCache settings, including TTL.
*/
public static class Caffeine {