1. 添加“显示ID”字段,用来给前端展示任务/规则/防护对象的自定义ID(非主键ID)

2. 新增Counter类,新增Redis相关以来
This commit is contained in:
EnderByEndera
2024-04-29 15:33:09 +08:00
parent 7086af3832
commit 4c30c719d9
22 changed files with 214 additions and 38 deletions

View File

@@ -0,0 +1,37 @@
package com.realtime.protection.configuration.utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class Counter {
private final StringRedisTemplate stringRedisTemplate;
public Counter(StringRedisTemplate stringRedisTemplate) {
this.stringRedisTemplate = stringRedisTemplate;
}
public Long generateId(String typeName) {
String key = String.format("counter::%s", typeName.toLowerCase());
BoundValueOperations<String, String> op = stringRedisTemplate.boundValueOps(key);
return op.increment();
}
@Scheduled(cron = "0 0 0 * * ?")
public void resetId() {
Cursor<String> cursor = stringRedisTemplate.scan(ScanOptions.scanOptions().build());
while (cursor.hasNext()) {
String key = cursor.next();
if (key.startsWith("counter::")) {
stringRedisTemplate.delete(key);
log.debug("删除计时器key: {}", key);
}
}
}
}