Skip to content
The Google Guava Cache implementation of JSR107 (JCACHE)
Java
Branch: master
Clone or download
Latest commit 272192a Sep 22, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore
.travis.yml
LICENSE
README.md
pom.xml

README.md

guava-jcache

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.

Build Status Maven Central License

Usage

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>

Example - Simple Cache

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");

Example - Loading Cache

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");

Documentation

javax.cache (JSR107 API and SPI 1.0.0 API)

com.google.common.cache (Guava: Google Core Libraries for Java 28.1-jre API)

You can’t perform that action at this time.