parent
da13ea697f
commit
951219b8df
|
@ -22,7 +22,6 @@ import com.ljsd.jieling.logic.mission.GameEvent;
|
||||||
import com.ljsd.jieling.logic.mission.MissionType;
|
import com.ljsd.jieling.logic.mission.MissionType;
|
||||||
import com.ljsd.jieling.logic.player.PlayerLogic;
|
import com.ljsd.jieling.logic.player.PlayerLogic;
|
||||||
import com.ljsd.jieling.logic.store.newRechargeInfo.NewRechargeInfo;
|
import com.ljsd.jieling.logic.store.newRechargeInfo.NewRechargeInfo;
|
||||||
import com.ljsd.jieling.util.DynamicLRUCache;
|
|
||||||
import com.ljsd.jieling.util.ItemUtil;
|
import com.ljsd.jieling.util.ItemUtil;
|
||||||
import com.ljsd.jieling.util.SysUtil;
|
import com.ljsd.jieling.util.SysUtil;
|
||||||
import config.SGameSetting;
|
import config.SGameSetting;
|
||||||
|
@ -37,18 +36,21 @@ import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.ConcurrentMap;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
public class UserManager {
|
public class UserManager {
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(UserManager.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(UserManager.class);
|
||||||
// 玩家数据缓存
|
// 玩家数据缓存
|
||||||
private static final int maxMemoryLimit = 1000; // 最大内存限制,实际应由您的服务器规格决定
|
private static volatile int maxMemoryLimit = 1000; // 最大内存限制,实际应由您的服务器规格决定
|
||||||
private static final long LIVE_TIME = 60 * 60 * 1000L;// 离线玩家缓存保留时间(毫秒)
|
private static final int LIVE_TIME = 60 * 60 * 1000;// 离线玩家缓存保留时间(毫秒)
|
||||||
private static final DynamicLRUCache<Integer, User> userMap = new DynamicLRUCache<>(600, LIVE_TIME);
|
private static final Map<Integer, User> userMap = new ConcurrentHashMap<>();
|
||||||
private static final AtomicInteger login_f_num = new AtomicInteger(0);//登陆计数
|
private static final AtomicInteger login_f_num = new AtomicInteger(0);//登陆计数
|
||||||
|
|
||||||
|
|
||||||
public static void addUser(User user) {
|
public static void addUser(User user) {
|
||||||
|
user.setExpireTime(TimeUtils.now());
|
||||||
userMap.put(user.getId(), user);
|
userMap.put(user.getId(), user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,27 +71,43 @@ public class UserManager {
|
||||||
return OnlineUserManager.sessionMap.size();
|
return OnlineUserManager.sessionMap.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 这个方法可能由定时任务或某种监控服务调用
|
/**
|
||||||
public static void adjustCacheBasedOnCurrentLoad() {
|
* 离线清除逻辑
|
||||||
// 调用缓存的调整方法来根据在线玩家数量调整缓存大小
|
* 这个方法可能由定时任务或某种监控服务调用
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* 离线清除逻辑
|
||||||
|
* 这个方法可能由定时任务或某种监控服务调用
|
||||||
|
*/
|
||||||
|
public static void checkOffline(){
|
||||||
Thread.currentThread().setName("user_cache_clean");
|
Thread.currentThread().setName("user_cache_clean");
|
||||||
userMap.shrinkToFit();
|
long now = TimeUtils.now();
|
||||||
}
|
List<User> usersToRemove = new ArrayList<>();
|
||||||
|
|
||||||
//离线清除逻辑
|
try {
|
||||||
// public static void checkOffline(){
|
synchronized (userMap) { // 同步userMap以防止并发修改
|
||||||
// try {
|
for (User user : userMap.values()) {
|
||||||
// long now = TimeUtils.now();
|
if (OnlineUserManager.checkUidOnline(user.getId())){
|
||||||
// for (User user : userMap.getAll()) {
|
continue;
|
||||||
// if (OnlineUserManager.checkUidOnline(user.getId())){
|
}
|
||||||
// continue;
|
|
||||||
// }
|
// 修正逻辑错误:检查用户是否已过期
|
||||||
// }
|
if (now - user.getExpireTime() >= LIVE_TIME){
|
||||||
// LOGGER.info("checkOffline left count={}", getUserMapSize());
|
usersToRemove.add(user);
|
||||||
// }catch (Exception e){
|
}
|
||||||
// LOGGER.error("Exception离线清除逻辑err={}",e.toString());
|
}
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
|
// 移除过期的用户
|
||||||
|
for (User user : usersToRemove) {
|
||||||
|
removeUser(user.getId());
|
||||||
|
}
|
||||||
|
} catch (Exception e){
|
||||||
|
LOGGER.error("Exception in offline cleanup logic", e); // 记录完整的异常信息
|
||||||
|
}
|
||||||
|
|
||||||
|
LOGGER.info("checkOffline last_count={}, consumer_time:{}ms", getUserMapSize(), TimeUtils.now()-now);
|
||||||
|
}
|
||||||
|
|
||||||
public static User userLogin(int uid,String openId,String channel,int pid,int gid,String platform,String ip,String channle_id,String bundle_id,String ccId,String pack_Id) throws Exception {
|
public static User userLogin(int uid,String openId,String channel,int pid,int gid,String platform,String ip,String channle_id,String bundle_id,String ccId,String pack_Id) throws Exception {
|
||||||
User user;
|
User user;
|
||||||
|
|
|
@ -6,6 +6,7 @@ import com.ljsd.jieling.handler.map.MapManager;
|
||||||
import com.ljsd.jieling.jbean.ActivityManager;
|
import com.ljsd.jieling.jbean.ActivityManager;
|
||||||
import com.ljsd.jieling.jbean.gm.GmActivityManager;
|
import com.ljsd.jieling.jbean.gm.GmActivityManager;
|
||||||
import com.ljsd.jieling.logic.dao.*;
|
import com.ljsd.jieling.logic.dao.*;
|
||||||
|
import org.springframework.data.annotation.Transient;
|
||||||
import org.springframework.data.mongodb.core.index.CompoundIndex;
|
import org.springframework.data.mongodb.core.index.CompoundIndex;
|
||||||
import org.springframework.data.mongodb.core.mapping.Document;
|
import org.springframework.data.mongodb.core.mapping.Document;
|
||||||
|
|
||||||
|
@ -17,6 +18,9 @@ public class User {
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
|
@Transient
|
||||||
|
private long expireTime;//缓存时间
|
||||||
|
|
||||||
private PlayerManager playerManager;
|
private PlayerManager playerManager;
|
||||||
|
|
||||||
private ItemManager itemManager;
|
private ItemManager itemManager;
|
||||||
|
@ -60,9 +64,8 @@ public class User {
|
||||||
private MagicSoldierManager magicSoldierManager;
|
private MagicSoldierManager magicSoldierManager;
|
||||||
|
|
||||||
//TODO 新增manager 需要兼容旧的数据 提前判null
|
//TODO 新增manager 需要兼容旧的数据 提前判null
|
||||||
|
|
||||||
//构造函数必须要声明,否则从mongodb读出来反编译成类不通过
|
|
||||||
public User(){
|
public User(){
|
||||||
|
//构造函数必须要声明,否则从mongodb读出来反编译成类不通过
|
||||||
}
|
}
|
||||||
|
|
||||||
public User(int uid){
|
public User(int uid){
|
||||||
|
@ -293,4 +296,12 @@ public class User {
|
||||||
public MainLevelManager getMainLevelManager() {
|
public MainLevelManager getMainLevelManager() {
|
||||||
return mainLevelManager;
|
return mainLevelManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long getExpireTime() {
|
||||||
|
return expireTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExpireTime(long expireTime) {
|
||||||
|
this.expireTime = expireTime;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -121,7 +121,7 @@ public class ThreadManager implements IManager {
|
||||||
// 战力任务
|
// 战力任务
|
||||||
TaskKit.scheduleAtFixedRate(new ForceRankTask(), 60,5, TimeUnit.SECONDS);
|
TaskKit.scheduleAtFixedRate(new ForceRankTask(), 60,5, TimeUnit.SECONDS);
|
||||||
// 每小时减容用户缓存
|
// 每小时减容用户缓存
|
||||||
TaskKit.scheduleAtFixedRate(UserManager::adjustCacheBasedOnCurrentLoad, 3, 1, TimeUnit.HOURS);
|
TaskKit.scheduleAtFixedRate(UserManager::checkOffline, 2, 1, TimeUnit.HOURS);
|
||||||
// 定时重启线程池
|
// 定时重启线程池
|
||||||
scheduledFutureSets.add(scheduledExecutor.scheduleAtFixedRate(new Thread(() -> {
|
scheduledFutureSets.add(scheduledExecutor.scheduleAtFixedRate(new Thread(() -> {
|
||||||
long lastMilis = System.currentTimeMillis();
|
long lastMilis = System.currentTimeMillis();
|
||||||
|
|
Loading…
Reference in New Issue