The Google Guava Cache implementation of JSR107 (JCACHE)
Java
Latest commit c88136d Jun 7, 2016 Christian Bourque use new jsr107tck test implementation
Permalink
Failed to load latest commit information.
src use new jsr107tck test implementation Jun 7, 2016
.gitignore + ignore eclipse files Feb 14, 2016
.travis.yml first travis Feb 14, 2016
LICENSE Initial commit Feb 13, 2016
README.md Update README.md Jun 5, 2016
pom.xml next development iteration Jun 5, 2016

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.4-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 19.0 API)