redis-memory as non-relational databases, are often employed in the project, with small redis described example of the basic usage
1: Introduction rely redis in the pom file project
<!--springboot整合redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2: Configuration of the connection information redis application.properties
#配置redis连接信息(单机模式)
spring.redis.host=192.168.58.128
spring.redis.port=6379
spring.redis.password=123456
3: the implementation code
@Autowired
private RedisTemplate<Object,Object> redisTemplate;
@GetMapping("/get/{id}")
@ResponseBody
public Student getOne(@PathVariable Integer id){
//redis序列化的方式
RedisSerializer stringSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);
//首先从缓存中获取
Student student= (Student) redisTemplate.opsForValue().get("studentSave");
if(null!=student){
log.info("redis方式");
return student;
}
//缓冲中无数据,从数据库中获取,并放入redis保存
log.info("mysql 方式");
student= studentService.selectOne(id);
//磁盘的存储要求类实现序列化(Student implements Serializable)
redisTemplate.opsForValue().set("studentSave",student);
return student;
}
Note: springboot automatically integrated redis only generic template object recognition RedisTemplate <Object, Object> redisTemplate; and RedisTemplate <String, String> redisTemplate;