This commit is contained in:
jack ning
2025-11-24 15:43:56 +08:00
parent d53b5ab095
commit 4ddef78f0a
4 changed files with 92 additions and 71 deletions

View File

@@ -50,4 +50,9 @@ public class RedisConsts {
// 验证码相关常量
public static final String KAPTCHA_PREFIX = BYTEDESK_REDIS_PREFIX + "kaptcha:";
// Redis 缓存心跳Key
public static final String REDIS_HEARTBEAT_HASH_KEY = RedisConsts.BYTEDESK_REDIS_PREFIX + "core:conn:hb";
// Redis 最近一次数据库写入时间Key
public static final String REDIS_LAST_DB_WRITE_HASH_KEY = RedisConsts.BYTEDESK_REDIS_PREFIX + "core:conn:hb:lastdb";
}

View File

@@ -25,9 +25,7 @@ public class ConnectionHeartbeatFlushTask {
private final StringRedisTemplate stringRedisTemplate;
private final ConnectionRestService connectionRestService;
private static final String REDIS_HEARTBEAT_HASH_KEY = RedisConsts.BYTEDESK_REDIS_PREFIX + "core:conn:hb";
private static final String REDIS_LAST_DB_WRITE_HASH_KEY = RedisConsts.BYTEDESK_REDIS_PREFIX + "core:conn:hb:lastdb";
// 每 10 秒批量刷新一次
@Scheduled(fixedDelay = 10_000)
@Transactional
@@ -37,7 +35,7 @@ public class ConnectionHeartbeatFlushTask {
}
try {
long start = System.nanoTime();
Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries(REDIS_HEARTBEAT_HASH_KEY);
Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries(RedisConsts.REDIS_HEARTBEAT_HASH_KEY);
if (entries == null || entries.isEmpty()) {
return;
}
@@ -62,12 +60,12 @@ public class ConnectionHeartbeatFlushTask {
for (Map.Entry<String, Long> e : heartbeats.entrySet()) {
String clientId = e.getKey();
Long hbTs = e.getValue();
Object lastDbStr = stringRedisTemplate.opsForHash().get(REDIS_LAST_DB_WRITE_HASH_KEY, clientId);
Object lastDbStr = stringRedisTemplate.opsForHash().get(RedisConsts.REDIS_LAST_DB_WRITE_HASH_KEY, clientId);
if (lastDbStr != null) {
try {
long lastDb = Long.parseLong(String.valueOf(lastDbStr));
if (lastDb >= hbTs) {
stringRedisTemplate.opsForHash().delete(REDIS_HEARTBEAT_HASH_KEY, clientId);
stringRedisTemplate.opsForHash().delete(RedisConsts.REDIS_HEARTBEAT_HASH_KEY, clientId);
}
} catch (NumberFormatException ignore) {}
}

View File

@@ -28,6 +28,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import com.bytedesk.core.base.BaseRestServiceWithExport;
import com.bytedesk.core.constant.RedisConsts;
import com.bytedesk.core.rbac.auth.AuthService;
import com.bytedesk.core.rbac.user.UserEntity;
import com.bytedesk.core.uid.UidUtils;
@@ -56,10 +57,7 @@ public class ConnectionRestService extends BaseRestServiceWithExport<ConnectionE
private final StringRedisTemplate stringRedisTemplate;
// Redis 缓存心跳Key
private static final String REDIS_HEARTBEAT_HASH_KEY = "core:conn:hb";
// Redis 最近一次数据库写入时间Key
private static final String REDIS_LAST_DB_WRITE_HASH_KEY = "core:conn:hb:lastdb";
// 最小数据库写入间隔(毫秒)
private static final long MIN_INTERVAL_MS = 5000L;
@@ -134,7 +132,7 @@ public class ConnectionRestService extends BaseRestServiceWithExport<ConnectionE
private void tryWriteHeartbeatToCache(String clientId, long ts) {
try {
if (stringRedisTemplate != null) {
stringRedisTemplate.opsForHash().put(REDIS_HEARTBEAT_HASH_KEY, clientId, String.valueOf(ts));
stringRedisTemplate.opsForHash().put(RedisConsts.REDIS_HEARTBEAT_HASH_KEY, clientId, String.valueOf(ts));
}
} catch (Exception ignore) {}
}
@@ -142,7 +140,7 @@ public class ConnectionRestService extends BaseRestServiceWithExport<ConnectionE
private void cacheLastDbWrite(String clientId, long ts) {
try {
if (stringRedisTemplate != null) {
stringRedisTemplate.opsForHash().put(REDIS_LAST_DB_WRITE_HASH_KEY, clientId, String.valueOf(ts));
stringRedisTemplate.opsForHash().put(RedisConsts.REDIS_LAST_DB_WRITE_HASH_KEY, clientId, String.valueOf(ts));
}
} catch (Exception ignore) {}
}