缓存优化
纳入Git管理
在码云中新建一个仓库为:
将项目上传到仓库中:
先创建一个本地的仓库:
add本地仓库并提交,再push远程仓库
新建一个分支为V1.0,对于所有缓存的操作,都在这个v1.0的分支下操作。
环境搭建
pom.xml文件中导入spring data redis的maven的坐标
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
|
添加配置文件
1 2 3 4 5 6
| spring: redis: host: 192.168.106.100 port: 6739 password: Zlw199805 database: 0
|
添加配置类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
@Configuration public class RedisConfig extends CachingConfigurerSupport {
@Bean public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate; }
}
|
缓存短信验证码
1:
1 2 3 4 5
|
redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
|
2:
1 2 3 4 5
|
Object codeInSession = redisTemplate.opsForValue().get(phone);
|
3:
1 2
| redisTemplate.delete(phone);
|
缓存菜单数据
改造list方法:
1 2 3 4 5 6 7 8 9 10 11
| List<DishDto> dishDtoList = null; String key = "dish_"+dish.getCategoryId()+"_"+dish.getStatus(); dishDtoList = (List<DishDto>) redisTemplate.opsForValue().get(key); if (dishDtoList!=null) return R.success(dishDtoList);
redisTemplate.opsForValue().set(key,dishDtoList,60, TimeUnit.MINUTES);
|
改造save和update方法:
1 2 3 4 5 6 7
|
String key = "dish_" + dishDto.getCategoryId()+"_1"; redisTemplate.delete(key);
|
提交给本体的Git,并推送到远程的仓库
Spring Cache
缓存套餐数据