Skip to content
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

Provides the ability to configure DefaultPersistenceConfiguration in CacheConfigurationBuilder #2997

Closed
linghengqian opened this issue Feb 28, 2022 · 1 comment

Comments

@linghengqian
Copy link

linghengqian commented Feb 28, 2022

  @Component
  public static class CachingSetup implements JCacheManagerCustomizer
  {
    @Override
    public void customize(CacheManager cacheManager)
    {
      cacheManager.createCache("people", new MutableConfiguration<>()  
        .setExpiryPolicyFactory(TouchedExpiryPolicy.factoryOf(new Duration(SECONDS, 10))) 
        .setStoreByValue(false)
        .setStatisticsEnabled(true));
    }
  }
@Bean
public JCacheManagerCustomizer cacheManagerCustomizer() {
    return cm -> {
        Configuration<Object, Object> cacheConfiguration = createCacheConfiguration();
        cm.createCache("vets", cacheConfiguration);
    };
}

private Configuration<Object, Object> createCacheConfiguration() {
    return Eh107Configuration.fromEhcacheCacheConfiguration(CacheConfigurationBuilder
        .newCacheConfigurationBuilder(Object.class, Object.class, ResourcePoolsBuilder
            .newResourcePoolsBuilder().heap(100)));
}
package com.lingh.config;

@EnableCaching
@Configuration
public class CacheConfig extends CachingConfigurerSupport {
    private javax.cache.CacheManager getCacheManager(EhcacheCachingProvider provider, DefaultConfiguration configuration) {
        return provider.getCacheManager(provider.getDefaultURI(), configuration);
    }

    @Bean 
    @Override
    public org.springframework.cache.CacheManager cacheManager() {
        return new JCacheCacheManager(createInMemoryCacheManager());
    }

    private javax.cache.CacheManager createInMemoryCacheManager() {
        CacheConfiguration<Object, Object> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(
                        Object.class, Object.class,
                        ResourcePoolsBuilder.newResourcePoolsBuilder().heap(123L, EntryUnit.ENTRIES)
                                .disk(123L, MemoryUnit.MB, true))
                .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofDays(123L)))
                .build();
        Map<String, CacheConfiguration<?, ?>> caches = new HashMap<>();
        caches.putAll(this.createCacheConfigurations(cacheConfiguration));
        EhcacheCachingProvider provider = this.getCachingProvider();
        DefaultConfiguration configuration = new DefaultConfiguration(caches, provider.getDefaultClassLoader(), new DefaultPersistenceConfiguration(new File("./cache-data")));
        return this.getCacheManager(provider, configuration);
    }

    private Map<String, CacheConfiguration<?, ?>> createCacheConfigurations(CacheConfiguration<Object, Object> cacheConfiguration) {
        Map<String, CacheConfiguration<?, ?>> caches = new HashMap<>();
        caches.put("test", cacheConfiguration);
        return caches;
    }

     private EhcacheCachingProvider getCachingProvider() {
        return (EhcacheCachingProvider) Caching.getCachingProvider(EhcacheCachingProvider.class.getName());
    }
}
@linghengqian
Copy link
Author

Considering #3038 (comment) 's point of view, I don't think the issue discusses anything that needs attention anymore. So I'm going to close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant