Spring Data Redis TransactionSupport
Easy to ignore issues:
RedisConfig
StringRedisTemplate customerTemplate(RedisConnectionFactory redisConnectionFactory) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(redisConnectionFactory);
stringRedisTemplate.setEnableTransactionSupport(true);
return stringRedisTemplate;
}
RedisTransactionSupportTest
@GetMapping(value = "/test")
@Transactional
public ResponseEntity<?> testRedisTransaction(@RequestParam String name) {
Person save = personRepository.save(new Person(null, name));
customerTemplate.opsForValue().set(save.getId().toString(), save.toString());
String s = customerTemplate.opsForValue().get(save.getId().toString());
return ResponseEntity.ok(Objects.requireNonNull(s));
}
@GetMapping(value = "/test/v2")
@Transactional
public ResponseEntity<?> testRedisTransactionv2(@RequestParam String name) {
Person save = personRepository.save(new Person(null, name));
customerTemplate.getRequiredConnectionFactory().getConnection().set(save.getId().toString().getBytes(StandardCharsets.UTF_8),
save.toString().getBytes(StandardCharsets.UTF_8));
byte[] s = customerTemplate.getRequiredConnectionFactory().getConnection().get(save.getId().toString().getBytes(StandardCharsets.UTF_8));
return ResponseEntity.ok(new String(s, StandardCharsets.UTF_8));
}
always /test
interface return null,other /test/v2
interface return person;
.....