From 393635b930677df8a789181488598b5d2aa13355 Mon Sep 17 00:00:00 2001 From: duhui Date: Fri, 10 Sep 2021 15:57:48 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ljsd/jieling/db/mongo/AreaManager.java | 16 +- .../com/ljsd/jieling/db/redis/RedisKey.java | 52 +- .../com/ljsd/jieling/db/redis/RedisUtil.java | 71 +- .../handler/map/behavior/BehaviorUtil.java | 2 +- .../fourChallenge/FourChallengeLogic.java | 6 +- .../jieling/logic/dao/TeamPosManager.java | 3 +- .../jieling/logic/family/DeathPathLogic.java | 3 +- .../ljsd/jieling/logic/fight/CombatLogic.java | 16 +- .../jieling/logic/help/HelpHeroLogic.java | 105 +- .../ljsd/jieling/logic/hero/HeroLogic.java | 8 +- .../com/ljsd/jieling/util/CBean2Proto.java | 3 +- .../main/java/com/WorldServerApplication.java | 10 +- .../AddDeathPathRankRequestHandler.java | 136 +- .../GetDeathPathFirstRequestHandler.java | 90 +- .../world/handler/GetRankRequestHandler.java | 74 +- .../com/world/logic/arean/ArenaLogic.java | 96 +- .../main/java/com/world/redis/RedisKey.java | 16 +- .../main/java/com/world/redis/RedisUtil.java | 1588 ++++++++--------- .../java/com/world/thread/MinuteTask.java | 12 +- 19 files changed, 1148 insertions(+), 1159 deletions(-) diff --git a/serverlogic/src/main/java/com/ljsd/jieling/db/mongo/AreaManager.java b/serverlogic/src/main/java/com/ljsd/jieling/db/mongo/AreaManager.java index ebc6e4fe6..a4d7ff5cb 100644 --- a/serverlogic/src/main/java/com/ljsd/jieling/db/mongo/AreaManager.java +++ b/serverlogic/src/main/java/com/ljsd/jieling/db/mongo/AreaManager.java @@ -419,17 +419,11 @@ public class AreaManager { } public int getServerIdByUid(int uid, int defaultServerId) { - int serverId = defaultServerId; - Integer serverIdByUid = RedisUtil.getInstence().getMapEntry(RedisKey.CUser_ServerId_Key, "", Integer.toString(uid), - Integer.class); -// serverIdByUid = MongoUtil.getInstence().findCUserServerId(uid); - if (serverIdByUid != null) { - serverId = serverIdByUid; - RedisUtil.getInstence().putMapEntry(RedisKey.CUser_ServerId_Key, "", Integer.toString(uid), - serverId); - } else { - return GameApplication.serverId; + Integer serverIdByUid = RedisUtil.getInstence().getMapEntry(RedisKey.CUser_ServerId_Key,"",Integer.toString(uid),Integer.class); + if (serverIdByUid == null){ + RedisUtil.getInstence().putMapEntry(RedisKey.CUser_ServerId_Key, "", Integer.toString(uid), defaultServerId); + serverIdByUid = defaultServerId; } - return serverId; + return serverIdByUid; } } diff --git a/serverlogic/src/main/java/com/ljsd/jieling/db/redis/RedisKey.java b/serverlogic/src/main/java/com/ljsd/jieling/db/redis/RedisKey.java index d71d5293f..01c1c510f 100644 --- a/serverlogic/src/main/java/com/ljsd/jieling/db/redis/RedisKey.java +++ b/serverlogic/src/main/java/com/ljsd/jieling/db/redis/RedisKey.java @@ -188,20 +188,22 @@ public class RedisKey { public static final String SESSION_OFFLINE = "SESSION_OFFLINE"; + // 十绝阵rediskey public static final String DEATH_PATH_EVERY_PERSON_RANK = "DEATH_PATH_EVERY_PERSON_RANK"; public static final String DEATH_PATH_EVERY_GUILD_RANK = "DEATH_PATH_EVERY_GUILD_RANK"; - + // 十绝阵单人挑战次数 public static final String DEATH_PATH_CHALLENGE_COUNT = "DEATH_PATH_CHALLENGE_COUNT"; public static final String DEATH_PATH_TOTAL_PERSON_RANK = "DEATH_PATH_TOTAL_PERSON_RANK"; public static final String DEATH_PATH_TOTAL_GUILD_RANK = "DEATH_PATH_TOTAL_GUILD_RANK"; - - public static final String DEATH_PATH_EVERY_OVER_HASH = "DEATH_PATH_EVERY_OVER_HASH";//结算标识 - - // 死亡之旅挑战第一名推送 + // 结算标识 + public static final String DEATH_PATH_EVERY_OVER_HASH = "DEATH_PATH_EVERY_OVER_HASH"; + // 十绝阵挑战第一名推送 public static final String DEATH_PATH_CHALLENGE_FIRST_PUSH = "DEATH_PATH_CHALLENGE_FIRST_PUSH"; + // 十绝阵挑战人数 + public static final String DEATH_PATH_TOTAL_CHALLENGE_COUNT = "DEATH_PATH_TOTAL_CHALLENGE_COUNT"; //问卷调查 public static final String QUESTION_FROMBACK = "QUESTION_FROMBACK"; @@ -257,9 +259,6 @@ public class RedisKey { public static final String HARD_STAGE_PASS_MIN_FORCE = "HARD_STAGE_PASS_MIN_FORCE"; public static final String TA_SUI_LING_XIAO_RANK = "TA_SUI_LING_XIAO_RANK"; - public static final String DEATH_PATH_TOTAL_CHALLENGE_COUNT = "DEATH_PATH_TOTAL_CHALLENGE_COUNT"; - - public final static String CAR_CHALLENGE_RECORD = "CAR_CHALLENGE_RECORD"; //车迟玩法挑战记录 public final static String USER_LEVEL_GUILD_INFO = "USER_LEVEL_GUILD_INFO"; //车迟玩法挑战记录 @@ -367,16 +366,20 @@ public class RedisKey { * 助战 * * 第一种 - * key:服务器id:HELP_FIGHT:MY:玩家id + * key: 服务器id:HELP_FIGHT:MY:玩家id * value:map《助战类型,助战英雄》 * 第二种 - * key:服务器id:HELP_FIGHT:CHOOSE:玩家id + * key: 服务器id:HELP_FIGHT:CHOOSE:玩家id * value:map《助战类型,助战英雄》 * 第三种 - * key:服务器id:HELP_FIGHT:助战类型 + * key: 服务器id:HELP_FIGHT:助战类型 * value:map《玩家id,助战英雄》 */ public final static String HELP_FIGHT = "HELP_FIGHT"; + /** + * 助战删除的key + */ + public final static String HELP_FIGHT_DELETE = "HELP_FIGHT_DELETE"; public static Set familyKey = new HashSet<>(); @@ -420,7 +423,6 @@ public class RedisKey { rankCacChe.add(SITUATION_RANK); rankCacChe.add(FOUR_CHALLENGE_TIER_RANK); - rankCacChe.add(HELP_FIGHT); } static { @@ -448,31 +450,7 @@ public class RedisKey { // familyKey.add(FAMILY_FIGHT_FINISH_MATCHING); } public static String getKey(String type, String key, boolean withoutServerId){ - if(RedisKey.TOKEN.equals(type)||RedisKey.USER_SERVER_INFO.equals(type)||RedisKey.SERVER_WORLDLEVE_INFO.equals(type)){ - return type + RedisKey.Delimiter_colon + key; - } - if(familyKey.contains(type)){ - return type + RedisKey.Delimiter_colon+key; - } - - if(!newAreaCacChe.contains(type)&&!rankCacChe.contains(type)){ - if(key.length() == 8) { - - Integer serverId = RedisUtil.getInstence().getMapEntry(RedisKey.CUser_ServerId_Key, "", key, - Integer.class); - if( serverId!=null && !AreaManager.getInstance().checkServerIdAllowPermit(serverId)){ - return -1 + RedisKey.Delimiter_colon + type + RedisKey.Delimiter_colon + key; - } - return serverId + RedisKey.Delimiter_colon + type + RedisKey.Delimiter_colon + key; -// } - } - } - - // if (rankCacChe.contains(type)||withoutServerId) { - return AreaManager.areaId + Delimiter_colon + type + Delimiter_colon + String.valueOf(key); -// } else { -// return GameApplication.serverId + Delimiter_colon + type + Delimiter_colon + String.valueOf(key); -// } + return RedisUtil.getInstence().getKey(type,key); } } diff --git a/serverlogic/src/main/java/com/ljsd/jieling/db/redis/RedisUtil.java b/serverlogic/src/main/java/com/ljsd/jieling/db/redis/RedisUtil.java index 378bc2109..0785d6676 100644 --- a/serverlogic/src/main/java/com/ljsd/jieling/db/redis/RedisUtil.java +++ b/serverlogic/src/main/java/com/ljsd/jieling/db/redis/RedisUtil.java @@ -24,6 +24,7 @@ import java.util.*; import java.util.concurrent.TimeUnit; import java.util.function.BiFunction; +@SuppressWarnings("ALL") public class RedisUtil { private static final Logger LOGGER = LoggerFactory.getLogger(RedisUtil.class); @@ -1342,38 +1343,58 @@ public class RedisUtil { * @return */ public String getKey(String type,String key,boolean judge){ - if (!judge){ - return type + RedisKey.Delimiter_colon + key; + String result = null; + if (!judge) { + result = type + RedisKey.Delimiter_colon + key; } - - if(RedisKey.PLAYER_INFO_CACHE.equals(type) ||RedisKey.FAMILY_INFO.equals(type)|| - RedisKey.PIDGIDTEMP.equals(type)||RedisKey.SERVER_WORLDLEVE_INFO.equals(type)|| - RedisKey.GUILD_INFO_CACHE.equals(type)||RedisKey.SERVER_SPLIT_INFO.equals(type)|| - RedisKey.SERVER_SPLIT_MAIL_INFO.equals(type)||RedisKey.CROSS_ARENA_ROBOT_INFO.equals(type)|| - RedisKey.CROSS_SERVICE_ARENA.equals(type)||RedisKey.WORLD_ARENA_RANK_MY_MATH.equals(type)|| - RedisKey.WORLD_ARENA_MY_PRON.equals(type)||RedisKey.WORLD_ARENA_RRECORD.equals(type)) { - return type + RedisKey.Delimiter_colon + key; + else if ( + RedisKey.PLAYER_INFO_CACHE.equals(type) || RedisKey.FAMILY_INFO.equals(type) || + RedisKey.PIDGIDTEMP.equals(type) || RedisKey.SERVER_WORLDLEVE_INFO.equals(type) || + RedisKey.GUILD_INFO_CACHE.equals(type) || RedisKey.SERVER_SPLIT_INFO.equals(type) || + RedisKey.SERVER_SPLIT_MAIL_INFO.equals(type) || RedisKey.CROSS_ARENA_ROBOT_INFO.equals(type) || + RedisKey.CROSS_SERVICE_ARENA.equals(type) || RedisKey.WORLD_ARENA_RANK_MY_MATH.equals(type) || + RedisKey.WORLD_ARENA_MY_PRON.equals(type) || RedisKey.WORLD_ARENA_RRECORD.equals(type) || + RedisKey.TOKEN.equals(type) || RedisKey.USER_SERVER_INFO.equals(type) || + RedisKey.CDKEY.equals(type) || RedisKey.ServerArenaJob.equals(type) + ) { + result = type + RedisKey.Delimiter_colon + key; } - - if(RedisKey.TOKEN.equals(type)||RedisKey.USER_SERVER_INFO.equals(type)|| - RedisKey.CDKEY.equals(type)||RedisKey.ServerArenaJob.equals(type)){ - return type + RedisKey.Delimiter_colon + key; - } - - if(!RedisKey.newAreaCacChe.contains(type)&&!RedisKey.rankCacChe.contains(type)){ - if(key.length() == 8) { - Integer serverId = RedisUtil.getInstence().getObject(RedisKey.CUser_ServerId_Key,key, - Integer.class, -1); - if(serverId!=null && !AreaManager.getInstance().checkServerIdAllowPermit(serverId)){ - return -1 + RedisKey.Delimiter_colon + type + RedisKey.Delimiter_colon + key; + else if (!RedisKey.newAreaCacChe.contains(type) && !RedisKey.rankCacChe.contains(type)) { + if (key.length() == 8) { + Integer serverId = RedisUtil.getInstence().getObject(RedisKey.CUser_ServerId_Key, key, Integer.class, -1); + if (serverId != null && !AreaManager.getInstance().checkServerIdAllowPermit(serverId)) { + result = -1 + RedisKey.Delimiter_colon + type + RedisKey.Delimiter_colon + key; + } + else if (serverId != null){ + result = serverId + RedisKey.Delimiter_colon + type + RedisKey.Delimiter_colon + key; } - return serverId + RedisKey.Delimiter_colon + type + RedisKey.Delimiter_colon + key; } - } + } + if (result == null){ + result = AreaManager.areaId + RedisKey.Delimiter_colon + type + RedisKey.Delimiter_colon + key; + } - return AreaManager.areaId+ RedisKey.Delimiter_colon + type + RedisKey.Delimiter_colon + key; + helpHeroKeyHandler(result); + return result; } + /** + * 十绝阵rediskey处理 + */ + private void deathPathKeyHandler(String key){ + if(key.contains(RedisKey.HELP_FIGHT)){ + RedisUtil.getInstence().sSet(RedisKey.HELP_FIGHT_DELETE,key); + } + } + + /** + * 助战rediskey处理 + */ + private void helpHeroKeyHandler(String key){ + if(key.contains(RedisKey.HELP_FIGHT)){ + RedisUtil.getInstence().sSet(RedisKey.HELP_FIGHT_DELETE,key); + } + } private static final String LOCK_SUCCESS = "OK"; private static final byte[] SET_IF_NOT_EXIST = "NX".getBytes(); diff --git a/serverlogic/src/main/java/com/ljsd/jieling/handler/map/behavior/BehaviorUtil.java b/serverlogic/src/main/java/com/ljsd/jieling/handler/map/behavior/BehaviorUtil.java index 0b3d59598..482f08735 100644 --- a/serverlogic/src/main/java/com/ljsd/jieling/handler/map/behavior/BehaviorUtil.java +++ b/serverlogic/src/main/java/com/ljsd/jieling/handler/map/behavior/BehaviorUtil.java @@ -247,7 +247,7 @@ public class BehaviorUtil { if (hero == null) { // 四灵试炼,助战 if (FourChallengeLogic.isFourChallengeTeam(teamId)){ - HelpHero helpHero = HelpHeroLogic.getHelpHero(user.getId(), teamPosHeroInfo.getHeroId()); + HelpHero helpHero = HelpHeroLogic.getHelpHeroByHeroId(user.getId(), teamPosHeroInfo.getHeroId()); if (helpHero == null){ continue; } diff --git a/serverlogic/src/main/java/com/ljsd/jieling/logic/activity/fourChallenge/FourChallengeLogic.java b/serverlogic/src/main/java/com/ljsd/jieling/logic/activity/fourChallenge/FourChallengeLogic.java index 20e7b2ca8..399b9377c 100644 --- a/serverlogic/src/main/java/com/ljsd/jieling/logic/activity/fourChallenge/FourChallengeLogic.java +++ b/serverlogic/src/main/java/com/ljsd/jieling/logic/activity/fourChallenge/FourChallengeLogic.java @@ -187,7 +187,7 @@ public class FourChallengeLogic { for (TeamPosHeroInfo info : teamPosHeroInfos) { Hero hero = user.getHeroManager().getHero(info.getHeroId()); if (hero == null){ - helpHero = HelpHeroLogic.getHelpHero(user.getId(), info.getHeroId()); + helpHero = HelpHeroLogic.getHelpHeroByHeroId(user.getId(), info.getHeroId()); if (helpHero == null){ throw new ErrorCodeException(ErrorCode.SERVER_SELF_DEFINE,"挑战编队英雄不存在"); } @@ -229,7 +229,9 @@ public class FourChallengeLogic { if (help == 1){ // 助战英雄使用状态修改 helpHero.setState(1); - HelpHeroLogic.addHelpHero(user.getId(),campId,helpHero); + // redis存储 + String subKey = HelpHeroLogic.subKey(user.getId(), HelpHeroLogic.CHOOSE); + HelpHeroLogic.addHelpHero(subKey,campId,helpHero); // 发送奖励 HelpHeroLogic.getInstance().useHelpSendReward(helpHero); // 删除队伍中英雄id diff --git a/serverlogic/src/main/java/com/ljsd/jieling/logic/dao/TeamPosManager.java b/serverlogic/src/main/java/com/ljsd/jieling/logic/dao/TeamPosManager.java index 9dfefb184..af3558d25 100644 --- a/serverlogic/src/main/java/com/ljsd/jieling/logic/dao/TeamPosManager.java +++ b/serverlogic/src/main/java/com/ljsd/jieling/logic/dao/TeamPosManager.java @@ -128,7 +128,8 @@ public class TeamPosManager extends MongoBase { * @param user */ public void clearTeamPosByFourChallenge(User user){ - Map helpHero = HelpHeroLogic.getHelpHeroMap(user.getId()); + String subKey = HelpHeroLogic.subKey(user.getId(),HelpHeroLogic.CHOOSE); + Map helpHero = HelpHeroLogic.getHelpHeroMap(subKey); helpHero.values().forEach(v->{ removeTeamPosByHeroId(GlobalsDef.FOURCHALLENGE_PEOPLE_TEAM,v.getHero().getId()); removeTeamPosByHeroId(GlobalsDef.FOURCHALLENGE_BUDDHA_TEAM,v.getHero().getId()); diff --git a/serverlogic/src/main/java/com/ljsd/jieling/logic/family/DeathPathLogic.java b/serverlogic/src/main/java/com/ljsd/jieling/logic/family/DeathPathLogic.java index e7a2276f0..0fbe3aa1e 100644 --- a/serverlogic/src/main/java/com/ljsd/jieling/logic/family/DeathPathLogic.java +++ b/serverlogic/src/main/java/com/ljsd/jieling/logic/family/DeathPathLogic.java @@ -303,7 +303,8 @@ public class DeathPathLogic { */ public void getBaseInfo(ISession session,MessageTypeProto.MessageType messageType){ int uid = session.getUid(); - String changeString = GameApplication.serverProperties.getId() + RedisKey.Delimiter_colon + RedisKey.DEATH_PATH_CHALLENGE_COUNT + RedisKey.Delimiter_colon + String.valueOf(uid); + String type = GameApplication.serverProperties.getId() + RedisKey.Delimiter_colon + RedisKey.DEATH_PATH_CHALLENGE_COUNT; + String changeString = RedisUtil.getInstence().getKey(type,String.valueOf(uid),false); String count = (String) RedisUtil.getInstence().get(changeString); int challengeCount = count==null?0:Integer.parseInt(count); // 结束时间和挑战次数 diff --git a/serverlogic/src/main/java/com/ljsd/jieling/logic/fight/CombatLogic.java b/serverlogic/src/main/java/com/ljsd/jieling/logic/fight/CombatLogic.java index a605486e9..e4ffc9e57 100644 --- a/serverlogic/src/main/java/com/ljsd/jieling/logic/fight/CombatLogic.java +++ b/serverlogic/src/main/java/com/ljsd/jieling/logic/fight/CombatLogic.java @@ -774,7 +774,7 @@ public class CombatLogic { } }else if (FourChallengeLogic.isFourChallengeTeam(teamId)){ // 四灵试炼,助战 - HelpHero helpHero = HelpHeroLogic.getHelpHero(user.getId(), teamPosHeroInfo.getHeroId()); + HelpHero helpHero = HelpHeroLogic.getHelpHeroByHeroId(user.getId(), teamPosHeroInfo.getHeroId()); if (helpHero != null){ hero = helpHero.getHero(); }else { @@ -835,7 +835,7 @@ public class CombatLogic { } // 四灵试炼 if (FourChallengeLogic.isFourChallengeTeam(teamId)){ - HelpHero helpHero = HelpHeroLogic.getHelpHero(user.getId(), key.getHeroId()); + HelpHero helpHero = HelpHeroLogic.getHelpHeroByHeroId(user.getId(), key.getHeroId()); if (helpHero != null){ heroFind.add(helpHero.getHero().getTemplateId()); continue; @@ -845,7 +845,6 @@ public class CombatLogic { heroFind.add(hero.getTemplateId()); } - Map propertiesMap = new HashMap<>(); for(Integer heroId:heroFind){ if(null==SCHero.sCHero.get(heroId)){ @@ -890,14 +889,9 @@ public class CombatLogic { public static > Map sortByValueDescending(Map map) { List> list = new LinkedList>(map.entrySet()); - Collections.sort(list, new Comparator>() - { - @Override - public int compare(Map.Entry o1, Map.Entry o2) - { - int compare = (o1.getValue()).compareTo(o2.getValue()); - return -compare; - } + list.sort((o1, o2) -> { + int compare = (o1.getValue()).compareTo(o2.getValue()); + return -compare; }); Map result = new LinkedHashMap(); diff --git a/serverlogic/src/main/java/com/ljsd/jieling/logic/help/HelpHeroLogic.java b/serverlogic/src/main/java/com/ljsd/jieling/logic/help/HelpHeroLogic.java index 2f31e2e39..ff3552673 100644 --- a/serverlogic/src/main/java/com/ljsd/jieling/logic/help/HelpHeroLogic.java +++ b/serverlogic/src/main/java/com/ljsd/jieling/logic/help/HelpHeroLogic.java @@ -23,6 +23,7 @@ import manager.STableManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import rpc.protocols.CommonProto; +import util.StringUtil; import util.TimeUtils; import java.util.*; @@ -57,7 +58,7 @@ public class HelpHeroLogic { */ public static final String CHOOSE = "CHOOSE"; - public static String getSubKey(int uid,String sub){ + public static String subKey(int uid,String sub){ return sub+":"+uid; } @@ -72,10 +73,15 @@ public class HelpHeroLogic { HashSet result = new HashSet<>(); // 如果类型是自己,则使用uid - String subKey = typeEnum==Me?getSubKey(user.getId(),MY):String.valueOf(typeEnum.getPropertyId()); + String subKey; + if (typeEnum == Me){ + subKey = subKey(user.getId(),MY); + }else { + subKey = String.valueOf(typeEnum.getPropertyId()); + } // redis获取对应类型的列表 map - Map helpHeroMap = RedisUtil.getInstence().getMapValues(RedisKey.HELP_FIGHT, subKey, String.class, HelpHero.class); + Map helpHeroMap = getHelpHeroMap(subKey); if (helpHeroMap == null || helpHeroMap.isEmpty()){ return result; } @@ -132,7 +138,10 @@ public class HelpHeroLogic { } // 已选中的助战 List strings = result.stream().map(v -> v.getHero().getId()).collect(Collectors.toList()); - HelpHero helpHero = getHelpHeroByType(user.getId(), typeEnum.getPropertyId()); + // 获取subkey,拿到英雄数据 + String subKey1 = HelpHeroLogic.subKey(user.getId(),HelpHeroLogic.CHOOSE); + HelpHero helpHero = getHelpHeroMap(subKey1).get(String.valueOf(typeEnum.getPropertyId())); + // 英雄不存在添加 if (helpHero != null && !strings.contains(helpHero.getHero().getId())){ result.add(CBean2Proto.helpHeroProto(helpHero)); } @@ -159,16 +168,15 @@ public class HelpHeroLogic { // 英雄类型 int propertyName = SCHero.getsCHero().get(hero.getTemplateId()).getPropertyName(); // 自己的助战列表 - HelpHero helpHero = RedisUtil.getInstence().getMapEntry(RedisKey.HELP_FIGHT, getSubKey(user.getId(),MY), String.valueOf(propertyName), HelpHero.class); + String subKey = subKey(user.getId(), MY); + HelpHero helpHero = getHelpHeroMap(subKey).get(String.valueOf(propertyName)); if (helpHero != null){ throw new ErrorCodeException(ErrorCode.SYS_ERROR_CODE,"已选择其他神将!"); } - // 自己的助战列表添加 - RedisUtil.getInstence().putMapEntry(RedisKey.HELP_FIGHT, getSubKey(user.getId(),MY), String.valueOf(propertyName), CBean2Proto.heroToHelpHero(user,hero)); - + addHelpHero(subKey(user.getId(),MY),propertyName,CBean2Proto.heroToHelpHero(user,hero)); // 类型助战列表添加,自己的助战列表没有该类型英雄,那么对应类型的助战列表也应该没有对应英雄,所以这里不再循环验证 - RedisUtil.getInstence().putMapEntry(RedisKey.HELP_FIGHT, String.valueOf(propertyName), String.valueOf(user.getId()), CBean2Proto.heroToHelpHero(user,hero)); + addHelpHero(String.valueOf(propertyName),user.getId(),CBean2Proto.heroToHelpHero(user,hero)); // 配置表,获取自己设置助战奖励 SCampTowerSetting campTowerSetting = STableManager.getConfig(SCampTowerSetting.class).get(1); @@ -189,7 +197,8 @@ public class HelpHeroLogic { */ public void useHelpHero(User user,String heroId,int type,int status) throws Exception{ // 该类型的助战使用状态 - HelpHero helpHero = getHelpHeroByType(user.getId(), type); + String subKey = subKey(user.getId(), CHOOSE); + HelpHero helpHero = getHelpHeroMap(subKey).get(String.valueOf(type)); // 选择助战 if (status == 1) { if (helpHero != null){ @@ -199,7 +208,7 @@ public class HelpHeroLogic { throw new ErrorCodeException(ErrorCode.SYS_ERROR_CODE,"已选择其他助战英雄!"); } // 获取全部助战列表 - Map helpMap = RedisUtil.getInstence().getMapValues(RedisKey.HELP_FIGHT, String.valueOf(type), String.class, HelpHero.class); + Map helpMap = getHelpHeroMap(String.valueOf(type)); for (Map.Entry data : helpMap.entrySet()) { // key int key = Integer.parseInt(data.getKey()); @@ -211,7 +220,7 @@ public class HelpHeroLogic { throw new ErrorCodeException(ErrorCode.SYS_ERROR_CODE, "不能使用自己的助战英雄!"); } // 添加助战英雄 - addHelpHero(user.getId(),type,data.getValue()); + addHelpHero(subKey,type,data.getValue()); break; } } @@ -221,7 +230,7 @@ public class HelpHeroLogic { throw new ErrorCodeException(ErrorCode.SYS_ERROR_CODE,"助战已使用或不存在,无法取消助战!"); } // 删除助战英雄魂宝 - removeHelpHero(user.getId(),type); + removeHelpHero(subKey(user.getId(),CHOOSE),type); } } @@ -266,30 +275,20 @@ public class HelpHeroLogic { user.getTeamPosManager().clearTeamPosByFourChallenge(user); // 领取奖励次数 user.getPlayerInfoManager().setHelpRewardNum(0); - } - - /*** - * 零点清理redis数据 - */ - public void zeroClearRedisHelp(){ - // 不是零点 - if (TimeUtils.getHourOfDay() != 0 || TimeUtils.getMiunte() != 0){ - return; - } - String key = GameApplication.serverId +":" + RedisKey.HELP_FIGHT + "*"; - // 模糊批量删除key - Set keys = RedisUtil.getInstence().getDimKey(key, -1); - keys.forEach(k->RedisUtil.getInstence().del(k)); + // 清理自己的助战信息 + String myKey = RedisUtil.getInstence().getKey(RedisKey.HELP_FIGHT, subKey(user.getId(), MY)); + RedisUtil.getInstence().del(myKey); + String chooseKey = RedisUtil.getInstence().getKey(RedisKey.HELP_FIGHT, subKey(user.getId(), CHOOSE)); + RedisUtil.getInstence().del(chooseKey); } /** - * 获取选择得助战列表 - * @param uid - * @return + * 根据类型删除助战英雄 + * @param subKey + * @param type */ - public static Map getHelpHeroMap(int uid){ - String subKey = getSubKey(uid,CHOOSE); - return RedisUtil.getInstence().getMapValues(RedisKey.HELP_FIGHT, subKey, String.class, HelpHero.class); + public static void removeHelpHero(String subKey, int type){ + RedisUtil.getInstence().removeMapEntrys(RedisKey.HELP_FIGHT, subKey, String.valueOf(type)); } /** @@ -298,8 +297,9 @@ public class HelpHeroLogic { * @param heroId * @return */ - public static HelpHero getHelpHero(int uid,String heroId){ - Map helpHeroMap = getHelpHeroMap(uid); + public static HelpHero getHelpHeroByHeroId(int uid,String heroId){ + String subKey = HelpHeroLogic.subKey(uid,HelpHeroLogic.CHOOSE); + Map helpHeroMap = getHelpHeroMap(subKey); for (HelpHero value : helpHeroMap.values()) { if (value.getHero().getId().equals(heroId)){ return value; @@ -309,37 +309,34 @@ public class HelpHeroLogic { } /** - * 根据类型获取助战英雄 - * @param uid - * @param type + * 获取选择得助战列表 + * @param subKey * @return */ - public static HelpHero getHelpHeroByType(int uid, int type){ - String subKey = getSubKey(uid,CHOOSE); - return RedisUtil.getInstence().getMapValue(RedisKey.HELP_FIGHT, subKey, String.valueOf(type), HelpHero.class); - } - - /** - * 根据类型删除助战英雄 - * @param uid - * @param type - */ - public static void removeHelpHero(int uid, int type){ - String subKey = getSubKey(uid,CHOOSE); - RedisUtil.getInstence().removeMapEntrys(RedisKey.HELP_FIGHT, subKey, String.valueOf(type)); + public static Map getHelpHeroMap(String subKey){ + return RedisUtil.getInstence().getMapValues(RedisKey.HELP_FIGHT, subKey, String.class, HelpHero.class); } /** * 添加助战英雄 - * @param uid + * @param subKey * @param type * @param helpHero */ - public static void addHelpHero(int uid,int type,HelpHero helpHero){ - String subKey = getSubKey(uid,CHOOSE); + public static void addHelpHero(String subKey, int type,HelpHero helpHero){ RedisUtil.getInstence().putMapEntry(RedisKey.HELP_FIGHT, subKey, String.valueOf(type), helpHero); } + /** + * 零点清理redis数据 + */ + public void zeroClearRedisHelp(){ + Set set = RedisUtil.getInstence().sGet(RedisKey.HELP_FIGHT_DELETE); + String[] keys = set.toArray(new String[0]); + RedisUtil.getInstence().del(keys); + RedisUtil.getInstence().setRemove(RedisKey.HELP_FIGHT_DELETE,keys); + } + /** * 删除好友时清理助战信息,暂时不用 * @param me diff --git a/serverlogic/src/main/java/com/ljsd/jieling/logic/hero/HeroLogic.java b/serverlogic/src/main/java/com/ljsd/jieling/logic/hero/HeroLogic.java index 920d6f8bb..119848b07 100644 --- a/serverlogic/src/main/java/com/ljsd/jieling/logic/hero/HeroLogic.java +++ b/serverlogic/src/main/java/com/ljsd/jieling/logic/hero/HeroLogic.java @@ -1120,7 +1120,7 @@ public class HeroLogic{ if(heroManager.getHeroMap().containsKey(heroId.getHeroId())){ set.add(heroManager.getHero(heroId.getHeroId()).getTemplateId()); } - HelpHero helpHero = HelpHeroLogic.getHelpHero(user.getId(), heroId.getHeroId()); + HelpHero helpHero = HelpHeroLogic.getHelpHeroByHeroId(user.getId(), heroId.getHeroId()); if (helpHero != null){ set.add(helpHero.getHero().getTemplateId()); } @@ -1222,7 +1222,7 @@ public class HeroLogic{ return "card not exists"; } }else if (FourChallengeLogic.isFourChallengeTeam(teamId)){ - if(user.getHeroManager().getHero(heroId) == null&&HelpHeroLogic.getHelpHero(user.getId(),heroId) == null){ + if(user.getHeroManager().getHero(heroId) == null&&HelpHeroLogic.getHelpHeroByHeroId(user.getId(),heroId) == null){ return "card not exists"; } }else { @@ -2367,7 +2367,7 @@ public class HeroLogic{ Map equipMap = new HashMap<>(); // 助战英雄 - HelpHero helpHero = HelpHeroLogic.getHelpHero(user.getId(), hero.getId()); + HelpHero helpHero = HelpHeroLogic.getHelpHeroByHeroId(user.getId(), hero.getId()); if (helpHero!=null){ for (Jewel jewel : helpHero.getJewels()) { equipMap.put(jewel.getId(),jewel); @@ -3063,7 +3063,7 @@ public class HeroLogic{ Hero hero = targetUser.getHeroManager().getHero(teamPosHeroInfo.getHeroId()); if (hero == null) { // 助战获取,随后最好加上编队验证 - hero = HelpHeroLogic.getHelpHero(targetUser.getId(),teamPosHeroInfo.getHeroId()).getHero(); + hero = HelpHeroLogic.getHelpHeroByHeroId(targetUser.getId(),teamPosHeroInfo.getHeroId()).getHero(); if (hero == null){ continue; } diff --git a/serverlogic/src/main/java/com/ljsd/jieling/util/CBean2Proto.java b/serverlogic/src/main/java/com/ljsd/jieling/util/CBean2Proto.java index c34c10a2a..3191bac1a 100644 --- a/serverlogic/src/main/java/com/ljsd/jieling/util/CBean2Proto.java +++ b/serverlogic/src/main/java/com/ljsd/jieling/util/CBean2Proto.java @@ -228,7 +228,8 @@ public class CBean2Proto { public static List getHelpFightList(User user) throws Exception { List lists = new ArrayList<>(); // 助战英雄列表 - Map helpHeroMap = HelpHeroLogic.getHelpHeroMap(user.getId()); + String subKey = HelpHeroLogic.subKey(user.getId(),HelpHeroLogic.CHOOSE); + Map helpHeroMap = HelpHeroLogic.getHelpHeroMap(subKey); if (helpHeroMap == null || helpHeroMap.isEmpty()){ return lists; } diff --git a/worldserver/src/main/java/com/WorldServerApplication.java b/worldserver/src/main/java/com/WorldServerApplication.java index fb8d38ee7..8866394c0 100644 --- a/worldserver/src/main/java/com/WorldServerApplication.java +++ b/worldserver/src/main/java/com/WorldServerApplication.java @@ -69,11 +69,11 @@ public class WorldServerApplication { // NetManager.getInstance().init(); - try { - ArenaLogic.initRank(); - }catch (Exception e){ - LOGGER.info("ArenaLogic err ->{}", e); - } +// try { +// ArenaLogic.initRank(); +// }catch (Exception e){ +// LOGGER.info("ArenaLogic err ->{}", e); +// } bean = ConfigurableApplicationContextManager.getBean(WorldCoreSettings.class); diff --git a/worldserver/src/main/java/com/world/handler/AddDeathPathRankRequestHandler.java b/worldserver/src/main/java/com/world/handler/AddDeathPathRankRequestHandler.java index 1783b5b49..8dbcd6e89 100644 --- a/worldserver/src/main/java/com/world/handler/AddDeathPathRankRequestHandler.java +++ b/worldserver/src/main/java/com/world/handler/AddDeathPathRankRequestHandler.java @@ -1,69 +1,69 @@ -package com.world.handler; - -import com.google.protobuf.GeneratedMessage; -import com.world.logic.rank.AbstractCrossRank; -import com.world.logic.rank.DeathPathCrossEveryPersonRank; -import com.world.redis.RedisKey; -import com.world.redis.RedisUtil; -import org.luaj.vm2.ast.Str; -import org.springframework.data.redis.core.ZSetOperations; -import rpc.net.RpcMessage; -import rpc.net.gtGBaseHandler; -import rpc.world.WorldProto; - -import java.math.BigDecimal; -import java.util.Set; - -/** - * @author lvxinran - * @date 2020/12/3 - * @discribe - */ -@Deprecated -public class AddDeathPathRankRequestHandler extends gtGBaseHandler{ - - @Override - public GeneratedMessage processWithProto(RpcMessage rpcMessage, WorldProto.AddDeathPathRankRequest proto) throws Exception { - RedisUtil redisUtil = RedisUtil.getInstence(); - //操作每个关的公会、个人数据 - BigDecimal decimal = new BigDecimal(String.valueOf(proto.getDamage())).divide(new BigDecimal(String.valueOf(Math.pow(10,15)))); - long everyGuildChangeScore = (long)proto.getDamage()*1000; - if(redisUtil.getZSetScore(proto.getGroupId()+":"+RedisKey.DEATH_PATH_EVERY_PERSON_RANK,String.valueOf(proto.getPathId()), String.valueOf(proto.getUid()))==-1){ -// decimal = decimal.add(new BigDecimal(0)); - //记录人数操作 - everyGuildChangeScore = everyGuildChangeScore+1; - } - - String before= firstGuild(redisUtil, proto.getGroupId(), proto.getPathId()); - redisUtil.incrementZsetScore(proto.getGroupId()+":"+RedisKey.DEATH_PATH_EVERY_GUILD_RANK,String.valueOf(proto.getPathId()),String.valueOf(proto.getGuildId()), everyGuildChangeScore); - String after = firstGuild(redisUtil, proto.getGroupId(), proto.getPathId()); - if(!before.equals(after)){ - decimal = decimal.add(new BigDecimal(1)); - if(!"".equals(before)){ - redisUtil.incrementZsetScore(proto.getGroupId()+":"+RedisKey.DEATH_PATH_TOTAL_GUILD_RANK,"",before, -1); - } - } - - - redisUtil.incrementZsetScore(proto.getGroupId()+":"+RedisKey.DEATH_PATH_EVERY_PERSON_RANK,String.valueOf(proto.getPathId()),String.valueOf(proto.getUid()), proto.getDamage()); - +//package com.world.handler; +// +//import com.google.protobuf.GeneratedMessage; +//import com.world.logic.rank.AbstractCrossRank; +//import com.world.logic.rank.DeathPathCrossEveryPersonRank; +//import com.world.redis.RedisKey; +//import com.world.redis.RedisUtil; +//import org.luaj.vm2.ast.Str; +//import org.springframework.data.redis.core.ZSetOperations; +//import rpc.net.RpcMessage; +//import rpc.net.gtGBaseHandler; +//import rpc.world.WorldProto; +// +//import java.math.BigDecimal; +//import java.util.Set; +// +///** +// * @author lvxinran +// * @date 2020/12/3 +// * @discribe +// */ +//@Deprecated +//public class AddDeathPathRankRequestHandler extends gtGBaseHandler{ +// +// @Override +// public GeneratedMessage processWithProto(RpcMessage rpcMessage, WorldProto.AddDeathPathRankRequest proto) throws Exception { +// RedisUtil redisUtil = RedisUtil.getInstence(); +// //操作每个关的公会、个人数据 +// BigDecimal decimal = new BigDecimal(String.valueOf(proto.getDamage())).divide(new BigDecimal(String.valueOf(Math.pow(10,15)))); +// long everyGuildChangeScore = (long)proto.getDamage()*1000; +// if(redisUtil.getZSetScore(proto.getGroupId()+":"+RedisKey.DEATH_PATH_EVERY_PERSON_RANK,String.valueOf(proto.getPathId()), String.valueOf(proto.getUid()))==-1){ +//// decimal = decimal.add(new BigDecimal(0)); +// //记录人数操作 +// everyGuildChangeScore = everyGuildChangeScore+1; +// } +// +// String before= firstGuild(redisUtil, proto.getGroupId(), proto.getPathId()); +// redisUtil.incrementZsetScore(proto.getGroupId()+":"+RedisKey.DEATH_PATH_EVERY_GUILD_RANK,String.valueOf(proto.getPathId()),String.valueOf(proto.getGuildId()), everyGuildChangeScore); +// String after = firstGuild(redisUtil, proto.getGroupId(), proto.getPathId()); +// if(!before.equals(after)){ +// decimal = decimal.add(new BigDecimal(1)); +// if(!"".equals(before)){ +// redisUtil.incrementZsetScore(proto.getGroupId()+":"+RedisKey.DEATH_PATH_TOTAL_GUILD_RANK,"",before, -1); +// } +// } +// +// // redisUtil.incrementZsetScore(proto.getGroupId()+":"+RedisKey.DEATH_PATH_EVERY_PERSON_RANK,String.valueOf(proto.getPathId()),String.valueOf(proto.getUid()), proto.getDamage()); - - //操作总的个人伤害 - redisUtil.incrementZsetScore(proto.getGroupId()+":"+RedisKey.DEATH_PATH_TOTAL_PERSON_RANK,"",String.valueOf(proto.getUid()), proto.getDamage()); - - //操作总的工会伤害 - - redisUtil.incrementZsetScore(proto.getGroupId()+":"+RedisKey.DEATH_PATH_TOTAL_GUILD_RANK,"",String.valueOf(proto.getGuildId()), decimal.doubleValue()); - - redisUtil.hset(RedisKey.DEATH_PATH_EVERY_OVER_HASH,String.valueOf(rpcMessage.getServerId()),1); - - return proto; - } - - private String firstGuild(RedisUtil redisUtil,int groupId,int pathId){ - Set> scores = redisUtil.getZsetreverseRangeWithScores(groupId + ":" + RedisKey.DEATH_PATH_EVERY_GUILD_RANK, String.valueOf(pathId), 0, 0); - return scores.iterator().hasNext()?scores.iterator().next().getValue():""; - } - -} +// +//// redisUtil.incrementZsetScore(proto.getGroupId()+":"+RedisKey.DEATH_PATH_EVERY_PERSON_RANK,String.valueOf(proto.getPathId()),String.valueOf(proto.getUid()), proto.getDamage()); +// +// //操作总的个人伤害 +// redisUtil.incrementZsetScore(proto.getGroupId()+":"+RedisKey.DEATH_PATH_TOTAL_PERSON_RANK,"",String.valueOf(proto.getUid()), proto.getDamage()); +// +// //操作总的工会伤害 +// +// redisUtil.incrementZsetScore(proto.getGroupId()+":"+RedisKey.DEATH_PATH_TOTAL_GUILD_RANK,"",String.valueOf(proto.getGuildId()), decimal.doubleValue()); +// +// redisUtil.hset(RedisKey.DEATH_PATH_EVERY_OVER_HASH,String.valueOf(rpcMessage.getServerId()),1); +// +// return proto; +// } +// +// private String firstGuild(RedisUtil redisUtil,int groupId,int pathId){ +// Set> scores = redisUtil.getZsetreverseRangeWithScores(groupId + ":" + RedisKey.DEATH_PATH_EVERY_GUILD_RANK, String.valueOf(pathId), 0, 0); +// return scores.iterator().hasNext()?scores.iterator().next().getValue():""; +// } +// +//} diff --git a/worldserver/src/main/java/com/world/handler/GetDeathPathFirstRequestHandler.java b/worldserver/src/main/java/com/world/handler/GetDeathPathFirstRequestHandler.java index 213ab5973..d36d26867 100644 --- a/worldserver/src/main/java/com/world/handler/GetDeathPathFirstRequestHandler.java +++ b/worldserver/src/main/java/com/world/handler/GetDeathPathFirstRequestHandler.java @@ -1,45 +1,45 @@ -package com.world.handler; - -import com.ljsd.jieling.logic.dao.GuildInfoCache; -import com.world.db.mongo.MongoUtil; -import com.world.logic.rank.DeathPathCrossEveryGuildRank; -import com.world.redis.RedisKey; -import com.world.redis.RedisUtil; -import org.springframework.data.redis.core.ZSetOperations; -import rpc.net.RpcMessage; -import rpc.net.gtGBaseHandler; -import rpc.world.WorldProto; - -import java.util.Iterator; -import java.util.Set; - -/** - * @author lvxinran - * @date 2020/12/12 - * @discribe - */ -@Deprecated -public class GetDeathPathFirstRequestHandler extends gtGBaseHandler { - - @Override - public WorldProto.GetDeathPathFirstResponse processWithProto(RpcMessage rpcMessage, WorldProto.GetDeathPathFirstRequest proto) throws Exception { - - - DeathPathCrossEveryGuildRank deathPathCrossEveryGuildRank = new DeathPathCrossEveryGuildRank(RedisKey.DEATH_PATH_EVERY_GUILD_RANK); - WorldProto.GetDeathPathFirstResponse.Builder response = WorldProto.GetDeathPathFirstResponse.newBuilder(); - /*for(int i = 1 ;i<=10;i++){ - Set> zSetReverseRangeWithScores = RedisUtil.getInstence().getZsetreverseRangeWithScores(proto.getGroupId()+":"+ RedisKey.DEATH_PATH_EVERY_GUILD_RANK, String.valueOf(i), 0, 0); - Iterator> iterator = zSetReverseRangeWithScores.iterator(); - if(!iterator.hasNext()){ - continue; - } - ZSetOperations.TypedTuple next = iterator.next(); - int guildId = Integer.parseInt(next.getValue()); - GuildInfoCache crossGuild = deathPathCrossEveryGuildRank.getCrossGuild(guildId); - String name = MongoUtil.getInstence().getServerNameById(crossGuild.getServerId()); - WorldProto.EvertDeathPath info = WorldProto.EvertDeathPath.newBuilder().setGuildName(crossGuild.getGuildName()).setPathId(i).setGid(guildId).setServerName(name).build(); - response.addDeathPathInfo(info); - }*/ - return response.build(); - } -} +//package com.world.handler; +// +//import com.ljsd.jieling.logic.dao.GuildInfoCache; +//import com.world.db.mongo.MongoUtil; +//import com.world.logic.rank.DeathPathCrossEveryGuildRank; +//import com.world.redis.RedisKey; +//import com.world.redis.RedisUtil; +//import org.springframework.data.redis.core.ZSetOperations; +//import rpc.net.RpcMessage; +//import rpc.net.gtGBaseHandler; +//import rpc.world.WorldProto; +// +//import java.util.Iterator; +//import java.util.Set; +// +///** +// * @author lvxinran +// * @date 2020/12/12 +// * @discribe +// */ +//@Deprecated +//public class GetDeathPathFirstRequestHandler extends gtGBaseHandler { +// +// @Override +// public WorldProto.GetDeathPathFirstResponse processWithProto(RpcMessage rpcMessage, WorldProto.GetDeathPathFirstRequest proto) throws Exception { +// +// +// DeathPathCrossEveryGuildRank deathPathCrossEveryGuildRank = new DeathPathCrossEveryGuildRank(RedisKey.DEATH_PATH_EVERY_GUILD_RANK); +// WorldProto.GetDeathPathFirstResponse.Builder response = WorldProto.GetDeathPathFirstResponse.newBuilder(); +// /*for(int i = 1 ;i<=10;i++){ +// Set> zSetReverseRangeWithScores = RedisUtil.getInstence().getZsetreverseRangeWithScores(proto.getGroupId()+":"+ RedisKey.DEATH_PATH_EVERY_GUILD_RANK, String.valueOf(i), 0, 0); +// Iterator> iterator = zSetReverseRangeWithScores.iterator(); +// if(!iterator.hasNext()){ +// continue; +// } +// ZSetOperations.TypedTuple next = iterator.next(); +// int guildId = Integer.parseInt(next.getValue()); +// GuildInfoCache crossGuild = deathPathCrossEveryGuildRank.getCrossGuild(guildId); +// String name = MongoUtil.getInstence().getServerNameById(crossGuild.getServerId()); +// WorldProto.EvertDeathPath info = WorldProto.EvertDeathPath.newBuilder().setGuildName(crossGuild.getGuildName()).setPathId(i).setGid(guildId).setServerName(name).build(); +// response.addDeathPathInfo(info); +// }*/ +// return response.build(); +// } +//} diff --git a/worldserver/src/main/java/com/world/handler/GetRankRequestHandler.java b/worldserver/src/main/java/com/world/handler/GetRankRequestHandler.java index fa8d35d27..d6cf1aa41 100644 --- a/worldserver/src/main/java/com/world/handler/GetRankRequestHandler.java +++ b/worldserver/src/main/java/com/world/handler/GetRankRequestHandler.java @@ -1,38 +1,38 @@ -package com.world.handler; - -import com.world.logic.arean.ArenaLogic; -import com.world.logic.rank.DeathPathCrossEveryGuildRank; -import com.world.logic.rank.DeathPathCrossEveryPersonRank; -import com.world.logic.rank.DeathPathCrossTotalGuildRank; -import com.world.redis.RedisKey; -import rpc.net.RpcMessage; -import rpc.net.gtGBaseHandler; -import rpc.protocols.PlayerInfoProto; -import rpc.world.WorldProto; - -/** - * @author lvxinran - * @date 2020/12/3 - * @discribe - */ -public class GetRankRequestHandler extends gtGBaseHandler { - - @Override - public PlayerInfoProto.RankResponse processWithProto(RpcMessage rpcMessage, WorldProto.GetRankRequest proto) throws Exception { - switch (proto.getType()){ +//package com.world.handler; +// +//import com.world.logic.arean.ArenaLogic; +//import com.world.logic.rank.DeathPathCrossEveryGuildRank; +//import com.world.logic.rank.DeathPathCrossEveryPersonRank; +//import com.world.logic.rank.DeathPathCrossTotalGuildRank; +//import com.world.redis.RedisKey; +//import rpc.net.RpcMessage; +//import rpc.net.gtGBaseHandler; +//import rpc.protocols.PlayerInfoProto; +//import rpc.world.WorldProto; +// +///** +// * @author lvxinran +// * @date 2020/12/3 +// * @discribe +// */ +//public class GetRankRequestHandler extends gtGBaseHandler { +// +// @Override +// public PlayerInfoProto.RankResponse processWithProto(RpcMessage rpcMessage, WorldProto.GetRankRequest proto) throws Exception { +// switch (proto.getType()){ // RankEnum.DEATH_PATH_TOTAL_PERSON_RANK.getType(); - case 15: - return new DeathPathCrossEveryPersonRank(proto.getCrossId()+":"+RedisKey.DEATH_PATH_EVERY_PERSON_RANK).getRank(proto.getUid(),String.valueOf(proto.getAcitvityId()),1,-1); - case 16: - return new DeathPathCrossEveryGuildRank(proto.getCrossId()+":"+RedisKey.DEATH_PATH_EVERY_GUILD_RANK).getRank(proto.getUid(),String.valueOf(proto.getAcitvityId()),1,-1); - case 17: - return new DeathPathCrossEveryPersonRank(proto.getCrossId()+":"+RedisKey.DEATH_PATH_TOTAL_PERSON_RANK).getRank(proto.getUid(),"",1,-1); - case 18: - return new DeathPathCrossTotalGuildRank(proto.getCrossId()+":"+RedisKey.DEATH_PATH_TOTAL_GUILD_RANK).getRank(proto.getUid(),"",1,-1); - case 28: - return ArenaLogic.getInstance().getRank(rpcMessage.getUid(),rpcMessage.getServerId()); - default: - return null; - } - } -} +// case 15: +// return new DeathPathCrossEveryPersonRank(proto.getCrossId()+":"+RedisKey.DEATH_PATH_EVERY_PERSON_RANK).getRank(proto.getUid(),String.valueOf(proto.getAcitvityId()),1,-1); +// case 16: +// return new DeathPathCrossEveryGuildRank(proto.getCrossId()+":"+RedisKey.DEATH_PATH_EVERY_GUILD_RANK).getRank(proto.getUid(),String.valueOf(proto.getAcitvityId()),1,-1); +// case 17: +// return new DeathPathCrossEveryPersonRank(proto.getCrossId()+":"+RedisKey.DEATH_PATH_TOTAL_PERSON_RANK).getRank(proto.getUid(),"",1,-1); +// case 18: +// return new DeathPathCrossTotalGuildRank(proto.getCrossId()+":"+RedisKey.DEATH_PATH_TOTAL_GUILD_RANK).getRank(proto.getUid(),"",1,-1); +// case 28: +// return ArenaLogic.getInstance().getRank(rpcMessage.getUid(),rpcMessage.getServerId()); +// default: +// return null; +// } +// } +//} diff --git a/worldserver/src/main/java/com/world/logic/arean/ArenaLogic.java b/worldserver/src/main/java/com/world/logic/arean/ArenaLogic.java index b0483cbef..6abc08630 100644 --- a/worldserver/src/main/java/com/world/logic/arean/ArenaLogic.java +++ b/worldserver/src/main/java/com/world/logic/arean/ArenaLogic.java @@ -68,27 +68,27 @@ public class ArenaLogic { public static void initRank() throws Exception { - Map serverSplit = RedisUtil.getInstence().getMapValues(RedisKey.SERVER_SPLIT_INFO, String.class, Integer.class); - serverSplit.forEach((s, integer) -> { - HashSet strings = map.getOrDefault(integer, new HashSet<>()); - strings.add(s); - map.put(integer, strings); - }); - ArenaLogic.serverSplit = serverSplit; - - Set keys = new HashSet<>(); - Cursor cursor = RedisUtil.getInstence().scan("WORLD_ARENA_RANK:*", 200); - while (cursor.hasNext()) { - //找到一次就添加一次 - keys.add(cursor.next()); - } - cursor.close(); - keys.forEach(k -> { - HashMap object = RedisUtil.getInstence().getObject(k, new TypeToken>() { - }.getType()); - - ranInfo.put(Integer.valueOf(k.split(":")[1]), object); - }); +// Map serverSplit = RedisUtil.getInstence().getMapValues(RedisKey.SERVER_SPLIT_INFO, String.class, Integer.class); +// serverSplit.forEach((s, integer) -> { +// HashSet strings = map.getOrDefault(integer, new HashSet<>()); +// strings.add(s); +// map.put(integer, strings); +// }); +// ArenaLogic.serverSplit = serverSplit; +// +// Set keys = new HashSet<>(); +// Cursor cursor = RedisUtil.getInstence().scan("WORLD_ARENA_RANK:*", 200); +// while (cursor.hasNext()) { +// //找到一次就添加一次 +// keys.add(cursor.next()); +// } +// cursor.close(); +// keys.forEach(k -> { +// HashMap object = RedisUtil.getInstence().getObject(k, new TypeToken>() { +// }.getType()); +// +// ranInfo.put(Integer.valueOf(k.split(":")[1]), object); +// }); } public void minCheck() { @@ -139,27 +139,27 @@ public class ArenaLogic { } private void onfinish(){ - map.clear(); - - List delKeys = new ArrayList<>(); - - delKeys.add(RedisUtil.getInstence().getKey(RedisKey.WORLD_ARENA_RANK_MY_MATH, "")); - delKeys.add(RedisUtil.getInstence().getKey(RedisKey.WORLD_ARENA_RRECORD, "")); - delKeys.add(RedisUtil.getInstence().getKey(RedisKey.WORLD_ARENA_MY_PRON, "")); - - Cursor cursor = RedisUtil.getInstence().scan("WORLD_ARENA_RANK_MY_MATH:*", -1); - while (cursor.hasNext()) { - String next = cursor.next(); - System.out.println("清除:"+next); - delKeys.add(next); - } - Cursor curso1 = RedisUtil.getInstence().scan("WORLD_ARENA_RRECORD:*", -1); - while (curso1.hasNext()) { - String next = curso1.next(); - System.out.println("清除:"+next); - delKeys.add(next); - } - RedisUtil.getInstence().del(delKeys.toArray(new String[0])); +// map.clear(); +// +// List delKeys = new ArrayList<>(); +// +// delKeys.add(RedisUtil.getInstence().getKey(RedisKey.WORLD_ARENA_RANK_MY_MATH, "")); +// delKeys.add(RedisUtil.getInstence().getKey(RedisKey.WORLD_ARENA_RRECORD, "")); +// delKeys.add(RedisUtil.getInstence().getKey(RedisKey.WORLD_ARENA_MY_PRON, "")); +// +// Cursor cursor = RedisUtil.getInstence().scan("WORLD_ARENA_RANK_MY_MATH:*", -1); +// while (cursor.hasNext()) { +// String next = cursor.next(); +// System.out.println("清除:"+next); +// delKeys.add(next); +// } +// Cursor curso1 = RedisUtil.getInstence().scan("WORLD_ARENA_RRECORD:*", -1); +// while (curso1.hasNext()) { +// String next = curso1.next(); +// System.out.println("清除:"+next); +// delKeys.add(next); +// } +// RedisUtil.getInstence().del(delKeys.toArray(new String[0])); } @@ -357,12 +357,12 @@ public class ArenaLogic { * 零点重置 */ public static void clearReidsInTime() { - List delKeys = new ArrayList<>(); - Cursor cursor = RedisUtil.getInstence().scan("WORLD_ARENA_MY_PRON:*", -1); - while (cursor.hasNext()) { - delKeys.add(cursor.next()); - } - RedisUtil.getInstence().del(delKeys.toArray(new String[0])); +// List delKeys = new ArrayList<>(); +// Cursor cursor = RedisUtil.getInstence().scan("WORLD_ARENA_MY_PRON:*", -1); +// while (cursor.hasNext()) { +// delKeys.add(cursor.next()); +// } +// RedisUtil.getInstence().del(delKeys.toArray(new String[0])); } diff --git a/worldserver/src/main/java/com/world/redis/RedisKey.java b/worldserver/src/main/java/com/world/redis/RedisKey.java index 1c6f19ac9..f4a05147e 100644 --- a/worldserver/src/main/java/com/world/redis/RedisKey.java +++ b/worldserver/src/main/java/com/world/redis/RedisKey.java @@ -27,13 +27,13 @@ public class RedisKey { public static final String WORLD_ARENA_RRECORD = "WORLD_ARENA_RRECORD";//竞技场记录 - public static final String DEATH_PATH_EVERY_PERSON_RANK = "DEATH_PATH_EVERY_PERSON_RANK"; - - public static final String DEATH_PATH_TOTAL_PERSON_RANK = "DEATH_PATH_TOTAL_PERSON_RANK"; - - public static final String DEATH_PATH_TOTAL_GUILD_RANK = "DEATH_PATH_TOTAL_GUILD_RANK"; - - public static final String DEATH_PATH_EVERY_GUILD_RANK = "DEATH_PATH_EVERY_GUILD_RANK"; +// public static final String DEATH_PATH_EVERY_PERSON_RANK = "DEATH_PATH_EVERY_PERSON_RANK"; +// +// public static final String DEATH_PATH_TOTAL_PERSON_RANK = "DEATH_PATH_TOTAL_PERSON_RANK"; +// +// public static final String DEATH_PATH_TOTAL_GUILD_RANK = "DEATH_PATH_TOTAL_GUILD_RANK"; +// +// public static final String DEATH_PATH_EVERY_GUILD_RANK = "DEATH_PATH_EVERY_GUILD_RANK"; public static final String PLAYER_INFO_CACHE = "PLAYER_INFO_CACHE"; @@ -41,7 +41,7 @@ public class RedisKey { public static final String WORLD_ARENA_MY_PRON = "WORLD_ARENA_MY_PRON";//我的膜拜记录 - public static final String DEATH_PATH_EVERY_OVER_HASH = "DEATH_PATH_EVERY_OVER_HASH";//结算标识 +// public static final String DEATH_PATH_EVERY_OVER_HASH = "DEATH_PATH_EVERY_OVER_HASH";//结算标识 public static final String ARENA_PROUD_COUNT = "ARENA_PROUD_COUNT"; public static final String ARENA_PROUD_COUNT_ROBOT = "ARENA_PROUD_COUNT_ROBOT"; diff --git a/worldserver/src/main/java/com/world/redis/RedisUtil.java b/worldserver/src/main/java/com/world/redis/RedisUtil.java index 74fc3b403..ebbba3550 100644 --- a/worldserver/src/main/java/com/world/redis/RedisUtil.java +++ b/worldserver/src/main/java/com/world/redis/RedisUtil.java @@ -1,794 +1,794 @@ -package com.world.redis; - - -import com.google.gson.Gson; -import com.ljsd.common.mogodb.util.GlobalData; -import com.world.config.ConfigurableApplicationContextManager; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.data.redis.RedisConnectionFailureException; -import org.springframework.data.redis.connection.RedisConnection; -import org.springframework.data.redis.connection.ReturnType; -import org.springframework.data.redis.core.*; -import org.springframework.data.redis.serializer.RedisSerializer; -import org.springframework.util.CollectionUtils; -import redis.clients.jedis.Protocol; -import util.TimeUtils; - -import java.util.*; -import java.util.concurrent.TimeUnit; - -/** - * Author: zsx - * CreateDate: 2020/11/18 17:30 - */ -public class RedisUtil { - - private static final Logger LOGGER = LoggerFactory.getLogger(RedisUtil.class); - - - private static Gson gson = new Gson(); - private static int MAX_TRY_TIMES = 3; //最大尝试次数,保障获取/存储成功 - private static int FAILED_SLEEP = 2; //每次失败最大停顿时间 - - private RedisUtil() { - } - - public static RedisUtil getInstence() { - return InnerClass.instance; - } - - - private static class InnerClass { - private static final RedisUtil instance = new RedisUtil(); - } - - private StringRedisTemplate redisTemplate; - - - public void init(){ - redisTemplate = ConfigurableApplicationContextManager.getBean(StringRedisTemplate.class); - } - - /** - * 删除缓存 - * - * @param key - * 可以传一个值 或多个 - */ - @SuppressWarnings("unchecked") - public void del(String... key) { - if (key != null && key.length > 0) { - if (key.length == 1) { - redisTemplate.delete(key[0]); - } else { - redisTemplate.delete(CollectionUtils.arrayToList(key)); - } - } - } - - // ============================String============================= - /** - * 普通缓存获取 - * - * @param key - * 键 - * @return 值 - */ - public Object get(String key) { - return key == null ? null : redisTemplate.opsForValue().get(key); - } - - public T getObject(String key,java.lang.reflect.Type type){ - String source = redisTemplate.opsForValue().get(key); - if(source == null){ - return null; - } - return gson.fromJson(source,type); - } - - public T getObject(String key,Class clazz){ - String source = redisTemplate.opsForValue().get(key); - if(source == null){ - return null; - } - return gson.fromJson(source, clazz); - } - /** - * 获取Object - * - * @param key - * @return - */ - public T getObject(String type, String key, Class clazz, int expireTime) { - try { - String valueStr = redisTemplate.opsForValue().get(type + RedisKey.Delimiter_colon + key); - if (valueStr == null) { - return null; - } - if (expireTime > 0) { - redisTemplate.expire(type + key, expireTime, TimeUnit.SECONDS); - } - return gson.fromJson(valueStr, clazz); - } catch (RedisConnectionFailureException e) { - LOGGER.error("------------------Redis 连接失败------------------msg={}", e); - } - return null; - } - - - - public Long increment(String key) { - for (int i = 0; i < MAX_TRY_TIMES; i++) { - try { - return redisTemplate.opsForValue().increment(key, 1); - } catch (Exception e) { - sleep(FAILED_SLEEP); - } - } - return -1L; - } - - public boolean set(String key, String value) { - try { - redisTemplate.opsForValue().set(key, value); - return true; - } catch (Exception e) { - e.printStackTrace(); - return false; - } - - } - - public void set(String type, String rkey, T t) { - String key = getKey(type, rkey); - redisTemplate.opsForValue().set(key, gson.toJson(t)); - } - public void set(String mtype, String rkey, T t,java.lang.reflect.Type type) { - String key = getKey(mtype, rkey); - redisTemplate.opsForValue().set(key, gson.toJson(t,type)); - } - // ===============================set================================= - - public void saddAll(String key, String[] members){ - try { - redisTemplate.opsForSet().add(key,members); - } catch (Exception e) { - sleep(FAILED_SLEEP); - } - } - - public List getAllSetToList(String key, Class clazz){ - for(int i=0;i members = redisTemplate.opsForSet().members(key); - if(members!=null && !members.isEmpty()){ - List result = new ArrayList<>(members.size()); - for(String item : members){ - result.add(gson.fromJson(item,clazz)); - } - return result; - } - } catch (Exception e) { - sleep(FAILED_SLEEP); - } - } - return null; - - } - - public boolean sadd(String key,String member){ - try { - redisTemplate.opsForSet().add(key,member); - return true; - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } - - - - - private void sleep(long sleepMs){ - try { - Thread.sleep(sleepMs); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - - - // ================================Map================================= - /** - * HashGet - * - * @param key - * 键 不能为null - * @param mapKey - * 项 不能为null - * @return 值 - */ - public Object hget(String key, String mapKey) { - return redisTemplate.opsForHash().get(key, mapKey); - } - - /** - * 获取hashKey对应的所有键值 - * - * @param key - * 键 - * @return 对应的多个键值 - */ - public Map hmget(String key) { - return redisTemplate.opsForHash().entries(key); - } - - /** - * HashSet - * - * @param key - * 键 - * @param map - * 对应多个键值 - * @return true 成功 false 失败 - */ - public boolean hmset(String key, Map map) { - try { - redisTemplate.opsForHash().putAll(key, map); - return true; - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } - - /** - * HashSet 并设置时间 - * - * @param key - * 键 - * @param map - * 对应多个键值 - * @param time - * 时间(秒) - * @return true成功 false失败 - */ - public boolean hmset(String key, Map map, long time) { - try { - redisTemplate.opsForHash().putAll(key, map); - if (time > 0) { - expire(key, time); - } - return true; - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } - - /** - * 向一张hash表中放入数据,如果不存在将创建 - * - * @param key - * 键 - * @param mapKey - * 项 - * @param value - * 值 - * @return true 成功 false失败 - */ - public boolean hset(String key, String mapKey, Object value) { - try { - if (!(value instanceof String)){ - value = gson.toJson(value); - } - redisTemplate.opsForHash().put(key, mapKey, value); - return true; - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } - - /** - * 向一张hash表中放入数据,如果不存在将创建 - * - * @param key - * 键 - * @param mapKey - * 项 - * @param value - * 值 - * @param time - * 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间 - * @return true 成功 false失败 - */ - public boolean hset(String key, String mapKey, Object value, long time) { - try { - if (!(value instanceof String)){ - value = gson.toJson(value); - } - redisTemplate.opsForHash().put(key, mapKey, value); - if (time > 0) { - expire(key, time); - } - return true; - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } - - /** - * 删除hash表中的值 - * - * @param key - * 键 不能为null - * @param item - * 项 可以使多个 不能为null - */ - public void hdel(String key, Object... item) { - redisTemplate.opsForHash().delete(key, item); - } - - /** - * 判断hash表中是否有该项的值 - * - * @param key - * 键 不能为null - * @param item - * 项 不能为null - * @return true 存在 false不存在 - */ - public boolean hHasKey(String key, String item) { - return redisTemplate.opsForHash().hasKey(key, item); - } - - /** - * hash递增 如果不存在,就会创建一个 并把新增后的值返回 - * - * @param key - * 键 - * @param item - * 项 - * @param by - * 要增加几(大于0) - * @return - */ - public double hincr(String key, String item, double by) { - return redisTemplate.opsForHash().increment(key, item, by); - } - - /** - * hash递减 - * - * @param key - * 键 - * @param item - * 项 - * @param by - * 要减少记(小于0) - * @return - */ - public double hdecr(String key, String item, double by) { - return redisTemplate.opsForHash().increment(key, item, -by); - } - /** - * 指定缓存失效时间 - * - * @param key - * 键 - * @param time - * 时间(秒) - * @return - */ - public boolean expire(String key, long time) { - try { - if (time > 0) { - redisTemplate.expire(key, time, TimeUnit.SECONDS); - } - return true; - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } - public Map getMapValues(String type,String key,Class keyClazz,Class valueClazz){ - Map result = new HashMap<>(); - String rkey = getKey(type, key); - Map entries = redisTemplate.opsForHash().entries(rkey); - for(Map.Entry item : entries.entrySet()){ - Object key1 = item.getKey(); - Object value = item.getValue(); - result.put(gson.fromJson(gson.toJson(key1),keyClazz),gson.fromJson(value.toString(),valueClazz)); - } - return result; - } - public Map getMapValues(String type,Class keyClazz,Class valueClazz){ - Map result = new HashMap<>(); - Map entries = redisTemplate.opsForHash().entries(type); - for(Map.Entry item : entries.entrySet()){ - Object key1 = item.getKey(); - Object value = item.getValue(); - result.put(gson.fromJson(gson.toJson(key1),keyClazz),gson.fromJson(value.toString(),valueClazz)); - } - return result; - } - public String getKey(String type,String key){ - - return type + RedisKey.Delimiter_colon + key; - } - public void removeMapEntrys(String type, String key, Object... mapKeys){ - String rkey = getKey(type, key); - for (int i = 0; i < MAX_TRY_TIMES; i++) { - try { - redisTemplate.opsForHash().delete(rkey,mapKeys); - return; - } catch (Exception e) { - TimeUtils.sleep(FAILED_SLEEP); - } - } - } - - public void putMapEntry(String type,String key,String mapKey, T value){ - String rkey = getKey(type, key); - for (int i = 0; i < MAX_TRY_TIMES; i++) { - try { - redisTemplate.opsForHash().put(rkey,mapKey,gson.toJson(value)); - return; - } catch (Exception e) { - TimeUtils.sleep(FAILED_SLEEP); - } - } - } - //操作sortset - public void zsetAddAall(String key, Set> items){ - for (int i = 0; i < MAX_TRY_TIMES; i++) { - try { - redisTemplate.opsForZSet().add(key,items); - return; - } catch (Exception e) { - TimeUtils.sleep(FAILED_SLEEP); - } - } - } - - //操作sortset - public void zsetAddOne(String key, String uid, double useTime){ - for (int i = 0; i < MAX_TRY_TIMES; i++) { - try { - redisTemplate.opsForZSet().add(key, uid, useTime); - return; - } catch (Exception e) { - TimeUtils.sleep(FAILED_SLEEP); - } - } - } - - //操作sortset score<<32+time - public void zsetAddOneOffsetByTime(String key, String value, int score){ - for (int i = 0; i < MAX_TRY_TIMES; i++) { - try { - long s= (long)score<<40; - long sort = s+(0xFFFFFFFFFFL-(int)(System.currentTimeMillis()/1000)); - redisTemplate.opsForZSet().add(key, value, sort); - return; - } catch (Exception e) { - TimeUtils.sleep(FAILED_SLEEP); - } - } - } - - public void zsetRemoveOne(String key, String value){ - if(key.contains(com.ljsd.jieling.db.redis.RedisKey.CAR_DEALY_RANK)){ - LOGGER.info("此处删除了车迟斗法排行key:{},value:{}",key,value); - } - for (int i = 0; i < MAX_TRY_TIMES; i++) { - try { - redisTemplate.opsForZSet().remove(key, value); - return; - } catch (Exception e) { - TimeUtils.sleep(FAILED_SLEEP); - } - } - } - //操作sortset - public Set> getZsetRangeWithScore(String key, double min, double max){ - for (int i = 0; i < MAX_TRY_TIMES; i++) { - try { - return redisTemplate.opsForZSet().rangeByScoreWithScores(key,min,max); - } catch (Exception e) { - TimeUtils.sleep(FAILED_SLEEP); - } - } - return null; - } - - //操作sortset - public Set getZsetRange(String type,String key, double min, double max){ - String rkey= getKey(type, key); - for (int i = 0; i < MAX_TRY_TIMES; i++) { - try { - return redisTemplate.opsForZSet().rangeByScore(rkey,min,max); - } catch (Exception e) { - TimeUtils.sleep(FAILED_SLEEP); - } - } - return null; - } - - - //操作sortset - public int getZsetSize(String key){ - try { - return redisTemplate.opsForZSet().size(key).intValue(); - } catch (Exception e) { - e.printStackTrace(); - return 0; - } - } - - public Set getZset(String key,long start,long end){ - for (int i = 0; i < MAX_TRY_TIMES; i++) { - try { - return redisTemplate.opsForZSet().range(key, start, end); - } catch (Exception e) { - TimeUtils.sleep(FAILED_SLEEP); - } - } - return null; - } - public Set getReverseZset(String key,long start,long end){ - for (int i = 0; i < MAX_TRY_TIMES; i++) { - try { - return redisTemplate.opsForZSet().reverseRange(key, start, end); - } catch (Exception e) { - TimeUtils.sleep(FAILED_SLEEP); - } - } - return null; - } - - public Set> getZsetreverseRangeWithScores(String type,String key, long start, long end){ - String rkey = getKey(type, key); - for (int i = 0; i < MAX_TRY_TIMES; i++) { - try { - return redisTemplate.opsForZSet().reverseRangeWithScores(rkey, start, end); - } catch (Exception e) { - TimeUtils.sleep(FAILED_SLEEP); - } - } - return null; - } - - public Set> getZsetRangeWithScores(String type,String key, long start, long end){ - String rkey = getKey(type, key); - for (int i = 0; i < MAX_TRY_TIMES; i++) { - try { - return redisTemplate.opsForZSet().rangeWithScores(rkey, start, end); - } catch (Exception e) { - TimeUtils.sleep(FAILED_SLEEP); - } - } - return null; - } - - public Set> getAllZsetRange(String key){ - for (int i = 0; i < MAX_TRY_TIMES; i++) { - try { - return redisTemplate.opsForZSet().rangeWithScores(key, 0, -1); - } catch (Exception e) { - TimeUtils.sleep(FAILED_SLEEP); - } - } - return null; - } - - public Set> rangeByScoreWithScores(String type,String key, double start, double end){ - String rkey = getKey(type, key); - for (int i = 0; i < MAX_TRY_TIMES; i++) { - try { - return redisTemplate.opsForZSet().reverseRangeByScoreWithScores(rkey, start, end); - } catch (Exception e) { - TimeUtils.sleep(FAILED_SLEEP); - } - } - return null; - } - - - - public Long getZSetreverseRank(String type,String key, String value){ - String rkey = getKey(type,key); - for (int i = 0; i < MAX_TRY_TIMES; i++) { - try { - Long rank = redisTemplate.opsForZSet().reverseRank(rkey, value); - if (rank == null) { - return -1L; - } else { - return rank + 1; - } - } catch (Exception e) { - TimeUtils.sleep(FAILED_SLEEP); - } - } - return -1L; - } - - - public Double getZSetScore(String type,String key, String value){ - String rkey = getKey(type, key); - for (int i = 0; i < MAX_TRY_TIMES; i++) { - try { - Double score = redisTemplate.opsForZSet().score(rkey, value); - if (score == null) { - return -1D; - } else { - return score; - } - } catch (Exception e) { - TimeUtils.sleep(FAILED_SLEEP); - } - } - return -1D; - } - - public void updateZsetScores(String type,String key, Set> tuples){ - String rkey = getKey(type, key); - for (int i = 0; i < MAX_TRY_TIMES; i++) { - try { - redisTemplate.opsForZSet().add(rkey,tuples); - return; - } catch (Exception e) { - TimeUtils.sleep(FAILED_SLEEP); - } - } - - } - - public void incrementZsetScore(String type,String key, String value, int changeScore){ - incrementZsetScore(type,key,value,(double)(changeScore)); - } - public void incrementZsetScore(String type,String key, String value, double changeScore){ - String rkey = getKey(type, key); - for (int i = 0; i < MAX_TRY_TIMES; i++) { - try { - redisTemplate.opsForZSet().incrementScore(rkey,value,changeScore); - return; - } catch (Exception e) { - TimeUtils.sleep(FAILED_SLEEP); - } - } - - } - public List lGet(String type,String key, long start, long end) { - try { - String key1 = getKey(type, key); - return redisTemplate.opsForList().range(key1, start, end); - } catch (Exception e) { - e.printStackTrace(); - return null; - } - } - - public boolean lSet(String key, String value) { - try { - redisTemplate.opsForList().rightPush(key, value); - return true; - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } - - public void removeZSetRangeByRank(String key,int start,int end){ - for (int i = 0; i < MAX_TRY_TIMES; i++) { - try { - redisTemplate.opsForZSet().removeRange(key,start,end); - return; - } catch (Exception e) { - TimeUtils.sleep(FAILED_SLEEP); - } - } - } - - public void removeZSetRangeByScore(String type,String key,double min,double max){ - String rkey = getKey(type, key); - for (int i = 0; i < MAX_TRY_TIMES; i++) { - try { - redisTemplate.opsForZSet().removeRangeByScore(rkey,min,max); - return; - } catch (Exception e) { - TimeUtils.sleep(FAILED_SLEEP); - } - } - } - - // ===============================set================================= - - private static final String LOCK_SUCCESS = "OK"; - private static final byte[] SET_IF_NOT_EXIST = "NX".getBytes(); - private static final byte[] SET_WITH_EXPIRE_TIME = "PX".getBytes(); - - //requestId:设置为值,用于解锁时,判断您是否是加锁者解锁 - //expireTime:过期时间,依据请求的处理时间,不应该大于1秒,建议设置为500毫秒 - public boolean tryGetDistributedLock(String type, String key, String requestId, int expireTime) { - String rkey = getKey(type, key); - RedisConnection connection = null; - try { - connection = redisTemplate.getConnectionFactory().getConnection(); - for (int i = 0; i < MAX_TRY_TIMES; i++) { - try { - Object result = connection.execute("SET", - rkey.getBytes(), requestId.getBytes(), SET_IF_NOT_EXIST, - SET_WITH_EXPIRE_TIME, Protocol.toByteArray(expireTime)); - if (result == null) { - TimeUtils.sleep(100); //不能获取锁,延时100毫秒 - continue; - } - String strResult = new String((byte[]) result); - if (strResult.equals(LOCK_SUCCESS)) { - return true; - } - TimeUtils.sleep(200); //不能获取锁,延时100毫秒 - } catch (Exception e){ - LOGGER.error("tryGetDistributedLock->uid={},i={},rkey={},emsg={}", - GlobalData.getLocalUserId(), i, rkey, e.getMessage(), e); - TimeUtils.sleep(FAILED_SLEEP); - } - } - return false; - }finally { - if(connection != null) { - connection.close(); - } - } - } - - public Cursor scan(String match, int count){ - ScanOptions.ScanOptionsBuilder match1 = ScanOptions.scanOptions().match(match); - if(count!=-1) - match1.count(count); - RedisSerializer redisSerializer = (RedisSerializer) redisTemplate.getKeySerializer(); - return (Cursor) redisTemplate.executeWithStickyConnection((RedisCallback) redisConnection -> - new ConvertingCursor<>(redisConnection.scan(match1.build()), redisSerializer::deserialize)); - } - - private final static String UNLOCK_SCRIPT = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end"; - //requestId:加锁者,才能解锁 - public boolean releaseDistributedLock(String type, String key, String requestId) { - String rkey = getKey(type, key); - RedisConnection connection = null; - try { - connection = redisTemplate.getConnectionFactory().getConnection(); - Long result = connection.eval(UNLOCK_SCRIPT.getBytes(), ReturnType.INTEGER, 1, rkey.getBytes(), requestId.getBytes()); - if(result == null){ - return false; - } - if(result == 1){ - return true; - } - return false; - } finally { - if(connection != null) { - connection.close(); - } - } - } - public T getMapEntry(String type,String key,String mapKey,Class valueClazz){ - String rkey = getKey(type, key); - for (int i = 0; i < MAX_TRY_TIMES; i++) { - try { - Object o = redisTemplate.opsForHash().get(rkey, mapKey); - if(o!=null){ - return gson.fromJson(o.toString(),valueClazz); - } - } catch (Exception e) { - TimeUtils.sleep(FAILED_SLEEP); - } - } - return null; - } -} +//package com.world.redis; +// +// +//import com.google.gson.Gson; +//import com.ljsd.common.mogodb.util.GlobalData; +//import com.world.config.ConfigurableApplicationContextManager; +//import org.slf4j.Logger; +//import org.slf4j.LoggerFactory; +//import org.springframework.data.redis.RedisConnectionFailureException; +//import org.springframework.data.redis.connection.RedisConnection; +//import org.springframework.data.redis.connection.ReturnType; +//import org.springframework.data.redis.core.*; +//import org.springframework.data.redis.serializer.RedisSerializer; +//import org.springframework.util.CollectionUtils; +//import redis.clients.jedis.Protocol; +//import util.TimeUtils; +// +//import java.util.*; +//import java.util.concurrent.TimeUnit; +// +///** +// * Author: zsx +// * CreateDate: 2020/11/18 17:30 +// */ +//public class RedisUtil { +// +// private static final Logger LOGGER = LoggerFactory.getLogger(RedisUtil.class); +// +// +// private static Gson gson = new Gson(); +// private static int MAX_TRY_TIMES = 3; //最大尝试次数,保障获取/存储成功 +// private static int FAILED_SLEEP = 2; //每次失败最大停顿时间 +// +// private RedisUtil() { +// } +// +// public static RedisUtil getInstence() { +// return InnerClass.instance; +// } +// +// +// private static class InnerClass { +// private static final RedisUtil instance = new RedisUtil(); +// } +// +// private StringRedisTemplate redisTemplate; +// +// +// public void init(){ +// redisTemplate = ConfigurableApplicationContextManager.getBean(StringRedisTemplate.class); +// } +// +// /** +// * 删除缓存 +// * +// * @param key +// * 可以传一个值 或多个 +// */ +// @SuppressWarnings("unchecked") +// public void del(String... key) { +// if (key != null && key.length > 0) { +// if (key.length == 1) { +// redisTemplate.delete(key[0]); +// } else { +// redisTemplate.delete(CollectionUtils.arrayToList(key)); +// } +// } +// } +// +// // ============================String============================= +// /** +// * 普通缓存获取 +// * +// * @param key +// * 键 +// * @return 值 +// */ +// public Object get(String key) { +// return key == null ? null : redisTemplate.opsForValue().get(key); +// } +// +// public T getObject(String key,java.lang.reflect.Type type){ +// String source = redisTemplate.opsForValue().get(key); +// if(source == null){ +// return null; +// } +// return gson.fromJson(source,type); +// } +// +// public T getObject(String key,Class clazz){ +// String source = redisTemplate.opsForValue().get(key); +// if(source == null){ +// return null; +// } +// return gson.fromJson(source, clazz); +// } +// /** +// * 获取Object +// * +// * @param key +// * @return +// */ +// public T getObject(String type, String key, Class clazz, int expireTime) { +// try { +// String valueStr = redisTemplate.opsForValue().get(type + RedisKey.Delimiter_colon + key); +// if (valueStr == null) { +// return null; +// } +// if (expireTime > 0) { +// redisTemplate.expire(type + key, expireTime, TimeUnit.SECONDS); +// } +// return gson.fromJson(valueStr, clazz); +// } catch (RedisConnectionFailureException e) { +// LOGGER.error("------------------Redis 连接失败------------------msg={}", e); +// } +// return null; +// } +// +// +// +// public Long increment(String key) { +// for (int i = 0; i < MAX_TRY_TIMES; i++) { +// try { +// return redisTemplate.opsForValue().increment(key, 1); +// } catch (Exception e) { +// sleep(FAILED_SLEEP); +// } +// } +// return -1L; +// } +// +// public boolean set(String key, String value) { +// try { +// redisTemplate.opsForValue().set(key, value); +// return true; +// } catch (Exception e) { +// e.printStackTrace(); +// return false; +// } +// +// } +// +// public void set(String type, String rkey, T t) { +// String key = getKey(type, rkey); +// redisTemplate.opsForValue().set(key, gson.toJson(t)); +// } +// public void set(String mtype, String rkey, T t,java.lang.reflect.Type type) { +// String key = getKey(mtype, rkey); +// redisTemplate.opsForValue().set(key, gson.toJson(t,type)); +// } +// // ===============================set================================= +// +// public void saddAll(String key, String[] members){ +// try { +// redisTemplate.opsForSet().add(key,members); +// } catch (Exception e) { +// sleep(FAILED_SLEEP); +// } +// } +// +// public List getAllSetToList(String key, Class clazz){ +// for(int i=0;i members = redisTemplate.opsForSet().members(key); +// if(members!=null && !members.isEmpty()){ +// List result = new ArrayList<>(members.size()); +// for(String item : members){ +// result.add(gson.fromJson(item,clazz)); +// } +// return result; +// } +// } catch (Exception e) { +// sleep(FAILED_SLEEP); +// } +// } +// return null; +// +// } +// +// public boolean sadd(String key,String member){ +// try { +// redisTemplate.opsForSet().add(key,member); +// return true; +// } catch (Exception e) { +// e.printStackTrace(); +// return false; +// } +// } +// +// +// +// +// private void sleep(long sleepMs){ +// try { +// Thread.sleep(sleepMs); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// } +// +// +// // ================================Map================================= +// /** +// * HashGet +// * +// * @param key +// * 键 不能为null +// * @param mapKey +// * 项 不能为null +// * @return 值 +// */ +// public Object hget(String key, String mapKey) { +// return redisTemplate.opsForHash().get(key, mapKey); +// } +// +// /** +// * 获取hashKey对应的所有键值 +// * +// * @param key +// * 键 +// * @return 对应的多个键值 +// */ +// public Map hmget(String key) { +// return redisTemplate.opsForHash().entries(key); +// } +// +// /** +// * HashSet +// * +// * @param key +// * 键 +// * @param map +// * 对应多个键值 +// * @return true 成功 false 失败 +// */ +// public boolean hmset(String key, Map map) { +// try { +// redisTemplate.opsForHash().putAll(key, map); +// return true; +// } catch (Exception e) { +// e.printStackTrace(); +// return false; +// } +// } +// +// /** +// * HashSet 并设置时间 +// * +// * @param key +// * 键 +// * @param map +// * 对应多个键值 +// * @param time +// * 时间(秒) +// * @return true成功 false失败 +// */ +// public boolean hmset(String key, Map map, long time) { +// try { +// redisTemplate.opsForHash().putAll(key, map); +// if (time > 0) { +// expire(key, time); +// } +// return true; +// } catch (Exception e) { +// e.printStackTrace(); +// return false; +// } +// } +// +// /** +// * 向一张hash表中放入数据,如果不存在将创建 +// * +// * @param key +// * 键 +// * @param mapKey +// * 项 +// * @param value +// * 值 +// * @return true 成功 false失败 +// */ +//// public boolean hset(String key, String mapKey, Object value) { +//// try { +//// if (!(value instanceof String)){ +//// value = gson.toJson(value); +//// } +//// redisTemplate.opsForHash().put(key, mapKey, value); +//// return true; +//// } catch (Exception e) { +//// e.printStackTrace(); +//// return false; +//// } +//// } +// +// /** +// * 向一张hash表中放入数据,如果不存在将创建 +// * +// * @param key +// * 键 +// * @param mapKey +// * 项 +// * @param value +// * 值 +// * @param time +// * 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间 +// * @return true 成功 false失败 +// */ +//// public boolean hset(String key, String mapKey, Object value, long time) { +//// try { +//// if (!(value instanceof String)){ +//// value = gson.toJson(value); +//// } +//// redisTemplate.opsForHash().put(key, mapKey, value); +//// if (time > 0) { +//// expire(key, time); +//// } +//// return true; +//// } catch (Exception e) { +//// e.printStackTrace(); +//// return false; +//// } +//// } +// +// /** +// * 删除hash表中的值 +// * +// * @param key +// * 键 不能为null +// * @param item +// * 项 可以使多个 不能为null +// */ +// public void hdel(String key, Object... item) { +// redisTemplate.opsForHash().delete(key, item); +// } +// +// /** +// * 判断hash表中是否有该项的值 +// * +// * @param key +// * 键 不能为null +// * @param item +// * 项 不能为null +// * @return true 存在 false不存在 +// */ +// public boolean hHasKey(String key, String item) { +// return redisTemplate.opsForHash().hasKey(key, item); +// } +// +// /** +// * hash递增 如果不存在,就会创建一个 并把新增后的值返回 +// * +// * @param key +// * 键 +// * @param item +// * 项 +// * @param by +// * 要增加几(大于0) +// * @return +// */ +// public double hincr(String key, String item, double by) { +// return redisTemplate.opsForHash().increment(key, item, by); +// } +// +// /** +// * hash递减 +// * +// * @param key +// * 键 +// * @param item +// * 项 +// * @param by +// * 要减少记(小于0) +// * @return +// */ +// public double hdecr(String key, String item, double by) { +// return redisTemplate.opsForHash().increment(key, item, -by); +// } +// /** +// * 指定缓存失效时间 +// * +// * @param key +// * 键 +// * @param time +// * 时间(秒) +// * @return +// */ +// public boolean expire(String key, long time) { +// try { +// if (time > 0) { +// redisTemplate.expire(key, time, TimeUnit.SECONDS); +// } +// return true; +// } catch (Exception e) { +// e.printStackTrace(); +// return false; +// } +// } +//// public Map getMapValues(String type,String key,Class keyClazz,Class valueClazz){ +//// Map result = new HashMap<>(); +//// String rkey = getKey(type, key); +//// Map entries = redisTemplate.opsForHash().entries(rkey); +//// for(Map.Entry item : entries.entrySet()){ +//// Object key1 = item.getKey(); +//// Object value = item.getValue(); +//// result.put(gson.fromJson(gson.toJson(key1),keyClazz),gson.fromJson(value.toString(),valueClazz)); +//// } +//// return result; +//// } +// public Map getMapValues(String type,Class keyClazz,Class valueClazz){ +// Map result = new HashMap<>(); +// Map entries = redisTemplate.opsForHash().entries(type); +// for(Map.Entry item : entries.entrySet()){ +// Object key1 = item.getKey(); +// Object value = item.getValue(); +// result.put(gson.fromJson(gson.toJson(key1),keyClazz),gson.fromJson(value.toString(),valueClazz)); +// } +// return result; +// } +// public String getKey(String type,String key){ +// +// return type + RedisKey.Delimiter_colon + key; +// } +//// public void removeMapEntrys(String type, String key, Object... mapKeys){ +//// String rkey = getKey(type, key); +//// for (int i = 0; i < MAX_TRY_TIMES; i++) { +//// try { +//// redisTemplate.opsForHash().delete(rkey,mapKeys); +//// return; +//// } catch (Exception e) { +//// TimeUtils.sleep(FAILED_SLEEP); +//// } +//// } +//// } +//// +//// public void putMapEntry(String type,String key,String mapKey, T value){ +//// String rkey = getKey(type, key); +//// for (int i = 0; i < MAX_TRY_TIMES; i++) { +//// try { +//// redisTemplate.opsForHash().put(rkey,mapKey,gson.toJson(value)); +//// return; +//// } catch (Exception e) { +//// TimeUtils.sleep(FAILED_SLEEP); +//// } +//// } +//// } +// //操作sortset +// public void zsetAddAall(String key, Set> items){ +// for (int i = 0; i < MAX_TRY_TIMES; i++) { +// try { +// redisTemplate.opsForZSet().add(key,items); +// return; +// } catch (Exception e) { +// TimeUtils.sleep(FAILED_SLEEP); +// } +// } +// } +// +// //操作sortset +// public void zsetAddOne(String key, String uid, double useTime){ +// for (int i = 0; i < MAX_TRY_TIMES; i++) { +// try { +// redisTemplate.opsForZSet().add(key, uid, useTime); +// return; +// } catch (Exception e) { +// TimeUtils.sleep(FAILED_SLEEP); +// } +// } +// } +// +// //操作sortset score<<32+time +// public void zsetAddOneOffsetByTime(String key, String value, int score){ +// for (int i = 0; i < MAX_TRY_TIMES; i++) { +// try { +// long s= (long)score<<40; +// long sort = s+(0xFFFFFFFFFFL-(int)(System.currentTimeMillis()/1000)); +// redisTemplate.opsForZSet().add(key, value, sort); +// return; +// } catch (Exception e) { +// TimeUtils.sleep(FAILED_SLEEP); +// } +// } +// } +// +// public void zsetRemoveOne(String key, String value){ +// if(key.contains(com.ljsd.jieling.db.redis.RedisKey.CAR_DEALY_RANK)){ +// LOGGER.info("此处删除了车迟斗法排行key:{},value:{}",key,value); +// } +// for (int i = 0; i < MAX_TRY_TIMES; i++) { +// try { +// redisTemplate.opsForZSet().remove(key, value); +// return; +// } catch (Exception e) { +// TimeUtils.sleep(FAILED_SLEEP); +// } +// } +// } +// //操作sortset +// public Set> getZsetRangeWithScore(String key, double min, double max){ +// for (int i = 0; i < MAX_TRY_TIMES; i++) { +// try { +// return redisTemplate.opsForZSet().rangeByScoreWithScores(key,min,max); +// } catch (Exception e) { +// TimeUtils.sleep(FAILED_SLEEP); +// } +// } +// return null; +// } +// +// //操作sortset +// public Set getZsetRange(String type,String key, double min, double max){ +// String rkey= getKey(type, key); +// for (int i = 0; i < MAX_TRY_TIMES; i++) { +// try { +// return redisTemplate.opsForZSet().rangeByScore(rkey,min,max); +// } catch (Exception e) { +// TimeUtils.sleep(FAILED_SLEEP); +// } +// } +// return null; +// } +// +// +// //操作sortset +// public int getZsetSize(String key){ +// try { +// return redisTemplate.opsForZSet().size(key).intValue(); +// } catch (Exception e) { +// e.printStackTrace(); +// return 0; +// } +// } +// +// public Set getZset(String key,long start,long end){ +// for (int i = 0; i < MAX_TRY_TIMES; i++) { +// try { +// return redisTemplate.opsForZSet().range(key, start, end); +// } catch (Exception e) { +// TimeUtils.sleep(FAILED_SLEEP); +// } +// } +// return null; +// } +// public Set getReverseZset(String key,long start,long end){ +// for (int i = 0; i < MAX_TRY_TIMES; i++) { +// try { +// return redisTemplate.opsForZSet().reverseRange(key, start, end); +// } catch (Exception e) { +// TimeUtils.sleep(FAILED_SLEEP); +// } +// } +// return null; +// } +// +// public Set> getZsetreverseRangeWithScores(String type,String key, long start, long end){ +// String rkey = getKey(type, key); +// for (int i = 0; i < MAX_TRY_TIMES; i++) { +// try { +// return redisTemplate.opsForZSet().reverseRangeWithScores(rkey, start, end); +// } catch (Exception e) { +// TimeUtils.sleep(FAILED_SLEEP); +// } +// } +// return null; +// } +// +// public Set> getZsetRangeWithScores(String type,String key, long start, long end){ +// String rkey = getKey(type, key); +// for (int i = 0; i < MAX_TRY_TIMES; i++) { +// try { +// return redisTemplate.opsForZSet().rangeWithScores(rkey, start, end); +// } catch (Exception e) { +// TimeUtils.sleep(FAILED_SLEEP); +// } +// } +// return null; +// } +// +// public Set> getAllZsetRange(String key){ +// for (int i = 0; i < MAX_TRY_TIMES; i++) { +// try { +// return redisTemplate.opsForZSet().rangeWithScores(key, 0, -1); +// } catch (Exception e) { +// TimeUtils.sleep(FAILED_SLEEP); +// } +// } +// return null; +// } +// +// public Set> rangeByScoreWithScores(String type,String key, double start, double end){ +// String rkey = getKey(type, key); +// for (int i = 0; i < MAX_TRY_TIMES; i++) { +// try { +// return redisTemplate.opsForZSet().reverseRangeByScoreWithScores(rkey, start, end); +// } catch (Exception e) { +// TimeUtils.sleep(FAILED_SLEEP); +// } +// } +// return null; +// } +// +// +// +// public Long getZSetreverseRank(String type,String key, String value){ +// String rkey = getKey(type,key); +// for (int i = 0; i < MAX_TRY_TIMES; i++) { +// try { +// Long rank = redisTemplate.opsForZSet().reverseRank(rkey, value); +// if (rank == null) { +// return -1L; +// } else { +// return rank + 1; +// } +// } catch (Exception e) { +// TimeUtils.sleep(FAILED_SLEEP); +// } +// } +// return -1L; +// } +// +// +// public Double getZSetScore(String type,String key, String value){ +// String rkey = getKey(type, key); +// for (int i = 0; i < MAX_TRY_TIMES; i++) { +// try { +// Double score = redisTemplate.opsForZSet().score(rkey, value); +// if (score == null) { +// return -1D; +// } else { +// return score; +// } +// } catch (Exception e) { +// TimeUtils.sleep(FAILED_SLEEP); +// } +// } +// return -1D; +// } +// +// public void updateZsetScores(String type,String key, Set> tuples){ +// String rkey = getKey(type, key); +// for (int i = 0; i < MAX_TRY_TIMES; i++) { +// try { +// redisTemplate.opsForZSet().add(rkey,tuples); +// return; +// } catch (Exception e) { +// TimeUtils.sleep(FAILED_SLEEP); +// } +// } +// +// } +// +// public void incrementZsetScore(String type,String key, String value, int changeScore){ +// incrementZsetScore(type,key,value,(double)(changeScore)); +// } +// public void incrementZsetScore(String type,String key, String value, double changeScore){ +// String rkey = getKey(type, key); +// for (int i = 0; i < MAX_TRY_TIMES; i++) { +// try { +// redisTemplate.opsForZSet().incrementScore(rkey,value,changeScore); +// return; +// } catch (Exception e) { +// TimeUtils.sleep(FAILED_SLEEP); +// } +// } +// +// } +// public List lGet(String type,String key, long start, long end) { +// try { +// String key1 = getKey(type, key); +// return redisTemplate.opsForList().range(key1, start, end); +// } catch (Exception e) { +// e.printStackTrace(); +// return null; +// } +// } +// +// public boolean lSet(String key, String value) { +// try { +// redisTemplate.opsForList().rightPush(key, value); +// return true; +// } catch (Exception e) { +// e.printStackTrace(); +// return false; +// } +// } +// +// public void removeZSetRangeByRank(String key,int start,int end){ +// for (int i = 0; i < MAX_TRY_TIMES; i++) { +// try { +// redisTemplate.opsForZSet().removeRange(key,start,end); +// return; +// } catch (Exception e) { +// TimeUtils.sleep(FAILED_SLEEP); +// } +// } +// } +// +// public void removeZSetRangeByScore(String type,String key,double min,double max){ +// String rkey = getKey(type, key); +// for (int i = 0; i < MAX_TRY_TIMES; i++) { +// try { +// redisTemplate.opsForZSet().removeRangeByScore(rkey,min,max); +// return; +// } catch (Exception e) { +// TimeUtils.sleep(FAILED_SLEEP); +// } +// } +// } +// +// // ===============================set================================= +// +// private static final String LOCK_SUCCESS = "OK"; +// private static final byte[] SET_IF_NOT_EXIST = "NX".getBytes(); +// private static final byte[] SET_WITH_EXPIRE_TIME = "PX".getBytes(); +// +// //requestId:设置为值,用于解锁时,判断您是否是加锁者解锁 +// //expireTime:过期时间,依据请求的处理时间,不应该大于1秒,建议设置为500毫秒 +// public boolean tryGetDistributedLock(String type, String key, String requestId, int expireTime) { +// String rkey = getKey(type, key); +// RedisConnection connection = null; +// try { +// connection = redisTemplate.getConnectionFactory().getConnection(); +// for (int i = 0; i < MAX_TRY_TIMES; i++) { +// try { +// Object result = connection.execute("SET", +// rkey.getBytes(), requestId.getBytes(), SET_IF_NOT_EXIST, +// SET_WITH_EXPIRE_TIME, Protocol.toByteArray(expireTime)); +// if (result == null) { +// TimeUtils.sleep(100); //不能获取锁,延时100毫秒 +// continue; +// } +// String strResult = new String((byte[]) result); +// if (strResult.equals(LOCK_SUCCESS)) { +// return true; +// } +// TimeUtils.sleep(200); //不能获取锁,延时100毫秒 +// } catch (Exception e){ +// LOGGER.error("tryGetDistributedLock->uid={},i={},rkey={},emsg={}", +// GlobalData.getLocalUserId(), i, rkey, e.getMessage(), e); +// TimeUtils.sleep(FAILED_SLEEP); +// } +// } +// return false; +// }finally { +// if(connection != null) { +// connection.close(); +// } +// } +// } +// +// public Cursor scan(String match, int count){ +// ScanOptions.ScanOptionsBuilder match1 = ScanOptions.scanOptions().match(match); +// if(count!=-1) +// match1.count(count); +// RedisSerializer redisSerializer = (RedisSerializer) redisTemplate.getKeySerializer(); +// return (Cursor) redisTemplate.executeWithStickyConnection((RedisCallback) redisConnection -> +// new ConvertingCursor<>(redisConnection.scan(match1.build()), redisSerializer::deserialize)); +// } +// +// private final static String UNLOCK_SCRIPT = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end"; +// //requestId:加锁者,才能解锁 +// public boolean releaseDistributedLock(String type, String key, String requestId) { +// String rkey = getKey(type, key); +// RedisConnection connection = null; +// try { +// connection = redisTemplate.getConnectionFactory().getConnection(); +// Long result = connection.eval(UNLOCK_SCRIPT.getBytes(), ReturnType.INTEGER, 1, rkey.getBytes(), requestId.getBytes()); +// if(result == null){ +// return false; +// } +// if(result == 1){ +// return true; +// } +// return false; +// } finally { +// if(connection != null) { +// connection.close(); +// } +// } +// } +// public T getMapEntry(String type,String key,String mapKey,Class valueClazz){ +// String rkey = getKey(type, key); +// for (int i = 0; i < MAX_TRY_TIMES; i++) { +// try { +// Object o = redisTemplate.opsForHash().get(rkey, mapKey); +// if(o!=null){ +// return gson.fromJson(o.toString(),valueClazz); +// } +// } catch (Exception e) { +// TimeUtils.sleep(FAILED_SLEEP); +// } +// } +// return null; +// } +//} diff --git a/worldserver/src/main/java/com/world/thread/MinuteTask.java b/worldserver/src/main/java/com/world/thread/MinuteTask.java index 220379791..e712f6186 100644 --- a/worldserver/src/main/java/com/world/thread/MinuteTask.java +++ b/worldserver/src/main/java/com/world/thread/MinuteTask.java @@ -33,7 +33,7 @@ public class MinuteTask extends Thread { } try { LOGGER.info("MinuteTask start..."); - ArenaLogic.getInstance().minCheck(); +// ArenaLogic.getInstance().minCheck(); LOGGER.info("MinuteTask end..."); } catch (Exception e) { @@ -55,11 +55,11 @@ public class MinuteTask extends Thread { }catch (Exception e){ LOGGER.info("MongoUtil err ->{}", e); } - try { - ArenaLogic.clearReidsInTime(); - }catch (Exception e){ - LOGGER.info("clearReidsInTime err ->{}", e); - } +// try { +// ArenaLogic.clearReidsInTime(); +// }catch (Exception e){ +// LOGGER.info("clearReidsInTime err ->{}", e); +// } LOGGER.info("DailyOfZeroTask end ..."); }