Description
I am facing the same need of Issue #29542 to set custom object into Properties
object that are passed into the CachingProvider.getCacheManager(URI, ClassLoader, Properties)
call.
Specifically, I am setting up Infinispan with Spring Boot via the JCache method, as opposed to using infinispan-spring-boot3-starter-embedded
or using InfinispanCacheConfiguration
. Based on the Infinispan's JCacheManager
documentation, it is possible to customize the global configuration and cache configuration using Java code. This can be achieved by passing in customizer function to the Properties
object with GLOBAL_CONFIGURATION_CONSUMER
and CACHE_CONFIGURATION_FUNCTION
keys, and these functions will get called during CachingProvider.getCacheManager(URI, ClassLoader, Properties)
and CacheManager.create(String, C)
call respectively.
Here is an example configuration class demostrating the use of GLOBAL_CONFIGURATION_CONSUMER
and CACHE_CONFIGURATION_FUNCTION
keys:
@ConfigurationProperties(prefix = "mysystem.cache")
data class CacheConfigProperty(
val dir: Path,
val expire: Duration
)
@Configuration(proxyBeanMethods = false)
class MyCacheConfig {
@Bean
fun jCacheManager(
cacheConfigProperty: CacheConfigProperty,
): CacheManager {
val properties = System.getProperties()
properties[JCacheManager.GLOBAL_CONFIGURATION_CONSUMER] = Consumer<GlobalConfigurationBuilder> {
it.globalState().enable()
.persistentLocation(cacheConfigProperty.dir.toString())
}
properties[JCacheManager.CACHE_CONFIGURATION_FUNCTION] = Function<String, org.infinispan.configuration.cache.Configuration> {
return@Function if (it == "mylists") {
ConfigurationBuilder()
.persistence().passivation(false)
.addSoftIndexFileStore()
.shared(false)
.preload(true)
.expiration().lifespan(cacheConfigProperty.expire.toMillis(), TimeUnit.MILLISECONDS)
.build()
} else {
null
}
}
val cacheManager = Caching.getCachingProvider("org.infinispan.jcache.embedded.JCachingProvider")
.getCacheManager(null, null, properties)
cacheManager.createCache("mylists", MutableConfiguration<SimpleKey, String>())
return cacheManager
}
}
However, I would prefer to make use of JCacheCacheConfiguration
class so that I can replace the cacheManager.createCache("mylists", MutableConfiguration<SimpleKey, String>())
call with the spring boot property spring.cache.cache-names=mylists