一、緩存優(yōu)化
問題說明
環(huán)境搭建
導(dǎo)入maven坐標(biāo)
在項目的pom.xm1文件中導(dǎo)入spring data redis的maven坐標(biāo):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置yml文件
在項目的application.yml中加入redis相關(guān)配置:
spring
redis:
host:172.17.2.94
port: 6379
password: root@123456
database: 0
設(shè)置序列化器,編寫配置類
在項目中加入配置類RedisConfig:
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory connectionFactory){
RedisTemplate<Object,Object> redisTemplate = new RedisTemplate<>();
//默認(rèn)的Key序列化器為: JdkSerializationRedisSerializer
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory( connectionFactory) ;
return redisTemplate;
}
}
也可以用StringRedisTemplate就不用配置類
緩存短信驗證碼
實現(xiàn)思路
前面我們已經(jīng)實現(xiàn)了移動端手機驗證碼登錄,隨機生成的驗證碼我們是保存在HttpSession中的?,F(xiàn)在需要改造為將驗證碼緩存在Redis中,具體的實現(xiàn)思路如下:
1、在服務(wù)端UserController中注入RedisTemplate對象,用于操作Redis
@Autowired
private RedisTemplate redisTemplate;
2、在服務(wù)端UserController的sendMsg方法中,將隨機生成的驗證碼緩存到Redis中,并設(shè)置有效期為5分鐘
redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
3、在服務(wù)端UserController的login方法中,從Redis中獲取緩存的驗證碼,如果登錄成功則刪除Redis中的驗證碼
//從redis中獲取保存的驗證碼
Object codeInSession =redisTemplate.opsForValue().get(phone);
//如果用戶登錄成功則刪除Redis中緩存的驗證碼
redisTemplate.delete(phone);
緩存菜品數(shù)據(jù)
實現(xiàn)思路
前面我們已經(jīng)實現(xiàn)了移動端菜品查看功能,對應(yīng)的服務(wù)端方法為DishController的list方法,此方法會根據(jù)前端提交的查詢條件進(jìn)行數(shù)據(jù)庫查詢操作。在高并發(fā)的情況下,頻繁查詢數(shù)據(jù)庫會導(dǎo)致系統(tǒng)性能下降,服務(wù)端響應(yīng)時間增長?,F(xiàn)在需要對此方法進(jìn)行緩存優(yōu)化,提高系統(tǒng)的性能。
具體的實現(xiàn)思路如下:
1、改造DishController的list方法,先從Redis中獲取菜品數(shù)據(jù),如果有則直接返回,無需查詢數(shù)據(jù)庫;如果沒有則查詢數(shù)據(jù)庫,并將查詢到的菜品數(shù)據(jù)放入Redis。
List<DishDto> dishDtoList=null;
//動態(tài)構(gòu)造Key
String key="dish_"+dish.getCategoryId()+"_"+dish.getStatus();
//先從redis中獲取緩存數(shù)據(jù)
dishDtoList= (List<DishDto>) redisTemplate.opsForValue().get(key);
if(dishDtoList!=null){
//如果存在,則直接返回,無需查詢數(shù)據(jù)庫
return R.success(dishDtoList);
}
...
//如果不存在,則查詢數(shù)據(jù)庫,并且將查詢到的菜品數(shù)據(jù)添加到緩存中
redisTemplate.opsForValue().set(key,dishDtoList,60, TimeUnit.MINUTES);
2、改造DishController的save和update方法,加入清理緩存的邏輯
//清理所有菜品緩存數(shù)據(jù)
//Set keys = redisTemplate.keys("dish_*");
//redisTemplate.delete(keys);
//清理某個分類下面的菜品緩存數(shù)據(jù)
String key="dish_"+dishDto.getCategoryId()+"_"+dishDto.getStatus();
redisTemplate.delete(key);
注意:在使用緩存過程中,要注意保證數(shù)據(jù)庫中的數(shù)據(jù)和緩存中的數(shù)據(jù)一致,如果數(shù)據(jù)庫中的數(shù)據(jù)發(fā)生變化,需要及時清理緩存數(shù)據(jù)。
SpringCache
Spring Cache介紹
Spring cache是一個框架,實現(xiàn)了基于注解的緩存功能,只需要簡單地加一個注解,就能實現(xiàn)緩存功能。
Spring Cache提供了一層抽象,底層可以切換不同的cache實現(xiàn)。具體就是通過CacheManager接口來統(tǒng)一不同的緩存技術(shù)。
CacheManager是Spring提供的各種緩存技術(shù)抽象接口。
針對不同的緩存技術(shù)需要實現(xiàn)不同的CacheManager:
Spring Cache常用注解
在spring boot項目中,使用緩存技術(shù)只需在項目中導(dǎo)入相關(guān)緩存技術(shù)的依賴包,并在啟動類上使用@EnableCaching開啟緩存支持即可。
例如,使用Redis作為緩存技術(shù),只需要導(dǎo)入Spring data Redis的maven坐標(biāo)即可。
Spring Cache使用方式
在Spring Boot項目中使用Spring Cache的操作步驟(使用redis緩存技術(shù));
1、導(dǎo)入maven坐標(biāo)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
2.配置application.yml
spring:
cache:
redis:
time-to-live: 1800000#設(shè)置緩存有效期
3、在啟動類上加入@EnableCaching注解,開啟緩存注解功能
4、在Controller的方法上加入@Cacheable、@CacheEvict等注解,進(jìn)行緩存操作
緩存套餐數(shù)據(jù)
實現(xiàn)思路
前面我們已經(jīng)實現(xiàn)了移動端套餐查看功能,對應(yīng)的服務(wù)端方法為SetmealController的list方法,此方法會根據(jù)前端提交的查詢條件進(jìn)行數(shù)據(jù)庫查詢操作。在高并發(fā)的情況下,頻繁查詢數(shù)據(jù)庫會導(dǎo)致系統(tǒng)性能下降,服務(wù)端響應(yīng)時間增長?,F(xiàn)在需要對此方法進(jìn)行緩存優(yōu)化,提高系統(tǒng)的性能。
具體的實現(xiàn)思路如下:
1、導(dǎo)入Spring Cache和Redis相關(guān)maven坐標(biāo)
2、在application.yml中配置緩存數(shù)據(jù)的過期時間
3、在啟動類上加入@EnableCaching注解,開啟緩存注解功能
4、在SetmealController的list方法上加入@Cacheable注解
5、在SetmealController的save和delete方法上加入CacheEvict注解
代碼改造
在pom.xml文件中導(dǎo)入maven坐標(biāo):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
在application.yml中配置緩存數(shù)據(jù)過期時間:
cache:
redis:
time-to-live: 1800000 #設(shè)置緩存數(shù)據(jù)過期時間
在啟動類@EnableCaching注解
在list方法上添加注解,實現(xiàn)在redis里添加緩存:
@Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
在update,add,delete方法上添加注解,清除緩存:文章來源:http://www.zghlxwxcb.cn/news/detail-520527.html
@CacheEvict(value = "setmealCache",allEntries = true)
注意:要讓R實現(xiàn)Serializable接口(序列化),注解才能生效文章來源地址http://www.zghlxwxcb.cn/news/detail-520527.html
到了這里,關(guān)于springboot項目外賣管理 day08-緩存優(yōu)化的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!