实现数据缓存,如果缓存中没有数据,则从数据库查询,并且写入redis缓存,如果redis缓存中有数据,则直接从redis中读取,同事删除更新等操作也需要维护缓存。本文基于前面两篇文章而来,部分重复就不贴。
实现1、依赖2、配置redis跟mybatisorg.springframework.boot spring-boot-starter-data-redisorg.springframework.boot spring-boot-starter-weborg.mybatis.spring.boot mybatis-spring-boot-starter1.3.2 mysql mysql-connector-javaruntime org.springframework.boot spring-boot-starter-testtest org.apache.commons commons-pool2
yml配置如下:
server:
port: 8989spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8 username: root password: root redis:
host: 127.0.0.1
port: 6379
lettuce:
pool:
max-idle: 8
min-idle: 0
max-active: 8mybatis:
config-location: classpath:/mybatis/config/mybatis-config.xml
mapper-locations: classpath:/mybatis/mapper@EnableCaching@Configuration@AutoConfigureAfter(RedisAutoConfiguration.class)public class RedisConfig {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
template.setValueSerializer(jackson2JsonRedisSerializer()); //使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(jackson2JsonRedisSerializer());
template.afterPropertiesSet(); return template;
}
@Bean
public RedisSerializer 相比上节,我们多配置了一个缓存管理器,在springboot2中配置缓存管理是新的api也就是builder模式构建。然后通过@EnableCaching 开启缓存注解。然后需要注意的是 你在redistemplate中的配置的key,value序列化方法并不会生效,需要在RedisCacheConfiguration中单独配置。
3、使用caching注解一般缓存在service层中使用。
代码如下
@Servicepublic class UserServiceImpl implements UserService { @Autowired
private UserMapper userMapper; @Override
public User saveUser(User user) {
userMapper.save(user); // 返回用户信息,带id
return user;
}
@CacheEvict(value = "user", key = "#root.args[0]", condition = "#result eq true") @Override
public Boolean removeUser(Long id) { // 如果删除记录不为1 则是失败
return userMapper.deleteById(id) == 1;
}
@Cacheable(value = "user", key = "#root.args[0]", unless = "#result eq null ") @Override
public User getById(Long id) { return userMapper.selectById(id);
}
@CachePut(value = "user", key = "#root.args[0]", unless = "#user eq null ") @Override
public User updateUser(User user) {
userMapper.update(user); return user;
}
}其中每个注解的作用都写在注释中了。
4、测试然后我们编写一下对应的rest接口来测试
@RequestMapping("/user")@RestControllerpublic class UserController { @Autowired
private UserService userService; @PostMapping
public User save(@RequestBody User user) { return userService.saveUser(user);
} @PutMapping
public User update(@RequestBody User user) { return userService.updateUser(user);
} @DeleteMapping(value = "/id/{id}") public Boolean delete(@PathVariable Long id) { return userService.removeUser(id);
} @GetMapping(value = "/id/{id}") public User findById (@PathVariable Long id) { return userService.getById(id);
}
}启动程序。首先访问http://localhost:8989/user/id/2
成功请求数据 并且控制台打印sql
image.png
查看redis是否成功缓存数据。
image.png
成功缓存数据,并且key为主键id。
再次刷新访问。
image.png
并没有打印sql,说明缓存生效,数据是从缓存中去的,而没有去访问数据库。
至于修改跟删除两个注解就交给大家测试。
作者:余空啊
链接:https://www.jianshu.com/p/6943bb8a9ab8
。



