jdash.client.cache.GDCache Maven / Gradle / Ivy
package jdash.client.cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import jdash.client.request.GDRequest;
import jdash.client.request.GDRouter;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.UnaryOperator;
/**
* A cache allows to retrieve the response associated to a request that was already made previously without needing to
* pass the request to the {@link GDRouter}.
*/
public interface GDCache {
/**
* Creates a {@link GDCache} based on {@link Caffeine} implementation, allowing to customize things such as
* expiration times or maximum size.
*
* @param caffeineBuilder a function that allows to configure the caffeine cache
* @return a new {@link GDCache}
*/
static GDCache caffeine(UnaryOperator> caffeineBuilder) {
return new CaffeineCache(caffeineBuilder);
}
/**
* Creates a {@link GDCache} that does nothing.
*
* @return a new {@link GDCache}
*/
static GDCache disabled() {
return NoOpCache.INSTANCE;
}
/**
* Creates a {@link GDCache} that is backed by a simple {@link ConcurrentHashMap}, storing everything indefinitely
* in main memory. It is recommended to clear it regularly to avoid memory leaks when used in production.
*
* @return a new {@link GDCache}
*/
static GDCache inMemory() {
return new InMemoryCache();
}
/**
* Attempts to retrieve the object associated to the given request.
*
* @param request the request
* @return the object associated to the request, or an empty {@link Optional} if not found.
*/
Optional
© 2015 - 2025 Weber Informatics LLC | Privacy Policy