From 364b0c7068f3e298c3d32c5084042fe4ff993567 Mon Sep 17 00:00:00 2001 From: tianfeng <769204422@qq.com> Date: Fri, 6 Feb 2026 14:19:11 +0800 Subject: [PATCH] azizi test --- .../other-start/src/test/java/RedisTest.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 rc-service/rc-service-other/other-start/src/test/java/RedisTest.java diff --git a/rc-service/rc-service-other/other-start/src/test/java/RedisTest.java b/rc-service/rc-service-other/other-start/src/test/java/RedisTest.java new file mode 100644 index 00000000..046bcde7 --- /dev/null +++ b/rc-service/rc-service-other/other-start/src/test/java/RedisTest.java @@ -0,0 +1,49 @@ +import com.red.circle.OtherServiceApplication; +import com.red.circle.component.redis.service.RedisService; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +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.test.context.junit4.SpringRunner; + +import java.util.concurrent.TimeUnit; + +@SpringBootTest(classes = OtherServiceApplication.class) +@RunWith(SpringRunner.class) +public class RedisTest { + + @Autowired + private RedisTemplate redisTemplate; + + @Test + public void testRedis(){ + setExpireTimeForAziziKeys(); + } + + public void setExpireTimeForAziziKeys() { + String pattern = "AZIZI_SEVEN_CHECK_IN:*"; + long expireTime = 365; // 天数 + + // 使用 scan 遍历匹配的 key + ScanOptions options = ScanOptions.scanOptions() + .match(pattern) + .count(100) // 每次扫描100个 + .build(); + + redisTemplate.execute((RedisCallback) connection -> { + Cursor cursor = connection.scan(options); + while (cursor.hasNext()) { + String key = new String(cursor.next()); + System.out.println(); + redisTemplate.expire(key, expireTime, TimeUnit.DAYS); + } + cursor.close(); + return null; + }); + } + +}