1:启动类里面加注解
@EnableScheduling
2:创建定时任务类
package com.limy.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import java.util.Date;
import java.util.Map;
import java.util.Set;
@Controller
@Component
public class TimingController {
@Autowired
private RedisTemplate redisTemplate;
public static final String allKey = "map3*";//这个*一定要加,否则无法模糊查询
//每隔十秒执行删除redis中开头为map的所有缓存
@Scheduled(cron = "0/10 * * * * ?")
public void timingTest(){
//模糊查询开头为map的所有key值
Set<String> keys = redisTemplate.keys(allKey);
//循环查到的所有的key
if(keys!=null&&keys.size()>0){
for(String key : keys){
System.out.println(key);
//删除
redisTemplate.delete(key);
}
}else{
System.out.println("没有该类型缓存");
}
// System.out.println("执行定时任务的时间是:"+new Date());
}
}