/jyqp_netty/src/main/java/com/zk/netty/redis/RedisUserService.java

https://bitbucket.org/shiyun3210/jyqp · Java · 189 lines · 129 code · 15 blank · 45 comment · 3 complexity · 906837c7bf45ae4dec5b33b9390b8a48 MD5 · raw file

  1. package com.zk.netty.redis;
  2. import io.netty.channel.DefaultChannelId;
  3. import java.util.Map;
  4. import org.apache.commons.lang.StringUtils;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.zk.netty.enums.GameRoom;
  7. import com.zk.netty.redis.db.JedisManager;
  8. import redis.clients.jedis.Jedis;
  9. public class RedisUserService {
  10. /**
  11. * 获取token
  12. * @param uid
  13. * @return
  14. */
  15. public static String getToken(String uid){
  16. Jedis jedis = null;
  17. try {
  18. jedis = JedisManager.getJedis();
  19. return jedis.get(RedisCommonKey.TOKEN_KEY+uid);
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }finally{
  23. if(jedis!=null){
  24. jedis.close();
  25. }
  26. }
  27. return "";
  28. }
  29. /**
  30. * 获取用户信息
  31. * @param uid
  32. * @return
  33. */
  34. @SuppressWarnings("unchecked")
  35. public static Map<String,String> getUserinfo(String uid){
  36. Jedis jedis = null;
  37. try {
  38. jedis = JedisManager.getJedis();
  39. String str = jedis.get(RedisCommonKey.USERINFO_KEY+uid);
  40. if(StringUtils.isNotBlank(str)){
  41. return JSONObject.parseObject(str, Map.class);
  42. }
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. }finally{
  46. JedisManager.closeConn(jedis);
  47. }
  48. return null;
  49. }
  50. /**
  51. * 设置用户channelId
  52. * @param uid
  53. * @param defaultChannelId
  54. */
  55. public static void setUserChannelId(String uid,DefaultChannelId defaultChannelId){
  56. Jedis jedis = null;
  57. try {
  58. jedis = JedisManager.getJedis();
  59. jedis.set((RedisCommonKey.USER_CHANNELID+uid).getBytes(), SerializeUtil.serialize(defaultChannelId));
  60. } catch (Exception e) {
  61. e.printStackTrace();
  62. }finally{
  63. JedisManager.closeConn(jedis);
  64. }
  65. }
  66. /**
  67. * 获取用户channelId
  68. * @param uid
  69. * @param defaultChannelId
  70. */
  71. public static DefaultChannelId getUserChannelId(String uid){
  72. Jedis jedis = null;
  73. try {
  74. jedis = JedisManager.getJedis();
  75. byte[] bb = jedis.get((RedisCommonKey.USER_CHANNELID+uid).getBytes());
  76. return (DefaultChannelId)SerializeUtil.unserialize(bb);
  77. } catch (Exception e) {
  78. e.printStackTrace();
  79. }finally{
  80. JedisManager.closeConn(jedis);
  81. }
  82. return null;
  83. }
  84. /**
  85. * 删除用户channelId
  86. * @param uid
  87. * @param defaultChannelId
  88. */
  89. public static void delUserChannelId(String uid){
  90. Jedis jedis = null;
  91. try {
  92. jedis = JedisManager.getJedis();
  93. jedis.del((RedisCommonKey.USER_CHANNELID+uid).getBytes());
  94. } catch (Exception e) {
  95. e.printStackTrace();
  96. }finally{
  97. JedisManager.closeConn(jedis);
  98. }
  99. }
  100. /**
  101. * 设置 用户所在房间
  102. * @param uid
  103. * @param roomId
  104. */
  105. public static void setUserInRoom(String uid,String roomId){
  106. Jedis jedis = null;
  107. try {
  108. jedis = JedisManager.getJedis();
  109. jedis.set(RedisCommonKey.USER_IN_ROOM+uid, roomId);
  110. } catch (Exception e) {
  111. e.printStackTrace();
  112. }finally{
  113. JedisManager.closeConn(jedis);
  114. }
  115. }
  116. /**
  117. * 获取 用户所在房间
  118. * @param uid
  119. */
  120. public static String getUserInRoom(String uid){
  121. Jedis jedis = null;
  122. try {
  123. jedis = JedisManager.getJedis();
  124. return jedis.get(RedisCommonKey.USER_IN_ROOM+uid);
  125. } catch (Exception e) {
  126. e.printStackTrace();
  127. }finally{
  128. JedisManager.closeConn(jedis);
  129. }
  130. return null;
  131. }
  132. /**
  133. * 删除 用户所在房间
  134. * @param uid
  135. */
  136. public static void delUserInRoom(String uid){
  137. Jedis jedis = null;
  138. try {
  139. jedis = JedisManager.getJedis();
  140. jedis.del(RedisCommonKey.USER_IN_ROOM+uid);
  141. } catch (Exception e) {
  142. e.printStackTrace();
  143. }finally{
  144. JedisManager.closeConn(jedis);
  145. }
  146. }
  147. public static void main(String[] args) {
  148. String roomId = "sjroom_1_1";
  149. System.out.println(roomId.substring(0, roomId.lastIndexOf("_")+1));
  150. }
  151. /**
  152. * 将用户从房间清除
  153. * @param uid
  154. */
  155. public static void clearUserByRoom(String uid,String roomId){
  156. Jedis jedis = null;
  157. try {
  158. jedis = JedisManager.getJedis();
  159. //房间人数减1
  160. jedis.zincrby(GameRoom.getInstanceBy(roomId.substring(0, roomId.lastIndexOf("_")+1)).getRoomListCacheCode(), -1, roomId);
  161. //移除用户在房间里的信息
  162. jedis.zremrangeByScore(roomId, uid, uid);
  163. //房间准备人数 是否减1
  164. int result = jedis.hdel(RedisCommonKey.ROOM_READY_HASH_FLAG+roomId, uid).intValue();
  165. if(result>0){//已准备
  166. jedis.incrBy(RedisCommonKey.ROOM_READY_FLAG+roomId,-1).intValue();
  167. }
  168. } catch (Exception e) {
  169. e.printStackTrace();
  170. }finally{
  171. JedisManager.closeConn(jedis);
  172. }
  173. }
  174. }