首先须要导入的包有 `java
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>`
实体类为:mysql
`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
`sql
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层为:app
`dom
@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); } }
` 小结:这里值得注意的是实体类必须实现可序列化接口,不然将报:spring-boot
` 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]
` 序列化的机制是,用于处理一个数据流中的对象,对象的流被称为所述内容对象的流化。 对象能够操做的对流的读出,该对象还能够通过流化网络之间传送。序列化是为了解决在 流中的问题时触发该对象上读取和写入操做。