redis模糊key,scan方法封装

back_recharge
duhui 2021-02-19 15:23:49 +08:00
parent 8c37ee1338
commit fa6937881c
4 changed files with 27 additions and 26 deletions

View File

@ -239,8 +239,7 @@ public class AreaManager {
//合并排行榜
//查找本服所有排行榜
Set<String> keys =new HashSet<>();
RedisUtil.getInstence().arenaAddDim(areaId+ "*" + "RANK*",200,keys);
Set<String> keys = RedisUtil.getInstence().getDimKey(areaId+ "*" + "RANK*",200);
keys.forEach(k->{
Set<ZSetOperations.TypedTuple<String>> set= RedisUtil.getInstence().getAllZsetRange(k);
@ -269,8 +268,7 @@ public class AreaManager {
//合并名称
//查找本服所有名称
Set<String> keys =new HashSet<>();
RedisUtil.getInstence().arenaAddDim(areaId+ ":" + RedisKey.C_User_Name_Key+"*",200,keys);
Set<String> keys = RedisUtil.getInstence().getDimKey(areaId+ ":" + RedisKey.C_User_Name_Key+"*",200);
keys.forEach(k->{
Object oldUserId = RedisUtil.getInstence().get(k);

View File

@ -70,32 +70,29 @@ public class RedisUtil {
new ConvertingCursor<>(redisConnection.scan(match1.build()), redisSerializer::deserialize));
}
public void arenaAddDim(String key, int count, Set<String> keys) throws IOException {
/**
* key
* @param key
* @param count
* @return
*/
public Set<String> getDimKey(String key, int count){
Set<String> keys = new HashSet<>();
long start = System.currentTimeMillis();
try {
Cursor<String> cursor = RedisUtil.getInstence().scan(key, count);
while (cursor.hasNext()){
//找到一次就添加一次
//添加key
keys.add(cursor.next());
}
cursor.close();
} catch (IOException e) {
e.printStackTrace();
}
LOGGER.info("scan扫描共耗时{} ms ,key数量{},模糊key{}",System.currentTimeMillis()-start,keys.size(),key);
return keys;
}
/**
* key
* serverId +":" + key + "*"
* @param key
* @param count
* @throws IOException
*/
public void delDim(String key, int count) throws IOException {
Cursor<String> cursor = RedisUtil.getInstence().scan(key, count);
while (cursor.hasNext()) {
//找到一次就添加一次
String next = cursor.next();
LOGGER.info("批量模糊删除{}",next);
RedisUtil.getInstence().del(next);
}
cursor.close();
}
/**
*
*

View File

@ -653,7 +653,10 @@ public class DeathPathLogic {
}
RedisUtil.getInstence().delDim(serverId +":DEATH_PATH*", -1);
Set<String> keys = RedisUtil.getInstence().getDimKey(serverId + ":DEATH_PATH*", -1);
keys.forEach(key->{
RedisUtil.getInstence().del(key);
});
}
public static int nextDiscrete(double[] probs) {

View File

@ -307,7 +307,10 @@ public class HelpHeroLogic {
int serverId = GameApplication.serverId;
String key = serverId +":" + RedisKey.HELP_FIGHT + "*";
// 模糊批量删除key
RedisUtil.getInstence().delDim(key,-1);
Set<String> keys = RedisUtil.getInstence().getDimKey(key, -1);
keys.forEach(k->{
RedisUtil.getInstence().del(k);
});
}
/**