Skip to content

Provide a callback that enables customization of the properties used to create the JCache CacheManager #39350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
CXwudi opened this issue Jan 31, 2024 · 5 comments
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@CXwudi
Copy link

CXwudi commented Jan 31, 2024

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

@CXwudi CXwudi changed the title Consider to make Consider to make JCachePropertiesCustomizer public again Jan 31, 2024
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 31, 2024
@wilkinsona wilkinsona added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-feedback We need additional information before we can continue labels Jan 31, 2024
@wilkinsona
Copy link
Member

Making JCachePropertiesCustomizer public would encourage use of CacheProperties as public API which we discourage in the documentation. There are a few other places where we have a similar situation. If we're going to add to them, we should probably reconsider our policy for @ConfigurationProperties classes.

@wilkinsona wilkinsona added the for: team-meeting An issue we'd like to discuss as a team to make progress label Jan 31, 2024
@CXwudi
Copy link
Author

CXwudi commented Jan 31, 2024

I see. Not sure if this is a good idea, but how about introducing a new public customizer interface that takes a Properties object exclusively? It appears that both my issue and #29542 are primarily focused on altering the Properties object supplied to the CachingProvider.getCacheManager() method, rather than adjusting the Spring CacheProperties configuration property. (P.S: I noticed that CacheProperties is already a public class, implying that users have the ability to inject it into their application codes.)

@wilkinsona
Copy link
Member

I think we could even change the existing callback so that it only takes the Properties and update HazelcastPropertiesCustomizer to inject CacheProperties into its constructor instead:

https://github.com/wilkinsona/spring-boot/tree/gh-39350

@wilkinsona wilkinsona changed the title Consider to make JCachePropertiesCustomizer public again Provide a callback that enables customization of the properties used to create the JCache CacheManager Jan 31, 2024
@philwebb philwebb added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged for: team-meeting An issue we'd like to discuss as a team to make progress labels Feb 14, 2024
@philwebb philwebb added this to the 3.x milestone Feb 14, 2024
@belowyoon
Copy link

I created PR #41209, PTAL :)

@wilkinsona
Copy link
Member

Thanks, @belowyoon, but, as mentioned above, we already have these changes prepared in a branch that we'll merge in due course.

@wilkinsona wilkinsona self-assigned this Jun 24, 2024
@wilkinsona wilkinsona modified the milestones: 3.x, 3.4.x, 3.4.0-M1 Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

5 participants