Reputation: 340
I am trying to use two Different types of Cache in my Spring boot application,based on Rest API called I hvae to store data in my cache. But during project deploy stage I am getting below error.
[main] ERROR o.s.boot.SpringApplication - Application startup failed 2020-06-30T11:15:17.05+0530 [APP/PROC/WEB/0] OUT java.lang.IllegalStateException: No CacheResolver specified, and no unique bean of type CacheManager found. Mark one as primary (or give it the name 'cacheManager') or declare a specific CacheManager to use, that serves as the default one.
Can I specify @Primary on one cache manager ?I hope it does not stop storing data into another cache . Or is their a better way of doing this?
CacheConfig.java
@Profile("cloud")
public class CacheConfig extends AbstractCloudConfig {
@Autowired
Environment env;
@Bean
public RedisConnectionFactory brRedisFactory() {
return connectionFactory().redisConnectionFactory(env.getProperty("redis_cache"));
}
@Bean
public RedisTemplate<String, Object> brRedisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(brRedisFactory());
return redisTemplate;
}
@Bean(name = "firstCacheMngr")
public CacheManager cacheManager() {
RedisCacheManager cacheManager = new RedisCacheManager(brRedisTemplate());
cacheManager.setUsePrefix(true);
cacheManager.setTransactionAware(true);
return cacheManager;
}
@Bean(name = "secondCacheMngr")
public CacheManager springCacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("secondCache")));
return cacheManager;
}
}
This is how I am storing data in redis cache-
@Cacheable(value="redis-cache",key ="#customerId",cacheManager = "firstCacheMngr")
public CustomerInfo retriveCustomerdetails(String customerId
String quarterEndDate) {
//Calling Rest API
}
return customerInfo;
}
Upvotes: 4
Views: 12598
Reputation: 646
you can put one as @Primary and inject the another one by using @Qualifier
@Bean(name = "firstCacheMngr")
public CacheManager cacheManager() {
RedisCacheManager cacheManager = new RedisCacheManager(brRedisTemplate());
cacheManager.setUsePrefix(true);
cacheManager.setTransactionAware(true);
return cacheManager;
}
@Bean(name = "secondCacheMngr")
@Primary
public CacheManager springCacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("secondCache")));
return cacheManager;
}
and then use the redisCachManager as
@Autowired
@Qualifier("firstCacheMngr")
private CacheManager cacheManager;
Upvotes: 5