>> map = new HashMap<>();
+ for(RedisRecord redisRecord : willSend){
+ String topic = redisRecord.getTopic();
+ if(!map.containsKey(topic)){
+ map.put(topic,new LinkedHashSet<>());
+ }
+ long increment = RedisUtil.getInstence().increment(topic+"_num");
+ redisRecord.setMsgNum(increment);
+ String itemInfo = gson.toJson(redisRecord);
+ map.get(topic).add(new DefaultTypedTuple<>(itemInfo, (double) increment));
+ }
+ for(String topic : map.keySet()){
+ RedisUtil.getInstence().zsetAddAall(topic,map.get(topic));
+ }
+
+ }catch (Exception e){
+ LOGGER.error("the exception",e);
+ }
+ }
+ public void addEntry(RedisRecord redisRecord){
+ redisRecords.add(redisRecord);
+ }
+}
diff --git a/src/main/java/com/jmfy/util/RedisRunTimeException.java b/src/main/java/com/jmfy/util/RedisRunTimeException.java
new file mode 100644
index 0000000..eb236db
--- /dev/null
+++ b/src/main/java/com/jmfy/util/RedisRunTimeException.java
@@ -0,0 +1,15 @@
+package com.jmfy.util;
+
+public class RedisRunTimeException extends RuntimeException{
+ public RedisRunTimeException()
+ {
+ super();
+ }
+ public RedisRunTimeException(String msg){
+ super(msg);
+ }
+ public RedisRunTimeException(Throwable cau)
+ {
+ super(cau);
+ }
+}
diff --git a/src/main/java/com/jmfy/util/RedisUtil.java b/src/main/java/com/jmfy/util/RedisUtil.java
new file mode 100644
index 0000000..b8f2d93
--- /dev/null
+++ b/src/main/java/com/jmfy/util/RedisUtil.java
@@ -0,0 +1,337 @@
+package com.jmfy.util;
+
+import com.google.gson.Gson;
+import com.jmfy.redisProperties.RedisProperties;
+import com.jmfy.redisProperties.RedisUserKey;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.data.redis.RedisConnectionFailureException;
+import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
+import org.springframework.data.redis.core.StringRedisTemplate;
+import org.springframework.data.redis.core.ZSetOperations;
+import redis.clients.jedis.JedisPoolConfig;
+
+import java.util.*;
+import java.util.concurrent.TimeUnit;
+
+
+/**
+ * 用来储存返回改前端的respose的实体 当断线重连时下次请求直接返回缓存中的数据
+ *
+ * 添加储存玩家常用数据
+ */
+public class RedisUtil {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(RedisUtil.class);
+
+ private static int MAX_TRY_TIMES = 3; //最大尝试次数,保障获取/存储成功
+ private static int FAILED_SLEEP = 2; //每次失败最大停顿时间
+
+ private StringRedisTemplate redisObjectTemplate;
+
+ public RedisUtil() {
+ }
+
+ private static RedisUtil redisUtil;
+ private Gson gson = new Gson();
+
+ public static RedisUtil getInstence() {
+ if (redisUtil == null) {
+ redisUtil = InnerClass.SINGLETON;
+ }
+ return redisUtil;
+ }
+
+ private static class InnerClass {
+ private static final RedisUtil SINGLETON = new RedisUtil();
+ }
+
+ public void init(RedisProperties redisProperties) {
+ try {
+ //initStringCacheDb(0, redisProperties);
+ initUserCacheDb(0, redisProperties);
+ LOGGER.info("redis Host={},port={},redis init ..", redisProperties.getHost(), redisProperties.getPort());
+
+ } catch (Exception e) {
+ LOGGER.error("------------------Redis 未启动------------------");
+ }
+
+
+ }
+
+ private void initUserCacheDb(int dbIndex, RedisProperties redisProperties) {
+ //实例化链接工厂
+ JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
+ //设置host
+ connectionFactory.setHostName(redisProperties.getHost());
+ //设置端口
+ connectionFactory.setPort(redisProperties.getPort());
+ // 分库
+ connectionFactory.setDatabase(dbIndex);
+ //设置密码
+ connectionFactory.setPassword(redisProperties.getPassword());
+ // 设置参数
+ JedisPoolConfig config = new JedisPoolConfig();
+ //连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
+ config.setBlockWhenExhausted(true);
+ //设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)
+ config.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy");
+ //是否启用pool的jmx管理功能, 默认true
+ config.setJmxEnabled(true);
+ //MBean ObjectName = new ObjectName("org.apache.commons.pool2:type=GenericObjectPool,name=" + "pool" + i); 默 认为"pool", JMX不熟,具体不知道是干啥的...默认就好.
+ config.setJmxNamePrefix("pool");
+ //是否启用后进先出, 默认true
+ config.setLifo(true);
+ //最大空闲连接数, 默认8个
+ config.setMaxIdle(8);
+ //最大连接数, 默认8个
+ config.setMaxTotal(1000);
+ //获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1
+ config.setMaxWaitMillis(-1);
+ //逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
+ config.setMinEvictableIdleTimeMillis(1800000);
+ //最小空闲连接数, 默认0
+ config.setMinIdle(0);
+ //每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
+ config.setNumTestsPerEvictionRun(3);
+ //对象空闲多久后逐出, 当空闲时间>该值 且 空闲连接>最大空闲数 时直接逐出,不再根据MinEvictableIdleTimeMillis判断 (默认逐出策略)
+ config.setSoftMinEvictableIdleTimeMillis(1800000);
+ //在获取连接的时候检查有效性, 默认false
+ config.setTestOnBorrow(false);
+ //在空闲时检查有效性, 默认false
+ config.setTestWhileIdle(false);
+ //逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
+ config.setTimeBetweenEvictionRunsMillis(-1);
+
+ connectionFactory.setPoolConfig(config);
+
+ //初始化connectionFactory
+ connectionFactory.afterPropertiesSet();
+ //实例化
+ redisObjectTemplate = new StringRedisTemplate(connectionFactory);
+// redisObjectTemplate.setConnectionFactory(connectionFactory);
+ redisObjectTemplate.afterPropertiesSet();
+ }
+
+ /**
+ * 获取Object
+ *
+ * @param key
+ * @return
+ */
+ public T getObject(String type, String key, Class clazz, int expireTime) {
+ try {
+ String valueStr = redisObjectTemplate.opsForValue().get(type + RedisUserKey.Delimiter_colon + key);
+ if (valueStr == null) {
+ return null;
+ }
+ if (expireTime > 0) {
+ redisObjectTemplate.expire(type + key, expireTime, TimeUnit.SECONDS);
+ }
+ return gson.fromJson(valueStr, clazz);
+ } catch (RedisConnectionFailureException e) {
+ LOGGER.error("------------------Redis 连接失败------------------msg={}", e);
+ }
+ return null;
+ }
+
+ public void delHash(String type, String key, String mapId) {
+ redisObjectTemplate.opsForHash().delete(type + ":" + key, mapId);
+ }
+
+ /**
+ * 更新Object
+ *
+ * @param key
+ * @param value
+ */
+ public void putObject(String type, String key, T value, int expireTime) {
+ try {
+ String valueStr = gson.toJson(value);
+ redisObjectTemplate.opsForValue().set(type + RedisUserKey.Delimiter_colon + key, valueStr);
+ if (expireTime > 0) {
+ redisObjectTemplate.expire(type + RedisUserKey.Delimiter_colon + key, expireTime, TimeUnit.SECONDS);
+ }
+ } catch (RedisConnectionFailureException e) {
+ LOGGER.error("------------------Redis 连接失败-----------------msg={}", e);
+ delObject(type, key);
+ return;
+ }
+ }
+
+ /**
+ * 更新Map某hashKey
+ *
+ * @param type
+ * @param key
+ * @param mapKey
+ * @param value
+ */
+ public void putMapEntry(String type, String key, String mapKey, T value, int expireTime) {
+ try {
+ String valueStr = gson.toJson(value);
+ redisObjectTemplate.opsForHash().put(type + RedisUserKey.Delimiter_colon + key, mapKey, valueStr);
+ if (expireTime > 0) {
+ redisObjectTemplate.expire(type + RedisUserKey.Delimiter_colon + key, expireTime, TimeUnit.SECONDS);
+ }
+ } catch (RedisConnectionFailureException e) {
+ LOGGER.error("------------------Redis 连接失败-----------------msg={}", e);
+ delObject(type, key);
+ }
+ }
+ public void putMap(String type, String key, String mapKey, String value, int expireTime) {
+ try {
+ redisObjectTemplate.opsForHash().put(type + RedisUserKey.Delimiter_colon + key, mapKey, value);
+ if (expireTime > 0) {
+ redisObjectTemplate.expire(type + RedisUserKey.Delimiter_colon + key, expireTime, TimeUnit.SECONDS);
+ }
+ } catch (RedisConnectionFailureException e) {
+ LOGGER.error("------------------Redis 连接失败-----------------msg={}", e);
+ delObject(type, key);
+ }
+ }
+
+ /**
+ * 删除Map中某hashKey值
+ *
+ * @param type
+ * @param key
+ * @param mapKey
+ */
+ public void delMapKey(String type, String key, String... mapKey) {
+ try {
+ redisObjectTemplate.opsForHash().delete(type + RedisUserKey.Delimiter_colon + key, mapKey);
+ } catch (RedisConnectionFailureException e) {
+ LOGGER.error("------------------Redis 连接失败----------------msg={}", e);
+ }
+ }
+
+ /**
+ * 删除Object
+ *
+ * @param type
+ * @param key
+ */
+ public void delObject(String type, String key) {
+ try {
+ redisObjectTemplate.delete(type + RedisUserKey.Delimiter_colon + key);
+ } catch (RedisConnectionFailureException e) {
+ LOGGER.error("------------------Redis 连接失败----------------msg={}", e);
+ } catch (Exception e) {
+ redisObjectTemplate.delete(type + RedisUserKey.Delimiter_colon + key);
+ }
+ }
+
+ /**
+ * 获取Map
+ *
+ * @param key
+ * @return
+ */
+ public Map getMap(String key, Class clazz, int expireTime) {
+ try {
+ Map reslut = new HashMap<>();
+ Map