Я пытаюсь получить доступ к кэшу redis из моего приложения Spring-boot. В одном из классов я поместил все данные в Redis, а затем в своем потоке кода я пытаюсь получить доступ к данным позже. Я распечатал данные карты, прежде чем пытаться вставить карту, чтобы убедиться, что идентификатор, к которому я пытаюсь получить доступ, хранится в кэше.
@Component
public class CustomerService {
@Autowired
private CustomerServiceImpl assetServiceImpl;
@Autowired
@Qualifier("modelCacheMngr")
CacheManager modelCacheMngr;
public void storeCustomerResponse( Map<String, CustomerDetailBO> customerDetails) throws
CustomerServiceExceptions {
try {
//name of my cache is redis-cache
Cache cacheIsin = modelCacheMngr.getCache("redis-cache");
populateCache(cacheIsin,customerDetails);
} catch (CustomerServiceExceptions e) {
throw e;
}
private void populateCache(Cache cacheIsin, Map<String, CustomerDetailBO> mergingCustomer) {
// using for-each loop for iteration over Map.entrySet()
for (Map.Entry<String, AssetDetailBO> entry : mergingCustomer.entrySet()) {
System.out.println("Item : " + entry.getKey() + " Count : " + entry.getValue());
cacheIsin.put(entry.getKey(), entry.getValue());
}
}
}
Класс, в который я пытаюсь получить Redis Cache
@Component
public class CustomerServiceUtility {
@Autowired
@Qualifier("modelCacheMngr")
CacheManager modelCacheMngr;
public AssetDetailBO getCustomerObject(
String customerId
) {
boolean found = false;
AssetDetailBO asset = null;
Cache isin=modelCacheMngr.getCache("redis_cache");
// Getting null here
if (modelCacheMngr.getCache("redis_cache").get(customerId) != null) {
asset = (AssetDetailBO) modelCacheMngr.getCache("redis_cache").get(customerId).get();
}
}
RedisConfigLocal. java
import org.apache.log4j.Logger;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
@Configuration
@Profile("local")
public class RedisConfigLocal extends CachingConfigurerSupport {
private static final Logger log = Logger.getLogger(RedisConfigLocal.class);
@Bean
public JedisConnectionFactory redisConnectionFactory() {
JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
redisConnectionFactory.setHostName("localhost");
redisConnectionFactory.setPort(6379);
return redisConnectionFactory;
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setEnableTransactionSupport(true);
return redisTemplate;
}
@Bean(name = "modelCacheMngr")
public CacheManager cacheManager() {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate());
cacheManager.setUsePrefix(true);
cacheManager.setTransactionAware(true);
return cacheManager;
}
}