xyc
2024-10-28 4cb2fda57541947b5ff743a911ca9414f1c7a514
新增凌晨3点清理redis过期key功能(key7天有效)
1个文件已修改
2个文件已添加
86 ■■■■ 已修改文件
starter/src/main/java/com/zt/life/AdminApplication.java 20 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
starter/src/main/java/com/zt/life/StartupHandler.java 14 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
starter/src/main/java/com/zt/task/PurgeDataTask.java 52 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
starter/src/main/java/com/zt/life/AdminApplication.java
@@ -19,10 +19,9 @@
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.EnableScheduling;
import java.io.File;
import java.text.ParseException;
/**
 * @author hehz
@@ -34,6 +33,7 @@
        org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class})
@EnableCaching
@EnableAsync
@EnableScheduling
public class AdminApplication extends SpringBootServletInitializer {
    @Value("${zt.oss.local-path}")
@@ -59,12 +59,12 @@
        return factory;
    }
    @Scheduled(cron = "0 0 3 * * ?") //每天3点执行
    public void timerListener() throws ParseException {
/*        projectService.dropTemporaryTable();
        projectService.updateHasLifeData();
        this.deleteTempFile();*/
    }
//    @Scheduled(cron = "0 0 3 * * ?") //每天3点执行
//    public void timerListener() throws ParseException {
///*        projectService.dropTemporaryTable();
//        projectService.updateHasLifeData();
//        this.deleteTempFile();*/
//    }
    private void deleteTempFile() {
        String tempUploadDir = localPath + File.separator + "TEMP_UPLOAD";
@@ -78,10 +78,6 @@
    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        try {
            this.timerListener();
        } catch (Exception e) {
        }
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        // 暂时先设置key和value的序列化都是字符串
        redisTemplate.setKeySerializer(new StringRedisSerializer());
starter/src/main/java/com/zt/life/StartupHandler.java
New file
@@ -0,0 +1,14 @@
package com.zt.life;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class StartupHandler implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 刷新缓存
    }
}
starter/src/main/java/com/zt/task/PurgeDataTask.java
New file
@@ -0,0 +1,52 @@
package com.zt.task;
import com.zt.common.utils.CommonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.concurrent.TimeUnit;
@Component
public class PurgeDataTask {
    private static final Logger logger = LoggerFactory.getLogger(PurgeDataTask.class);
    @Autowired
    private RedisTemplate redisTemplate;
    @Scheduled(cron = "0 0 3 * * ?") //每天3点执行
    public void task() {
        logger.info("清理数据task开始");
        Date beginDate = new Date();
        purgeRedis();
        logger.info("清理数据task耗时:" + CommonUtils.getDatePoor(new Date(), beginDate));
        logger.info("清理数据task结束");
    }
    private void purgeRedis() {
        Cursor<byte[]> cursor = (Cursor<byte[]>) redisTemplate.executeWithStickyConnection(
                (RedisCallback<Cursor<byte[]>>) connection -> connection.scan(ScanOptions.NONE)
        );
        while (cursor.hasNext()) {
            String key = new String(cursor.next());
            System.out.println(key);
            Long ttl = redisTemplate.getExpire(key);
            if (ttl == -1) {
                redisTemplate.expire(key, 7*24*3600, TimeUnit.SECONDS);
                System.out.println("set key["+key+"] expire 7 days");
            }
        }
    }
}