软件测试最新Redis应用(5)——Redis的项目应用(四(1),软件测试开发者必看避坑指南

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
    // 1.清除缓存中的数据
    stringRedisTemplate.delete("usernames");

    // 2.更新缓存中的数据
    List<User> userList = userMapper.selectList(null);
    userList.forEach(user -> stringRedisTemplate.opsForSet().add("usernames", user.getUsername()));

    log.debug("redis缓存预热成功》》》》》》");
}

}


### 定时任务的配置类PreHotConfig.java


![在这里插入图片描述](https://img-blog.csdnimg.cn/c68ef65dff37440b9a3dd8ef0c8ace48.png)


表达式 意义



> 
> [Cron - 在线Cron表达式生成器 (ciding.cc)]( )
> 
> 
> 


0 0 8 ? 每天早上8点触发  
 0 30 9 ? 每天早上9点半触发  
 0 *23* *? 每天晚上的11点0分到晚上11点59分,每一分钟触发一次  
 0 0/5 22,23* *? 每天晚上的10点0分到11点55分,及每天晚上11点0分到晚上11点55分,每五分钟触发一次  
 0 0-5 23* *? 每天晚上的11点0分到晚上11点5分,每一分钟触发一次  
 0 10,45 14 ? 3 4 每年3月的星期三,下午2点10分和下午2点45分触发  
 0 30 9 ?* 2-5 每个月的周一到周五的9点半触发  
 0 30 9 15 *? 每个月15号的9点半触发  
 0 55 23 L* ? 每个月最后一天的晚上11点55分触发  
 0 55 23 ? \* 6L 每个月最后一个周五的晚上11点55分触发  
 0 0 8 ? 5 1#2 5月的第二个星期天的早上8点触发,即母亲节那天八点给妈妈发节日快乐  
 0 0 8 ? 6 1#3 6月的第三个星期天的早上8点触发,即父亲节那天八点给爸爸发节日快乐



package com.tianju.redisDemo.config;

import com.tianju.redisDemo.job.UserPreHot;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* 定时任务的配置类
*/
@Configuration
public class PreHotConfig {

/\*\*

* 计划任务的细节配置,和哪个类关联,采用建造者模式
* 和 job 下的 UserPreHot 关联起来
* @return 返回JobDetail
*/
@Bean
public JobDetail jobDetail(){
return JobBuilder.newJob() // 建造者设计模式
.ofType(UserPreHot.class)
.storeDurably()
.build();
}

@Bean
public Trigger trigger(){
    return TriggerBuilder.newTrigger()
            .forJob(jobDetail())
            .withSchedule(
                    CronScheduleBuilder.cronSchedule("0 04 21 \* \* ?")
            )
            .startNow()
            .build();
}

}


### 启动项目后定时任务


![在这里插入图片描述](https://img-blog.csdnimg.cn/5ca98ac26c8242a3add4536152a7dbab.png)


![在这里插入图片描述](https://img-blog.csdnimg.cn/15d0345f72c6484b9ef37921f9e6a9ee.png)



server:
port: 9099

spring:

redis的相关配置

redis:
host: 192.168.111.130
port: 6379
database: 0

mysql的相关配置

datasource:
druid:
url: jdbc:mysql://127.0.0.1/community?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username: root
password: 123
driver-class-name: com.mysql.cj.jdbc.Driver

日志需要配置一下

logging:
level:
com.tianju.redisDemo: debug

mybatis-plus的日志

mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl


## 缓存预热:用@Schedule的方式


### 主启动类@EnableScheduling



package com.tianju.redisDemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling //

public class RedisApp {
public static void main(String[] args) {
SpringApplication.run(RedisApp.class, args);
}
}


### 预热方法@Scheduled(cron = “0 54 21 \* \* ?”)


![在这里插入图片描述](https://img-blog.csdnimg.cn/8c5c5cca2bb44bdbb57ea0d84f45b8e4.png)


表达式 意义



> 
> [Cron - 在线Cron表达式生成器 (ciding.cc)]( )
> 
> 
> 


0 0 8 ? 每天早上8点触发  
 0 30 9 ? 每天早上9点半触发  
 0 *23* *? 每天晚上的11点0分到晚上11点59分,每一分钟触发一次  
 0 0/5 22,23* *? 每天晚上的10点0分到11点55分,及每天晚上11点0分到晚上11点55分,每五分钟触发一次  
 0 0-5 23* *? 每天晚上的11点0分到晚上11点5分,每一分钟触发一次  
 0 10,45 14 ? 3 4 每年3月的星期三,下午2点10分和下午2点45分触发  
 0 30 9 ?* 2-5 每个月的周一到周五的9点半触发  
 0 30 9 15 *? 每个月15号的9点半触发  
 0 55 23 L* ? 每个月最后一天的晚上11点55分触发  
 0 55 23 ? \* 6L 每个月最后一个周五的晚上11点55分触发  
 0 0 8 ? 5 1#2 5月的第二个星期天的早上8点触发,即母亲节那天八点给妈妈发节日快乐  
 0 0 8 ? 6 1#3 6月的第三个星期天的早上8点触发,即父亲节那天八点给爸爸发节日快乐



package com.tianju.redisDemo.job;

import com.tianju.redisDemo.dao.UserMapper;
import com.tianju.redisDemo.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.Schedules;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
* 采用 @Scheduled(cron = “0 54 21 * * ?”)进行预热
*/
@Slf4j
@Component
public class UsernamesPreHot {
@Resource
private StringRedisTemplate stringRedisTemplate;

@Resource
private UserMapper userMapper;

@Scheduled(cron = "0 54 21 \* \* ?")
public void preHot() {
    // 1.清除缓存中的数据
    stringRedisTemplate.delete("usernames");

    // 2.更新缓存中的数据
    List<User> userList = userMapper.selectList(null);
    userList.forEach(user ->
            stringRedisTemplate.opsForSet()
                    .add("usernames", user.getUsername()));

    log.debug("redis缓存预热,usernames");
}

}


预热成功


![在这里插入图片描述](https://img-blog.csdnimg.cn/65a398df4b7e4f69879c255a4c5a2bc6.png)




---


## 总结


1.缓存预热是啥,将某些数据加载到Redis中;  
 2.注册的流程,缓存预热在用户注册中的应用;  
 3.quartz方式,定时任务extends QuartzJobBean;  
 4.定时任务配置类,JobDetail,Trigger,@Bean;  
 5.@Schedule方式:@Component+@Scheduled(cron = “0 54 21 \* \* ?”);







![img](https://img-blog.csdnimg.cn/img_convert/f150ef4e02e8d12b820c759e388599ba.png)
![img](https://img-blog.csdnimg.cn/img_convert/6ac56222c54e61584042db06176fb25c.png)
![img](https://img-blog.csdnimg.cn/img_convert/da6dd16aaaced78e25ea8de0c63c0e07.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.youkuaiyun.com/forums/4f45ff00ff254613a03fab5e56a57acb)**


[外链图片转存中...(img-Obej7mCF-1715579123129)]

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.youkuaiyun.com/forums/4f45ff00ff254613a03fab5e56a57acb)**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值