K - Cache key.V - Cache value.public abstract class CaffeineCache<K,V> extends Object implements Jooby.Module
Cache services from Caffeine
App.java:
{
use(CaffeineCache.newCache());
get("/", req -> {
Cache cache = req.require(Cache.class);
// do with cache...
});
}
A default cache will be created, if you need to control the number of entries, expire policy,
etc... set the caffeine.cache property in application.conf, like:
caffeine.cache {
maximumSize = 10
}
or via CaffeineSpec syntax:
caffeine.cache = "maximumSize=10"
Just add entries to: caffeine., like:
# default cache (don't need a name on require calls) caffeine.cache = "maximumSize=10" caffeine.cache1 = "maximumSize=1" caffeine.cacheX = "maximumSize=100"
{
get("/", req -> {
Cache defcache = req.require(Cache.class);
Cache cache1 = req.require("cache1", Cache.class);
Cache cacheX = req.require("cacheX", Cache.class);
});
}
Type safe caches are provided by calling and creating a new CaffeineCache subclass:
{
// please notes the {} at the line of the next line
use(new CaffeineCache<Integer, String>() {});
}
Later, you can inject this cache in a type-safe manner:
@Inject
public MyService(Cache<Integer, String> cache) {
...
}
This module comes with a Session.Store implementation. In order to use it you need to
define a cache named session in your application.conf file:
caffeine.session = "maximumSize=10"And set the
CaffeineSessionStore:
{
session(CaffeineSessionStore.class);
}
You can access to the ```session``` via name:
{
get("/", req {
Cache cache = req.require("session", Cache.class);
});
}
CaffeineSessionStore| Modifier and Type | Class and Description |
|---|---|
static interface |
CaffeineCache.AsyncCallback<K,V> |
static interface |
CaffeineCache.Callback<K,V,C extends com.github.benmanes.caffeine.cache.Cache<K,V>> |
| Constructor and Description |
|---|
CaffeineCache()
Creates a new
CaffeineCache using caffeine as cache namespace. |
CaffeineCache(String name)
Creates a new
CaffeineCache using the provided namespace. |
| Modifier and Type | Method and Description |
|---|---|
void |
configure(Env env,
com.typesafe.config.Config conf,
com.google.inject.Binder binder) |
CaffeineCache<K,V> |
doWith(CaffeineCache.Callback<K,V,com.github.benmanes.caffeine.cache.Cache<K,V>> configurer)
Configure a cache builder and creates a new
Cache. |
CaffeineCache<K,V> |
doWithAsync(CaffeineCache.AsyncCallback<K,V> configurer)
Configure a cache builder and creates a new
AsyncLoadingCache. |
static CaffeineCache<String,Object> |
newCache()
|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitconfigpublic CaffeineCache(String name)
CaffeineCache using the provided namespace.name - Cache namespace.public CaffeineCache()
CaffeineCache using caffeine as cache namespace.public static CaffeineCache<String,Object> newCache()
CaffeineCache.public CaffeineCache<K,V> doWith(CaffeineCache.Callback<K,V,com.github.benmanes.caffeine.cache.Cache<K,V>> configurer)
Cache.configurer - Configurer callback.public CaffeineCache<K,V> doWithAsync(CaffeineCache.AsyncCallback<K,V> configurer)
AsyncLoadingCache.configurer - Configurer callback.public void configure(Env env, com.typesafe.config.Config conf, com.google.inject.Binder binder)
configure in interface Jooby.ModuleCopyright © 2019. All rights reserved.