这是我在@configuration 类中定义的redis 模板,它采用绝地连接工厂,我已将自定义类 Student 设置为 RedisTemplate 中的值
@Bean
public RedisTemplate<String, Student> template() {
RedisTemplate<String, Student> template = new RedisTemplate<String, Student>();
template.setConnectionFactory(connectionFactory());
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new JdkSerializationRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setEnableTransactionSupport(true);
template.afterPropertiesSet();
return template;
}
这是我的学生班
@Entity
@Table(name = "student")
@Getter
@Setter
public class Student implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, unique = true)
private Integer id;
@Column(name = "full_name", nullable = false)
private String fullName;
@Column(name = "email", nullable = false, unique = true)
private String email;
@Column(name = "created_on", nullable = false)
private LocalDateTime createdOn;
@Column(name = "updated_on", nullable = false)
private LocalDateTime updatedOn;
@PrePersist
public void onCreate() {
this.createdOn = LocalDateTime.now();
this.updatedOn = LocalDateTime.now();
}
@PreUpdate
public void onUpdate() {
this.updatedOn = LocalDateTime.now();
}
}
这是我的控制器代码
@GetMapping("/student/{id}")
@Cacheable(key="#id", value = "student")
public Student getStudentHandler(@PathVariable Integer id) {
return studentService.getStudentById(id);
}
我正在使用 redis-cli 以这种形式查看数据
127.0.0.1:6380> get student::1
"\xac\xed\x00\x05sr\x00 com.demo.school.entity.Student\xc7\x8f\xd4\xd3\x86S@\x9c\x02\x00\x05L\x00\tcreatedOnt\x00\x19Ljava/time/LocalDateTime;L\x00\x05emailt\x00\x12Ljava/lang/String;L\x00\bfullNameq\x00~\x00\x02L\x00\x02idt\x00\x13Ljava/lang/Integer;L\x00\tupdatedOnq\x00~\x00\x01xpsr\x00\rjava.time.Ser\x95]\x84\xba\x1b\"H\xb2\x0c\x00\x00xpw\x0e\x05\x00\x00\a\xe4\x0c\x1c\x0f!*\x1ejA@xt\x00\rabc@gmail.comt\x00\x03abcsr\x00\x11java.lang.Integer\x12\xe2\xa0\xa4\xf7\x81\x878\x02\x00\x01I\x00\x05valuexr\x00\x10java.lang.Number\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\x00\x01sq\x00~\x00\x05w\x0e\x05\x00\x00\a\xe4\x0c\x1c\x0f!*\x1ej\x93Hx"
我正在使用 Postgresql 作为我的数据库,并使用 redis 来缓存我怎样才能使它可读。