From cf70d93e95d066f7bc7a849280e0e29b2488bacd Mon Sep 17 00:00:00 2001 From: Auri Munoz Date: Tue, 19 Dec 2023 14:43:04 +0100 Subject: [PATCH] Add custom expiration policy for cache snippet to the doc --- .../custom-service-discovery.md | 8 +++- ...mExpirationCachedAcmeServiceDiscovery.java | 44 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 docs/snippets/examples/CustomExpirationCachedAcmeServiceDiscovery.java diff --git a/docs/docs/service-discovery/custom-service-discovery.md b/docs/docs/service-discovery/custom-service-discovery.md index ee529b87..456315de 100644 --- a/docs/docs/service-discovery/custom-service-discovery.md +++ b/docs/docs/service-discovery/custom-service-discovery.md @@ -150,4 +150,10 @@ If you want to customize the expiration strategy, you need: 1. Implement the `cache` method where the expiration strategy should be defined. 2. Invalidate the cache when expiration condition evaluates to true. -Take a look to the [Kubernetes Service Discovery](kubernetes.md#Caching the service instances) for further details about this feature. +Look at the example bellow: + +```java linenums="1" +{{ insert('examples/CustomExpirationCachedAcmeServiceDiscovery.java') }} +``` + +Additionally, you can check the [Kubernetes Service Discovery](kubernetes.md#Caching the service instances) for further details about an event-based invalidation example. diff --git a/docs/snippets/examples/CustomExpirationCachedAcmeServiceDiscovery.java b/docs/snippets/examples/CustomExpirationCachedAcmeServiceDiscovery.java new file mode 100644 index 00000000..a754427e --- /dev/null +++ b/docs/snippets/examples/CustomExpirationCachedAcmeServiceDiscovery.java @@ -0,0 +1,44 @@ +package examples; + +import io.smallrye.mutiny.Uni; +import io.smallrye.stork.api.ServiceInstance; +import io.smallrye.stork.impl.CachingServiceDiscovery; +import io.smallrye.stork.impl.DefaultServiceInstance; +import io.smallrye.stork.utils.ServiceInstanceIds; + +import java.util.Collections; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; + +public class CustomExpirationCachedAcmeServiceDiscovery extends CachingServiceDiscovery { + + private final String host; + private final int port; + + private AtomicBoolean invalidated = new AtomicBoolean(); + + public CustomExpirationCachedAcmeServiceDiscovery(CachedAcmeConfiguration configuration) { + super(configuration.getRefreshPeriod()); + this.host = configuration.getHost(); + this.port = Integer.parseInt(configuration.getPort()); + } + + @Override + public Uni> fetchNewServiceInstances(List previousInstances) { + // Retrieve services... + DefaultServiceInstance instance = + new DefaultServiceInstance(ServiceInstanceIds.next(), host, port, false); + return Uni.createFrom().item(() -> Collections.singletonList(instance)); + } + + @Override + public Uni> cache(Uni> uni) { + return uni.memoize().until(() -> invalidated.get()); + } + + //command-based cache invalidation: user triggers the action to invalidate the cache. + public void invalidate() { + invalidated.set(true); + } + +} \ No newline at end of file