2019独角兽企业重金招聘Python工程师标准>>>
首先需要导入的包有 `
org.springframework.boot
spring-boot-starter-cache
org.springframework.boot
spring-boot-starter-data-redis
`
实体类为:
`
[@Entity](https://my.oschina.net/u/1260961)
public class Person implements Serializable{
private static final long serialVersionUID = -1L;
[@Id](https://my.oschina.net/u/3451001)
@GeneratedValue
private Long id;
private String name;
private Integer age;
private String address;
public Person(){
}
public Person(Long id,String name,Integer age,String address){
this.id=id;
this.name=name;
this.age=age;
this.address=address;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
` application.yml配置为:
`
spring:
cache:
type: redis
redis:
host: 127.0.0.1 # server host
port: 6379 # connection port
pool:
## 连接池最大连接数(使用负值表示没有限制)
max-active: 8
## 连接池中的最大空闲连接
max-idle: 8
## 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
## 连接池中的最小空闲连接
min-idle: 0
## 连接超时时间(毫秒)
timeout: 0
database: 0
password:
profiles:
active:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/t_user
password: 123456
username: root
jpa:
hibernate:
ddl-auto: update
show-sql: true`
Service层使用SpringCache对实现对数据的缓存,注解的用意可自行百度:
`
[@Service](https://my.oschina.net/service)
public class PersonServiceImpl{
@Autowired
private PersonRepository repository;
@CachePut(value = "people",key = "#person.id")
public Person save(Person person) {
Person p=repository.save(person);
System.out.println("为id、key为:"+p.getId()+"的数据做了缓存");
return p;
}
@Cacheable(value = "people",key = "#person.id")
public Person findOne(Person person) {
Person p=repository.findOne(person.getId());
System.out.println("为id、key为:"+p.getId()+"的数据做了缓存");
return p;
}
@CacheEvict(value = "people")
public void remove(Long id) {
repository.delete(id);
System.out.println("删除了id、key为:"+id+"的数据缓存");
}
}
` Controller层为:
`
@RestController
public class CacheController {
@Autowired
private PersonServiceImpl personService;
@RequestMapping("/put")
public Person save(Person person){
return personService.save(person);
}
@RequestMapping("/evit")
public String remove(Long id){
personService.remove(id);
return "ok";
}
@RequestMapping("/able")
public Person cacheable(Person person){
return personService.findOne(person);
}
}
` 小结:这里值得注意的是实体类必须实现可序列化接口,否则将报:
` Cannot serialize; nested exception is org.springframework.core.serializer.support.SerializationFailedException: Failed to serialize object using DefaultSerializer; nested exception is java.lang.IllegalArgumentException DefaultSerializer requires a Serializable payload but received an object of type [com.example.doman.Person]
` 序列化的机制是,用于处理一个数据流中的对象,对象的流被称为所述内容对象的流化。 对象可以操作的对流的读出,该对象还可以经过流化网络之间传送。序列化是为了解决在 流中的问题时触发该对象上读取和写入操作。