1.修改判断用户是否登录 AI 逻辑

This commit is contained in:
2025-08-27 21:40:38 +08:00
parent 85d62459a6
commit 417c2186c2
2 changed files with 42 additions and 2 deletions

View File

@@ -9,6 +9,7 @@ import com.yupi.springbootinit.model.vo.country.CountryInfoVO;
import com.yupi.springbootinit.rabbitMQ.MQSender;
import com.yupi.springbootinit.service.HostInfoService;
import com.yupi.springbootinit.utils.JsonUtils;
import com.yupi.springbootinit.utils.RedisUtils;
import com.yupi.springbootinit.utils.SseEmitterUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
@@ -40,6 +41,8 @@ public class HostInfoServiceImpl extends ServiceImpl<NewHostsMapper, NewHosts> i
private RedisTemplate redisTemplate;
@Resource
private RedisUtils redisUtils;
@Resource
private MQSender mqSender;
@@ -72,8 +75,7 @@ public class HostInfoServiceImpl extends ServiceImpl<NewHostsMapper, NewHosts> i
}
// Boolean o = (Boolean) redisTemplate.opsForValue().get("ai_login:"+newHosts.get(0).getTenantId());
Set<String> keys = redisTemplate.keys("ai_login:"+newHosts.get(0).getTenantId()+":*");
if (!keys.isEmpty()) {
if (redisUtils.hasKeyByPrefix("ai_login:"+ newHosts.get(0).getTenantId())) {
newHosts.forEach(newHost -> {
mqSender.send(newHost.getTenantId(),newHost);
});

View File

@@ -0,0 +1,38 @@
package com.yupi.springbootinit.utils;
/*
* @author: ziin
* @date: 2025/8/27 20:35
*/
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.stereotype.Component;
import javax.annotation.Resource;
import java.util.Set;
@Component
public class RedisUtils {
@Resource
private RedisTemplate<String,Object> redisTemplate;
public boolean hasKeyByPrefix(String prefix) {
return Boolean.TRUE.equals(
redisTemplate.execute((RedisCallback<Boolean>) conn -> {
try (Cursor<byte[]> cursor = conn.scan(
ScanOptions.scanOptions()
.match(prefix + ":*")
.count(100) // 每次返回条数,可调整
.build())) {
return cursor.hasNext(); // 只要存在一条就返回 true
}
})
);
}
}