1、pom文件添加redis启动器
<!--redis启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
2、application-dev.yml添加配置
spring:
redis:
database: 0
host: 192.168.102.63
port: 10079
password: 123_12345678^_00
lettuce:
pool:
max-active: 16 #连接池最大连接数(使用负值表示没有限制)
max-idle: 8 #连接池中的最大空闲连接
max-wait: 100ms #连接池最大阻塞等待时间(使用负值表示没有限制)
min-idle: 4 #连接池中的最小空闲连接
3、配置RedisTemplate序列化器
RedisTemplate默认使用JdkSerializationRedisSerializer序列化,缺点是:
1)实体类必须实现Serializable接口
2)序列化后存入redis为十六进制字符\x0c这样的,可读性差
3)占用空间特别大
在包com.example.config下,添加RedisTemplateConfig.java
package com.example.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
@Configuration
public class RedisTemplateConfig {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 创建一个json的序列化对象
GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
// 设置key的序列化方式为String
redisTemplate.setKeySerializer(new GenericToStringSerializer<>(Object.class));
// 设置value的序列化方式为json
redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
// 设置hashKey的序列化方式为String
redisTemplate.setHashKeySerializer(new GenericToStringSerializer<>(Object.class));
// 设置hashValue的序列化方式为json
redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
// 设置支持事务
redisTemplate.setEnableTransactionSupport(true);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
4、建立测试Controller
在包com.example.web下,添加RedisController.java
package com.example.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.create.entity.TblTeacherInf;
import com.example.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@Api(description = "redis测试接口")
@RestController
@RequestMapping("/redis")
public class RedisController {
@Autowired
TeacherService teacherService;
@ApiOperation("增加一条记录")
@GetMapping("/insert")
public boolean insert(TblTeacherInf teacher) {
return teacherService.insert(teacher);
}
@ApiOperation("修改一条记录")
@GetMapping("/update")
public boolean update(TblTeacherInf teacher) {
return teacherService.update(teacher);
}
@ApiOperation("删除一条记录")
@GetMapping("/delete")
public boolean delete(TblTeacherInf teacher) {
return teacherService.delete(teacher);
}
@ApiOperation("查找一条记录")
@GetMapping("/findById")
public TblTeacherInf findById(Integer id) {
return teacherService.findById(id);
}
}
5、建立测试Service
在包com.example.service下,添加TeacherService.java
package com.example.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import com.create.entity.TblTeacherInf;
import com.create.entity.TblTeacherInfMapper;
@Service
public class TeacherService {
public static final String CACHE_KEY = "redis-cache:";
@Autowired
TblTeacherInfMapper teacherMapper;
@Autowired
RedisTemplate<Object, Object> redisTemplate;
/**
* 增加
*
* @param teacher
* @return
*/
public boolean insert(TblTeacherInf teacher) {
// 先插入数据库
teacherMapper.insert(teacher);
// 再查询出插入的对象
teacher = teacherMapper.selectByPrimaryKey(teacher.getId());
if (null != teacher) {
// 再增加redis缓存
redisTemplate.opsForValue().set(CACHE_KEY + teacher.getId(), teacher);
return true;
} else {
return false;
}
}
/**
* 修改
*
* @param teacher
* @return
*/
public boolean update(TblTeacherInf teacher) {
// 先修改数据库
teacherMapper.updateByPrimaryKeySelective(teacher);
// 再查询出修改的对象
teacher = teacherMapper.selectByPrimaryKey(teacher.getId());
if (null != teacher) {
// 再更新redis缓存,更新也是set命令
redisTemplate.opsForValue().set(CACHE_KEY + teacher.getId(), teacher);
return true;
} else {
return false;
}
}
/**
* 删除
*
* @param teacher
* @return
*/
public boolean delete(TblTeacherInf teacher) {
// 临时保存key
String tempKey = CACHE_KEY + teacher.getId();
// 先删除数据库记录
teacherMapper.deleteByPrimaryKey(teacher.getId());
// 再查询删除的对象,为空则删除缓存
teacher = teacherMapper.selectByPrimaryKey(teacher.getId());
if (null == teacher) {
// 再删除redis缓存
redisTemplate.delete(tempKey);
return true;
} else {
return false;
}
}
/**
* 查找
*
* @param id
* @return
*/
public TblTeacherInf findById(Integer id) {
ValueOperations<Object, Object> operation = redisTemplate.opsForValue();
// 先查找redis缓存
TblTeacherInf obj = (TblTeacherInf) operation.get(CACHE_KEY + id);
if (obj == null) {
// 到数据库查询
obj = teacherMapper.selectByPrimaryKey(id);
if (obj != null) {
// 添加到redis缓存
operation.set(CACHE_KEY + obj.getId(), obj);
}
}
return obj;
}
}
6、注意事项
6.1、config里定义redisTemplate类型为RedisTemplate<Object, Object>,注入对象时不能改为RedisTemplate<String, TblTeacherInf>,会报错:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field redisTemplate in com.example.service.TeacherService required a bean of type 'org.springframework.data.redis.core.RedisTemplate' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
The following candidates were found but could not be injected:
- Bean method 'redisTemplate' in 'RedisAutoConfiguration' not loaded because @ConditionalOnMissingBean (names: redisTemplate; SearchStrategy: all) found beans named redisTemplate
Action:
Consider revisiting the entries above or defining a bean of type 'org.springframework.data.redis.core.RedisTemplate' in your configuration.
6.2、数据库和缓存之间的操作关系
增加时:
1)先插数据库
2)再查询出插入对象
3)再增加redis缓存
修改时:
1)先修改数据库
2)再查询出修改的对象
3)再更新redis缓存
删除时:
1)先删除数据库记录
2)再查询数据库记录,为空则删除缓存
查询时:
1)先查询redis缓存
2)如果缓存为空,到数据库查询
3)如果数据库有记录,则更新到缓存
7、测试
访问swagger测试页面:http://127.0.0.1:8088/swagger-ui.html
注:最新代码上传至https://github.com/csj50/myboot