Traffic on my site can get heavy and I would like to slow down how frequently I run an expensive cleanup function; I would also like to run it in the background.
I implemented a cache with a removalListener and expected it to run asynchronously 30 seconds after an entry was created, but I'm seeing it either not run at all or run instantly. I also am having to wait for the cleanup script when it does run. Am I misunderstanding the purpose of a cache and removal listener?
@Path("my-endpoint")
public class MyResource{
private static final Cache<String, String> debounceCleaner = CacheBuilder
.newBuilder()
.maximumSize(1000)
.expireAfterWrite(30, TimeUnit.SECONDS)
.removalListener((RemovalListener<String, String>) x-> new Cleaner().clean())
.build();
@PUT
public Response update(){
...
try{
debounceCleaner.get("foo", ()->"bar");
} catch (ExecutionException ex){
throw new IllegalStateException("Unhandled", ex);
}
return Response(...)