CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-github-ben-manes-caffeine--jcache

JSR-107 JCache compatibility adapter for Caffeine caching library

Pending
Overview
Eval results
Files

cache-management.mddocs/

Cache Management

The Cache Management system provides lifecycle management for caches within a CacheManager, including creation, retrieval, destruction, and configuration. The CacheManagerImpl is the central component that manages multiple cache instances and their configurations.

Capabilities

CacheManagerImpl

JSR-107 CacheManager implementation that manages Caffeine-based caches with full lifecycle support and configuration management.

/**
 * An implementation of JSR-107 CacheManager that manages Caffeine-based caches.
 * Handles cache creation, retrieval, lifecycle, and management operations.
 */
public final class CacheManagerImpl implements CacheManager {
    
    /**
     * Create a new cache with the specified configuration
     * @param cacheName the name of the cache
     * @param configuration the cache configuration
     * @return newly created Cache instance
     * @throws CacheException if cache already exists or is externally configured
     * @throws IllegalArgumentException if arguments are null
     * @throws IllegalStateException if CacheManager is closed
     */
    public <K, V, C extends Configuration<K, V>> Cache<K, V> createCache(String cacheName, C configuration) throws IllegalArgumentException, CacheException, IllegalStateException;
    
    /**
     * Get an existing cache with type safety
     * @param cacheName the name of the cache
     * @param keyType the expected key type
     * @param valueType the expected value type
     * @return Cache instance or null if not found
     * @throws ClassCastException if types don't match
     * @throws IllegalArgumentException if arguments are null
     * @throws IllegalStateException if CacheManager is closed
     */
    public <K, V> @Nullable Cache<K, V> getCache(String cacheName, Class<K> keyType, Class<V> valueType) throws IllegalArgumentException, ClassCastException, IllegalStateException;
    
    /**
     * Get an existing cache without type checking
     * @param cacheName the name of the cache
     * @return CacheProxy instance or null if not found
     * @throws IllegalArgumentException if cacheName is null
     * @throws IllegalStateException if CacheManager is closed
     */
    public <K, V> CacheProxy<K, V> getCache(String cacheName) throws IllegalArgumentException, IllegalStateException;
    
    /**
     * Get all cache names managed by this CacheManager
     * @return unmodifiable collection of cache names
     * @throws IllegalStateException if CacheManager is closed
     */
    public Collection<String> getCacheNames() throws IllegalStateException;
    
    /**
     * Destroy and remove a cache by name
     * @param cacheName the name of the cache to destroy
     * @throws IllegalStateException if CacheManager is closed
     */
    public void destroyCache(String cacheName) throws IllegalStateException;
    
    /**
     * Enable or disable JMX management for a cache
     * @param cacheName the name of the cache
     * @param enabled true to enable management
     * @throws IllegalStateException if CacheManager is closed
     */
    public void enableManagement(String cacheName, boolean enabled) throws IllegalStateException;
    
    /**
     * Enable or disable statistics collection for a cache
     * @param cacheName the name of the cache
     * @param enabled true to enable statistics
     * @throws IllegalStateException if CacheManager is closed
     */
    public void enableStatistics(String cacheName, boolean enabled) throws IllegalStateException;
    
    /**
     * Get the CachingProvider that created this CacheManager
     * @return the associated CachingProvider
     */
    public CachingProvider getCachingProvider();
    
    /**
     * Get the URI that identifies this CacheManager
     * @return the CacheManager URI
     */
    public URI getURI();
    
    /**
     * Get the ClassLoader associated with this CacheManager
     * @return the ClassLoader (may be null if garbage collected)
     */
    public @Nullable ClassLoader getClassLoader();
    
    /**
     * Get the properties used to configure this CacheManager
     * @return the configuration properties
     */
    public Properties getProperties();
    
    /**
     * Close this CacheManager and all associated caches
     */
    public void close();
    
    /**
     * Check if this CacheManager is closed
     * @return true if closed
     */
    public boolean isClosed();
    
    /**
     * Unwrap this CacheManager to the specified type
     * @param clazz the class to unwrap to
     * @return unwrapped instance
     * @throws IllegalArgumentException if unwrapping is not supported
     */
    public <T> T unwrap(Class<T> clazz) throws IllegalArgumentException;
}

Usage Examples:

import javax.cache.Caching;
import javax.cache.CacheManager;
import javax.cache.Cache;
import javax.cache.configuration.MutableConfiguration;
import com.github.benmanes.caffeine.jcache.configuration.CaffeineConfiguration;

// Get CacheManager
CacheManager cacheManager = Caching.getCachingProvider().getCacheManager();

// Create cache with standard configuration
Cache<String, String> basicCache = cacheManager.createCache("basic", 
    new MutableConfiguration<String, String>()
        .setTypes(String.class, String.class));

// Create cache with Caffeine-specific configuration
Cache<String, Integer> advancedCache = cacheManager.createCache("advanced",
    new CaffeineConfiguration<String, Integer>()
        .setTypes(String.class, Integer.class)
        .setMaximumSize(OptionalLong.of(1000))
        .setExpireAfterWrite(OptionalLong.of(Duration.ofMinutes(30).toNanos()))
        .setStatisticsEnabled(true));

// Retrieve existing caches
Cache<String, String> retrieved = cacheManager.getCache("basic", String.class, String.class);
Cache<Object, Object> untyped = cacheManager.getCache("basic");

// Manage cache lifecycle
Collection<String> cacheNames = cacheManager.getCacheNames();
cacheManager.enableStatistics("advanced", true);
cacheManager.enableManagement("advanced", true);

// Cleanup
cacheManager.destroyCache("basic");
cacheManager.close();

Cache Factory Integration

The CacheManager works with the internal CacheFactory to create cache instances from configurations:

// Internal factory methods (package-private)
static CacheProxy<?, ?> createCache(CacheManager cacheManager, String cacheName, Configuration<?, ?> configuration);
static CacheProxy<?, ?> tryToCreateFromExternalSettings(CacheManager cacheManager, String cacheName);
static boolean isDefinedExternally(CacheManager cacheManager, String cacheName);

External Configuration Support

The CacheManager supports external configuration through:

  • Typesafe Config: Configuration files can define cache settings
  • Properties: Runtime properties for cache behavior
  • Environment Variables: System-level configuration
// Example with external configuration
Properties props = new Properties();
props.setProperty("cache.myCache.maximumSize", "1000");
props.setProperty("cache.myCache.expireAfterWrite", "PT30M");

CacheManager manager = Caching.getCachingProvider()
    .getCacheManager(URI.create("custom://config"), 
                    getClass().getClassLoader(), 
                    props);

OSGi Support

The CacheManager includes special handling for OSGi environments:

  • ClassLoader Management: Proper context ClassLoader handling
  • Component Lifecycle: Integration with OSGi component lifecycle
  • Dynamic Configuration: Support for runtime configuration changes
// OSGi-aware cache creation (internal handling)
// Automatically detects OSGi environment and adjusts ClassLoader behavior
Cache<String, String> osgiCache = cacheManager.createCache("osgi-cache", configuration);

Error Handling

Common exceptions thrown by CacheManager operations:

  • CacheException: When cache already exists or configuration conflicts
  • ClassCastException: When retrieved cache types don't match expected types
  • IllegalStateException: When operations are performed on closed CacheManager
  • IllegalArgumentException: When unwrapping to unsupported types

Install with Tessl CLI

npx tessl i tessl/maven-com-github-ben-manes-caffeine--jcache

docs

cache-management.md

cache-operations.md

configuration.md

events.md

index.md

integration.md

management.md

spi.md

tile.json