This WIP shall be a full implementation of the API and SPI from JSR-107 (aka JCache). It provides a wrapper around a Google Guava cache that allows allows you to use Guava as the caching provider using only JSR-107 APIs.
Development snapshots are available on Sonatype Nexus repository
<repositories>
<repository>
<id>sonatype-nexus-snapshots</id>
<name>Sonatype Nexus Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>ca.exprofesso</groupId>
<artifactId>guava-jcache</artifactId>
<version>1.0.5-SNAPSHOT</version>
</dependency>
</dependencies>
MutableConfiguration<String, Integer> configuration = new MutableConfiguration<>();
configuration.setStoreByValue(false);
configuration.setTypes(String.class, Integer.class);
CachingProvider cachingProvider = Caching.getCachingProvider(GuavaCachingProvider.class.getName());
CacheManager cacheManager = cachingProvider.getCacheManager();
Cache<String, Integer> cache = cacheManager.createCache("cache", configuration);
cache.put("key", 1);
Integer value = cache.get("key");
final CacheLoader<String, Integer> cacheLoader = new CacheLoader<String, Integer>()
{
@Override
public Integer load(String key)
throws CacheLoaderException
{
// in a real application the value would probably come from a database...
return Integer.valueOf(key);
}
@Override
public Map<String, Integer> loadAll(Iterable<? extends String> keys)
throws CacheLoaderException
{
Map<String, Integer> map = new HashMap<>();
for (String key : keys)
{
// in a real application the value would probably come from a database...
map.put(key, Integer.valueOf(key));
}
return map;
}
};
MutableConfiguration<String, Integer> configuration = new MutableConfiguration<>();
configuration.setStoreByValue(false);
configuration.setTypes(String.class, Integer.class);
custom.setReadThrough(true);
custom.setCacheLoaderFactory
(
new Factory<CacheLoader<String, Integer>>()
{
@Override
public CacheLoader<String, Integer> create()
{
return cacheLoader;
}
}
);
CachingProvider cachingProvider = Caching.getCachingProvider(GuavaCachingProvider.class.getName());
CacheManager cacheManager = cachingProvider.getCacheManager();
Cache<String, Integer> cache = cacheManager.createCache("cache", configuration);
Integer value = cache.get("key");
javax.cache (JSR107 API and SPI 1.0.0 API)
com.google.common.cache (Guava: Google Core Libraries for Java 28.1-jre API)