From 8388db8f8e7d3a8d0a609e1840c2a4159b8b3fdd Mon Sep 17 00:00:00 2001 From: tianfeng <769204422@qq.com> Date: Tue, 17 Mar 2026 16:06:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Eredis=20viplevel=E9=87=8D?= =?UTF-8?q?=E7=BD=AE=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/test/java/ResetVipLevelTest.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 rc-service/rc-service-other/other-start/src/test/java/ResetVipLevelTest.java diff --git a/rc-service/rc-service-other/other-start/src/test/java/ResetVipLevelTest.java b/rc-service/rc-service-other/other-start/src/test/java/ResetVipLevelTest.java new file mode 100644 index 00000000..fce584f8 --- /dev/null +++ b/rc-service/rc-service-other/other-start/src/test/java/ResetVipLevelTest.java @@ -0,0 +1,55 @@ +import com.red.circle.OtherServiceApplication; +import lombok.extern.slf4j.Slf4j; +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) +@Slf4j +public class ResetVipLevelTest { + + @Autowired + private RedisTemplate redisTemplate; + + private static final String VIP_LEVEL_KEY_PATTERN = "GAME:VIP:LEVEL:*"; + private static final long EXPIRE_DAYS = 7; + + /** + * 批量将所有用户 VIP 等级缓存重置为 0,TTL 设置为 7 天 + */ + @Test + public void resetAllVipLevelToZero() { + ScanOptions options = ScanOptions.scanOptions() + .match(VIP_LEVEL_KEY_PATTERN) + .count(500) + .build(); + + int[] count = {0}; + + redisTemplate.execute((RedisCallback) connection -> { + Cursor cursor = connection.scan(options); + while (cursor.hasNext()) { + String key = new String(cursor.next()); + redisTemplate.opsForValue().set(key, 0, EXPIRE_DAYS, TimeUnit.DAYS); + count[0]++; + if (count[0] % 100 == 0) { + log.info("已处理: {} 条", count[0]); + } + } + cursor.close(); + return null; + }); + + log.info("完成,共重置 {} 个用户 VIP 缓存 -> 0,TTL={}天", count[0], EXPIRE_DAYS); + + } +}