JSR-107 JCache compatibility adapter for Caffeine caching library
—
The Caffeine JCache Service Provider Interface (SPI) provides the entry point for JSR-107 cache discovery and CacheManager creation. The CaffeineCachingProvider is automatically discovered through the Java ServiceLoader mechanism when using Caching.getCachingProvider().
Main SPI implementation that provides JCache CachingProvider backed by Caffeine. Manages CacheManager instances and their lifecycles.
/**
* A provider that produces a JCache implementation backed by Caffeine.
* Typically instantiated using Caching.getCachingProvider() which discovers
* this implementation through ServiceLoader.
*/
public final class CaffeineCachingProvider implements CachingProvider {
/**
* Get the default cache manager using default URI, ClassLoader, and Properties
* @return CacheManager instance
*/
public CacheManager getCacheManager();
/**
* Get cache manager with specific URI and ClassLoader, using default Properties
* @param uri the URI to identify the cache manager
* @param classLoader the ClassLoader for the cache manager
* @return CacheManager instance
*/
public CacheManager getCacheManager(URI uri, ClassLoader classLoader);
/**
* Get cache manager with full configuration
* @param uri the URI to identify the cache manager
* @param classLoader the ClassLoader for the cache manager
* @param properties configuration properties
* @return CacheManager instance
*/
public CacheManager getCacheManager(URI uri, ClassLoader classLoader, Properties properties);
/**
* Close all cache managers managed by this provider
*/
public void close();
/**
* Close all cache managers associated with the specified ClassLoader
* @param classLoader the ClassLoader to close cache managers for
*/
public void close(ClassLoader classLoader);
/**
* Close the specific cache manager identified by URI and ClassLoader
* @param uri the URI identifying the cache manager
* @param classLoader the ClassLoader of the cache manager
*/
public void close(URI uri, ClassLoader classLoader);
/**
* Check if the specified optional feature is supported
* @param optionalFeature the feature to check
* @return true if supported (only STORE_BY_REFERENCE is supported)
*/
public boolean isSupported(OptionalFeature optionalFeature);
/**
* Get the default URI for this provider
* @return URI based on the provider class name
*/
public URI getDefaultURI();
/**
* Get the default ClassLoader for this provider
* @return ClassLoader that combines context and class ClassLoaders
*/
public ClassLoader getDefaultClassLoader();
/**
* Get the default properties for this provider
* @return empty Properties instance
*/
public Properties getDefaultProperties();
}Usage Examples:
import javax.cache.Caching;
import javax.cache.CacheManager;
import com.github.benmanes.caffeine.jcache.spi.CaffeineCachingProvider;
// Automatic discovery through ServiceLoader
CacheManager cacheManager = Caching.getCachingProvider().getCacheManager();
// Direct instantiation
CaffeineCachingProvider provider = new CaffeineCachingProvider();
CacheManager manager = provider.getCacheManager();
// With custom URI and ClassLoader
URI customUri = URI.create("custom://cache-config");
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
CacheManager customManager = provider.getCacheManager(customUri, classLoader);
// With properties
Properties props = new Properties();
props.setProperty("custom.property", "value");
CacheManager configuredManager = provider.getCacheManager(customUri, classLoader, props);
// Cleanup
provider.close();The Caffeine JCache implementation supports the following JSR-107 optional features:
// Only STORE_BY_REFERENCE is supported
OptionalFeature.STORE_BY_REFERENCE // true
OptionalFeature.STORE_BY_VALUE // false (but can be enabled via configuration)The provider is automatically registered through the ServiceLoader mechanism via:
META-INF/services/javax.cache.spi.CachingProvidercom.github.benmanes.caffeine.jcache.spi.CaffeineCachingProviderThis allows automatic discovery when using:
// Discovers CaffeineCachingProvider automatically
CachingProvider provider = Caching.getCachingProvider();The provider includes a custom JCacheClassLoader that combines multiple ClassLoader sources:
This ensures proper class resolution in various deployment environments including OSGi containers.
Install with Tessl CLI
npx tessl i tessl/maven-com-github-ben-manes-caffeine--jcache