Class JCacheXCacheManager
- java.lang.Object
-
- io.github.dhruv1110.jcachex.spring.core.JCacheXCacheManager
-
- All Implemented Interfaces:
org.springframework.cache.CacheManager
public class JCacheXCacheManager extends Object implements org.springframework.cache.CacheManager
Spring CacheManager implementation that integrates JCacheX with Spring's caching abstraction. This cache manager provides seamless integration between JCacheX high-performance caching and Spring's declarative caching support (@Cacheable, @CacheEvict, etc.). It supports all JCacheX features while maintaining compatibility with Spring's Cache interface.Key Features:
- Full JCacheX Integration: Access to all eviction strategies, async operations, and metrics
- Spring Compatibility: Works with @Cacheable, @CacheEvict, @CachePut annotations
- Dynamic Cache Creation: Automatically creates caches on demand with default configuration
- Configuration-Driven: Respects JCacheXProperties for cache-specific settings
- Thread-Safe: Fully thread-safe for concurrent access in multi-threaded environments
- Graceful Fallback: Handles missing caches and configuration errors gracefully
Usage Examples:
Basic Configuration:
{ @code @Configuration @EnableCaching public class CacheConfig {
- Since:
- 1.0.0
- See Also:
JCacheXProperties
,JCacheXSpringCache
,Cache
-
-
Constructor Summary
Constructors Constructor Description JCacheXCacheManager()
Creates a new cache manager with default properties.JCacheXCacheManager(JCacheXProperties properties)
Creates a new cache manager with the specified properties.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clearAllCaches()
Clears all caches managed by this cache manager.Map<String,io.github.dhruv1110.jcachex.CacheStats>
getAllCacheStats()
Gets statistics for all managed caches.org.springframework.cache.Cache
getCache(String name)
Retrieves a cache by name, creating it if necessary and dynamic creation is enabled.Collection<String>
getCacheNames()
Returns the names of all caches managed by this cache manager.io.github.dhruv1110.jcachex.Cache<Object,Object>
getNativeCache(String name)
Gets the underlying JCacheX cache instance for advanced operations.JCacheXProperties
getProperties()
Gets the properties used by this cache manager.long
getTotalSize()
Gets the total size of all caches.void
initializeCaches()
Initializes caches based on configuration properties.boolean
isAllowNullValues()
Gets whether null values are allowed in caches.boolean
isDynamic()
Gets whether caches are created dynamically.void
registerCache(String name, io.github.dhruv1110.jcachex.Cache<Object,Object> cache)
Registers a pre-built JCacheX cache instance with this manager under the given name.JCacheXSpringCache
removeCache(String cacheName)
Removes a cache from this cache manager.void
setAllowNullValues(boolean allowNullValues)
Sets whether to allow null values in caches.void
setCacheNames(Collection<String> cacheNames)
Pre-creates caches with the specified names using default configuration.void
setDynamic(boolean dynamic)
Sets whether to create caches dynamically when requested.String
toString()
-
-
-
Constructor Detail
-
JCacheXCacheManager
public JCacheXCacheManager()
Creates a new cache manager with default properties.
-
JCacheXCacheManager
public JCacheXCacheManager(JCacheXProperties properties)
Creates a new cache manager with the specified properties.- Parameters:
properties
- the JCacheX properties for configuration
-
-
Method Detail
-
getCache
@Nullable public org.springframework.cache.Cache getCache(String name)
Retrieves a cache by name, creating it if necessary and dynamic creation is enabled.- Specified by:
getCache
in interfaceorg.springframework.cache.CacheManager
- Parameters:
name
- the cache name- Returns:
- the Cache instance, or null if not found and dynamic creation is disabled
-
getCacheNames
public Collection<String> getCacheNames()
Returns the names of all caches managed by this cache manager.- Specified by:
getCacheNames
in interfaceorg.springframework.cache.CacheManager
- Returns:
- collection of cache names
-
setAllowNullValues
public void setAllowNullValues(boolean allowNullValues)
Sets whether to allow null values in caches. When disabled, caching null values will result in exceptions. When enabled (default), null values are cached and returned as null.- Parameters:
allowNullValues
- whether to allow null values
-
isAllowNullValues
public boolean isAllowNullValues()
Gets whether null values are allowed in caches.- Returns:
- true if null values are allowed
-
setDynamic
public void setDynamic(boolean dynamic)
Sets whether to create caches dynamically when requested. When enabled (default), unknown caches are created automatically with default configuration. When disabled, only pre-configured caches are available.- Parameters:
dynamic
- whether to create caches dynamically
-
isDynamic
public boolean isDynamic()
Gets whether caches are created dynamically.- Returns:
- true if dynamic cache creation is enabled
-
setCacheNames
public void setCacheNames(Collection<String> cacheNames)
Pre-creates caches with the specified names using default configuration. This is useful when you want to ensure caches exist at startup or when dynamic cache creation is disabled.- Parameters:
cacheNames
- the names of caches to create
-
getNativeCache
@Nullable public io.github.dhruv1110.jcachex.Cache<Object,Object> getNativeCache(String name)
Gets the underlying JCacheX cache instance for advanced operations. This provides direct access to JCacheX-specific features like async operations, detailed statistics, and advanced configuration options.- Parameters:
name
- the cache name- Returns:
- the JCacheX cache instance, or null if not found
-
registerCache
public void registerCache(String name, io.github.dhruv1110.jcachex.Cache<Object,Object> cache)
Registers a pre-built JCacheX cache instance with this manager under the given name. If a cache with the same name exists it will be replaced.- Parameters:
name
- cache namecache
- native JCacheX cache
-
initializeCaches
public void initializeCaches()
Initializes caches based on configuration properties. This method is called automatically by the auto-configuration to create all caches defined in the properties file.
-
getProperties
public JCacheXProperties getProperties()
Gets the properties used by this cache manager.- Returns:
- the JCacheX properties
-
getAllCacheStats
public Map<String,io.github.dhruv1110.jcachex.CacheStats> getAllCacheStats()
Gets statistics for all managed caches.- Returns:
- map of cache names to their statistics
-
clearAllCaches
public void clearAllCaches()
Clears all caches managed by this cache manager.
-
removeCache
@Nullable public JCacheXSpringCache removeCache(String cacheName)
Removes a cache from this cache manager.- Parameters:
cacheName
- the name of the cache to remove- Returns:
- the removed cache, or null if not found
-
getTotalSize
public long getTotalSize()
Gets the total size of all caches.- Returns:
- the total number of entries across all caches
-
-