SpringBoot+SpringCache+redis實現數據緩存

首先須要導入的包有 `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]

` 序列化的機制是,用於處理一個數據流中的對象,對象的流被稱爲所述內容對象的流化。 對象能夠操做的對流的讀出,該對象還能夠通過流化網絡之間傳送。序列化是爲了解決在 流中的問題時觸發該對象上讀取和寫入操做。

相關文章
相關標籤/搜索