springboot定时删除redis中key值为map开头的所有的缓存定时任务

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());
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值