提问人:Bandita Pradhan 提问时间:9/7/2023 更新时间:9/7/2023 访问量:21
无法使用 ReactiveRedisTemplate 从 Redis 缓存中获取值
Unable to fetch value from Redis Cache using ReactiveRedisTemplate
问:
我正在尝试使用 TestContainer 在 Redis 缓存中插入记录。
我的 redis 缓存键/值对是 SccRuleDetail 的 String 和 Set。
我可以使用 StringRedisTemplate<String, String 将记录写入 Redis 缓存>
我正在使用 ReactiveRedisTemplate<String, Object>但似乎它正在插入记录,但当我尝试获取记录时,它说 null。我正在使用 StepVerifier 检查是否插入了记录,并且我得到了预期值。我不确定为什么在检索记录时得到空值。
这是我的代码。
@Test
public void testAddSccRules() throws JsonProcessingException {
String sccActiveRuleRedisKey = "1G"+"79JP"+"130";
Set<SccRuleDetail> sccRuleDetails = new HashSet<>();
SccRuleDetail sccRuleDetail = new SccRuleDetail();
sccRuleDetail.setRowNumber(1);
sccRuleDetail.setRuleID("130");
sccRuleDetail.setCarrierList("AA");
sccRuleDetail.setLocationFromList("DEN");
sccRuleDetail.setLocationToList("JFK");
sccRuleDetails.add(sccRuleDetail);
Map sccRulesMap = new HashMap<String, List<SccRuleDetail>>();
sccRulesMap.put(sccActiveRuleRedisKey, sccRuleDetails);
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(new RedisStandaloneConfiguration(redis.getContainerIpAddress(), redis.getMappedPort(6379)));
connectionFactory.afterPropertiesSet();
RedisSerializer<String> keySerializer = new StringRedisSerializer();
RedisSerializer<Object> valueSerializer = (RedisSerializer<Object>) new SnappySerializer();
RedisSerializationContext.RedisSerializationContextBuilder<String, Object> builder =
RedisSerializationContext.newSerializationContext((RedisSerializer<?>) keySerializer);
RedisSerializationContext<String, Object> serializationContext =
builder.value(valueSerializer).build();
ReactiveRedisTemplate<String, Object> reactiveRedisTemplate =
new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
System.out.println("redisTemplate = " + reactiveRedisTemplate);
Mono<Boolean> result = reactiveRedisTemplate.opsForValue().multiSet(sccRulesMap);
StepVerifier.create(result)
.expectNext(true)
.verifyComplete();
sccRulesMap.forEach((k, v) -> reactiveRedisTemplate.expire(sccActiveRuleRedisKey, Duration.ofMinutes(100L)).subscribe());
Object sccRulesObj = stringRedisTemplate.opsForValue().get(sccActiveRuleRedisKey);
System.out.println("obj =" + sccRulesObj);
}
答: 暂无答案
评论