Browse Source

Switch to Spring Boot String to Duration conversion.

pull/646/head
Olga Maciaszek-Sharma 5 years ago
parent
commit
1ddeca4a8e
  1. 2
      docs/src/main/asciidoc/spring-cloud-commons.adoc
  2. 4
      spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/cache/CaffeineBasedLoadBalancerCacheManager.java
  3. 15
      spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/cache/LoadBalancerCacheProperties.java

2
docs/src/main/asciidoc/spring-cloud-commons.adoc

@ -809,7 +809,7 @@ implementation. @@ -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]

4
spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/cache/CaffeineBasedLoadBalancerCacheManager.java vendored

@ -24,8 +24,8 @@ import org.springframework.util.StringUtils; @@ -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

15
spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/cache/LoadBalancerCacheProperties.java vendored

@ -34,8 +34,10 @@ public class LoadBalancerCacheProperties { @@ -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 { @@ -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 {

Loading…
Cancel
Save