工具类添加

lvxinran 2020-12-01 10:24:46 +08:00
parent 4d9a185be5
commit b89e6988fe
2 changed files with 94 additions and 0 deletions

View File

@ -13,4 +13,6 @@ public class RedisKey {
public static final String MATCH_SERVER_INFO = "MATCH_SERVER_INFO"; //匹配服务器地址信息 public static final String MATCH_SERVER_INFO = "MATCH_SERVER_INFO"; //匹配服务器地址信息
public static final String MATCH_SIZE = "MATCH_SIZE"; //匹配人数设置 public static final String MATCH_SIZE = "MATCH_SIZE"; //匹配人数设置
public static final String MATCHED_SPLIT_KEY = "MATCHED_SPLIT_KEY";
} }

View File

@ -157,5 +157,97 @@ public class RedisUtil {
} }
} }
/**
*
*
* @param key
*
* @param value
*
* @param time
* () time0 time0
* @return true false
*/
public boolean set(String key, String value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
//操作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
public Set<ZSetOperations.TypedTuple<String>> getZsetRevRangeWithScore(String key, double min, double max){
for (int i = 0; i < MAX_TRY_TIMES; i++) {
try {
return redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key,min,max);
} catch (Exception e) {
TimeUtils.sleep(FAILED_SLEEP);
}
}
return null;
}
public <T> void putMapEntrys(String type, Map<String,T> valus){
Map<String, String> result = new HashMap<>();
valus.forEach((k,v)->{
result.put(k,gson.toJson(v));
});
for (int i = 0; i < MAX_TRY_TIMES; i++) {
try {
redisTemplate.opsForHash().putAll(type,result);
return;
} catch (Exception e) {
TimeUtils.sleep(FAILED_SLEEP);
}
}
}
public <T> List<T> getMapEntrys(String type,Collection<Object> mapKeys, Class<T> valueClazz){
List<T> result = new ArrayList<>();
for (int i = 0; i < MAX_TRY_TIMES; i++) {
try {
List<Object> objects = redisTemplate.opsForHash().multiGet(type, mapKeys);
if(objects!=null &&!objects.isEmpty()){
for(Object o : objects){
if(o!=null){
result.add(gson.fromJson(o.toString(),valueClazz));
}
}
return result;
}
} catch (Exception e) {
TimeUtils.sleep(FAILED_SLEEP);
}
}
return result;
}
public <K,T> Map<K,T> getMapValues(String type,Class<K> keyClazz,Class<T> valueClazz){
Map<K,T> result = new HashMap<>();
Map<Object, Object> entries = redisTemplate.opsForHash().entries(type);
for(Map.Entry<Object,Object> 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;
}
} }