From 4cb2fda57541947b5ff743a911ca9414f1c7a514 Mon Sep 17 00:00:00 2001
From: xyc <jc_xiong@hotmail.com>
Date: 星期一, 28 十月 2024 15:41:17 +0800
Subject: [PATCH] 新增凌晨3点清理redis过期key功能(key7天有效)

---
 starter/src/main/java/com/zt/life/StartupHandler.java   |   14 +++++++
 starter/src/main/java/com/zt/task/PurgeDataTask.java    |   52 ++++++++++++++++++++++++++
 starter/src/main/java/com/zt/life/AdminApplication.java |   20 ++++------
 3 files changed, 74 insertions(+), 12 deletions(-)

diff --git a/starter/src/main/java/com/zt/life/AdminApplication.java b/starter/src/main/java/com/zt/life/AdminApplication.java
index 7ed8ad2..7dfdcac 100644
--- a/starter/src/main/java/com/zt/life/AdminApplication.java
+++ b/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<>();
         // 鏆傛椂鍏堣缃甼ey鍜寁alue鐨勫簭鍒楀寲閮芥槸瀛楃涓�
         redisTemplate.setKeySerializer(new StringRedisSerializer());
diff --git a/starter/src/main/java/com/zt/life/StartupHandler.java b/starter/src/main/java/com/zt/life/StartupHandler.java
new file mode 100644
index 0000000..7d62798
--- /dev/null
+++ b/starter/src/main/java/com/zt/life/StartupHandler.java
@@ -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 {
+        // 鍒锋柊缂撳瓨
+    }
+}
diff --git a/starter/src/main/java/com/zt/task/PurgeDataTask.java b/starter/src/main/java/com/zt/task/PurgeDataTask.java
new file mode 100644
index 0000000..1f5dee8
--- /dev/null
+++ b/starter/src/main/java/com/zt/task/PurgeDataTask.java
@@ -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");
+            }
+        }
+    }
+
+}
\ No newline at end of file

--
Gitblit v1.9.1