-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #739 from aureamunoz/custom-expiration-snippet
Add custom expiration policy for cache snippet to the doc
- Loading branch information
Showing
2 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
docs/snippets/examples/CustomExpirationCachedAcmeServiceDiscovery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<List<ServiceInstance>> fetchNewServiceInstances(List<ServiceInstance> previousInstances) { | ||
// Retrieve services... | ||
DefaultServiceInstance instance = | ||
new DefaultServiceInstance(ServiceInstanceIds.next(), host, port, false); | ||
return Uni.createFrom().item(() -> Collections.singletonList(instance)); | ||
} | ||
|
||
@Override | ||
public Uni<List<ServiceInstance>> cache(Uni<List<ServiceInstance>> 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); | ||
} | ||
|
||
} |