@Configuration
@EnableCaching
public class ArcusConfiguration extends CachingConfigurerSupport {
// Not need to extend CachingConfigurerSupport since Spring 6.0
// because CachingConfigurerSupport is deprecated since Spring 6.0
// Just register the beans below since Spring 6.0
private static String ADMIN_ADDRESS = "127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183";
private static String SERVICE_CODE = "test";
private static int POOL_SIZE = 8;
@Bean
public KeyGenerator keyGenerator() {
return new StringKeyGenerator();
}
@Bean
public CacheManager cacheManager() {
return new ArcusCacheManager(
ADMIN_ADDRESS,
SERVICE_CODE,
new ConnectionFactoryBuilder(),
POOL_SIZE,
/* default cache configuration (missing cache) */
defaultCacheConfig(),
/* a map of cache configuration (key=cache name, value=cache configuration) */
initialCacheConfig()
);
}
@Bean
public ArcusCacheConfiguration defaultCacheConfig() {
return new ArcusCacheConfiguration();
}
@Bean
public Map<String, ArcusCacheConfiguration> initialCacheConfig() {
Map<String, ArcusCacheConfiguration> initialCacheConfig = new HashMap<>();
initialCacheConfig.put("testCache", testCacheConfig());
initialCacheConfig.put("devCache", devCacheConfig());
return initialCacheConfig;
}
@Bean
public ArcusCacheConfiguration testCacheConfig() {
return new ArcusCacheConfiguration()
.withServiceId("TEST-")
.withPrefix("PRODUCT")
.withExpireSeconds(60)
.withTimeoutMilliSeconds(800);
}
@Bean
public ArcusCacheConfiguration devCacheConfig() {
return new ArcusCacheConfiguration()
.withServiceId("DEV-")
.withPrefix("PRODUCT")
.withExpireSeconds(120)
.withTimeoutMilliSeconds(800);
}
}
@Cacheable(cacheNames = "testCache", key="#id")
public Product getProduct_TestCache(int id) {
return new Product(id);
}
@Cacheable(cacheNames = "devCache", key="#id")
public Product getProduct_DevCache(int id) {
return new Product(id);
}
@Cacheable(cacheNames = "missingCache", key="#id")
public Product getProduct_DefaultCache(int id) {
return new Product(id);
}