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