azizi test
This commit is contained in:
parent
700a4e1a70
commit
364b0c7068
@ -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<Object>) connection -> {
|
||||
Cursor<byte[]> 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;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user