火箭能量更新roomKey处理
This commit is contained in:
parent
1be939fd0e
commit
ac034f9cc4
@ -15,6 +15,7 @@ import com.red.circle.other.infra.database.mongo.service.live.RoomProfileManager
|
|||||||
import com.red.circle.other.infra.gateway.RocketStatusGatewayImpl;
|
import com.red.circle.other.infra.gateway.RocketStatusGatewayImpl;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@ -43,6 +44,8 @@ public class RocketEnergyAggregator {
|
|||||||
private final RocketLaunchManager rocketLaunchManager;
|
private final RocketLaunchManager rocketLaunchManager;
|
||||||
private final RoomProfileManagerService roomProfileManagerService;
|
private final RoomProfileManagerService roomProfileManagerService;
|
||||||
private final StringRedisTemplate redisTemplate;
|
private final StringRedisTemplate redisTemplate;
|
||||||
|
@Value("${spring.profiles.active}")
|
||||||
|
private String activeProfile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Redis 队列 Key 前缀
|
* Redis 队列 Key 前缀
|
||||||
@ -79,20 +82,29 @@ public class RocketEnergyAggregator {
|
|||||||
*/
|
*/
|
||||||
private final Map<Long, Long> lastPushTime = new ConcurrentHashMap<>();
|
private final Map<Long, Long> lastPushTime = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
// 根据环境动态生成 Key 前缀
|
||||||
|
private String getKey(String roomId) {
|
||||||
|
return activeProfile + ":" + RocketEnergyAggregator.ENERGY_QUEUE_PREFIX + roomId;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getActiveRoomsKey() {
|
||||||
|
return activeProfile + ":" + RocketEnergyAggregator.ACTIVE_ROOMS_KEY;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 接收能量增加请求(写入Redis队列)
|
* 接收能量增加请求(写入Redis队列)
|
||||||
*/
|
*/
|
||||||
public void addEnergy(RocketEnergyAddCmd cmd) {
|
public void addEnergy(RocketEnergyAddCmd cmd) {
|
||||||
try {
|
try {
|
||||||
Long roomId = cmd.getRoomId();
|
Long roomId = cmd.getRoomId();
|
||||||
String queueKey = ENERGY_QUEUE_PREFIX + roomId;
|
String queueKey = getKey(String.valueOf(roomId)) ;
|
||||||
String value = JSON.toJSONString(cmd);
|
String value = JSON.toJSONString(cmd);
|
||||||
|
|
||||||
// 写入队列
|
// 写入队列
|
||||||
redisTemplate.opsForList().rightPush(queueKey, value);
|
redisTemplate.opsForList().rightPush(queueKey, value);
|
||||||
|
|
||||||
// 记录活跃房间(使用 Set 去重)
|
// 记录活跃房间(使用 Set 去重)
|
||||||
redisTemplate.opsForSet().add(ACTIVE_ROOMS_KEY, roomId.toString());
|
redisTemplate.opsForSet().add(getActiveRoomsKey(), roomId.toString());
|
||||||
|
|
||||||
// 设置队列过期时间(24小时)
|
// 设置队列过期时间(24小时)
|
||||||
redisTemplate.expire(queueKey, 24, TimeUnit.HOURS);
|
redisTemplate.expire(queueKey, 24, TimeUnit.HOURS);
|
||||||
@ -113,7 +125,7 @@ public class RocketEnergyAggregator {
|
|||||||
public void flushToDatabase() {
|
public void flushToDatabase() {
|
||||||
try {
|
try {
|
||||||
// 获取所有活跃房间
|
// 获取所有活跃房间
|
||||||
Set<String> activeRooms = redisTemplate.opsForSet().members(ACTIVE_ROOMS_KEY);
|
Set<String> activeRooms = redisTemplate.opsForSet().members(getActiveRoomsKey());
|
||||||
|
|
||||||
if (activeRooms == null || activeRooms.isEmpty()) {
|
if (activeRooms == null || activeRooms.isEmpty()) {
|
||||||
log.debug("没有待处理的房间");
|
log.debug("没有待处理的房间");
|
||||||
@ -172,14 +184,14 @@ public class RocketEnergyAggregator {
|
|||||||
* @return 是否处理了数据
|
* @return 是否处理了数据
|
||||||
*/
|
*/
|
||||||
private boolean processRoomEnergyFromRedis(Long roomId) {
|
private boolean processRoomEnergyFromRedis(Long roomId) {
|
||||||
String queueKey = ENERGY_QUEUE_PREFIX + roomId;
|
String queueKey = getKey(String.valueOf(roomId)) ;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 检查队列长度
|
// 检查队列长度
|
||||||
Long queueSize = redisTemplate.opsForList().size(queueKey);
|
Long queueSize = redisTemplate.opsForList().size(queueKey);
|
||||||
if (queueSize == null || queueSize == 0) {
|
if (queueSize == null || queueSize == 0) {
|
||||||
// 队列为空,从活跃房间列表中移除
|
// 队列为空,从活跃房间列表中移除
|
||||||
redisTemplate.opsForSet().remove(ACTIVE_ROOMS_KEY, roomId.toString());
|
redisTemplate.opsForSet().remove(getActiveRoomsKey(), roomId.toString());
|
||||||
log.debug("房间{}队列为空,从活跃列表移除", roomId);
|
log.debug("房间{}队列为空,从活跃列表移除", roomId);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -338,7 +350,7 @@ public class RocketEnergyAggregator {
|
|||||||
public void cleanupExpiredData() {
|
public void cleanupExpiredData() {
|
||||||
try {
|
try {
|
||||||
// 清理本地推送时间缓存
|
// 清理本地推送时间缓存
|
||||||
Set<String> activeRooms = redisTemplate.opsForSet().members(ACTIVE_ROOMS_KEY);
|
Set<String> activeRooms = redisTemplate.opsForSet().members(getActiveRoomsKey());
|
||||||
if (activeRooms != null) {
|
if (activeRooms != null) {
|
||||||
Set<Long> activeRoomIds = new HashSet<>();
|
Set<Long> activeRoomIds = new HashSet<>();
|
||||||
for (String roomIdStr : activeRooms) {
|
for (String roomIdStr : activeRooms) {
|
||||||
@ -385,7 +397,7 @@ public class RocketEnergyAggregator {
|
|||||||
public Map<String, Object> getQueueStatus() {
|
public Map<String, Object> getQueueStatus() {
|
||||||
Map<String, Object> status = new HashMap<>();
|
Map<String, Object> status = new HashMap<>();
|
||||||
|
|
||||||
Set<String> activeRooms = redisTemplate.opsForSet().members(ACTIVE_ROOMS_KEY);
|
Set<String> activeRooms = redisTemplate.opsForSet().members(getActiveRoomsKey());
|
||||||
status.put("activeRoomCount", activeRooms != null ? activeRooms.size() : 0);
|
status.put("activeRoomCount", activeRooms != null ? activeRooms.size() : 0);
|
||||||
status.put("localPushCacheSize", lastPushTime.size());
|
status.put("localPushCacheSize", lastPushTime.size());
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user