UserTokenCache.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package com.kmall.api.cache;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
  4. import com.kmall.api.entity.TokenEntity;
  5. import com.kmall.api.entity.UserVo;
  6. import com.kmall.common.utils.StringUtils;
  7. import com.kmall.common.utils.redis.JedisUtil;
  8. import org.apache.log4j.Logger;
  9. /**
  10. * 会员缓存
  11. *
  12. * @author Scott
  13. */
  14. public class UserTokenCache {
  15. private static Logger logger = Logger.getLogger(UserTokenCache.class);
  16. static SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
  17. /**
  18. * 存放user和token的关联关系
  19. */
  20. public static final String XCX_TOKEN_USER_PREFIXX = "xcx_token_user_";
  21. public static final String XCX_USER_TOKEN_PREFIXX = "xcx_user_token_";
  22. public static final String XCX_USER_INFO_PREFIXX = "xcx_user_info_";
  23. public static int wsTokenExpireTime = 3600 * 24 * 30;
  24. /**
  25. * 根据用户Id
  26. *
  27. * @param userId
  28. * @return
  29. */
  30. public static TokenEntity getUserTokenByUserId(Long userId) {
  31. String temp = JedisUtil.get(toUserTokenKey(userId));
  32. if (!org.springframework.util.StringUtils.isEmpty(temp)) {
  33. TokenEntity d = JSON.parseObject(temp, TokenEntity.class);
  34. return d;
  35. } else {
  36. return null;
  37. }
  38. }
  39. /**
  40. * 根据token
  41. *
  42. * @param token
  43. * @return
  44. */
  45. public static TokenEntity getUserInfoByToken(String token) {
  46. String temp = JedisUtil.get(toTokenUserKey(token));
  47. if (StringUtils.isNullOrEmpty(temp)) {
  48. return null;
  49. }
  50. String userID = toUserTokenKey(Long.valueOf(temp));
  51. String tokenEntityJson = JedisUtil.get(userID);
  52. if (!org.springframework.util.StringUtils.isEmpty(tokenEntityJson)) {
  53. TokenEntity d = JSON.parseObject(tokenEntityJson, TokenEntity.class);
  54. return d;
  55. } else {
  56. return null;
  57. }
  58. }
  59. public static void del(Long userId) {
  60. JedisUtil.del(toUserTokenKey(userId));
  61. }
  62. /**
  63. * 放置用户Id和token关系
  64. *
  65. * @param tokenEntity
  66. */
  67. public static void putUserToken(TokenEntity tokenEntity) {
  68. JedisUtil.set(toUserTokenKey(tokenEntity.getUserId()), fromTokenCacheString(tokenEntity), wsTokenExpireTime);
  69. JedisUtil.set(toTokenUserKey(tokenEntity.getToken()), tokenEntity.getUserId().toString(), wsTokenExpireTime);
  70. }
  71. private static String fromTokenCacheString(TokenEntity d) {
  72. if (d == null)
  73. return null;
  74. return JSON.toJSONString(d, filter);
  75. }
  76. private static String toUserTokenKey(Long userId) {
  77. return XCX_TOKEN_USER_PREFIXX + userId;
  78. }
  79. private static String toTokenUserKey(String token) {
  80. return XCX_USER_TOKEN_PREFIXX + token;
  81. }
  82. private static String toUserInfoKey(Long userId) {
  83. return XCX_USER_INFO_PREFIXX + userId;
  84. }
  85. public static UserVo getUserInfo(Long userId) {
  86. // logger.error("getUserInfo key:" + toUserInfoKey(userId));
  87. String temp = JedisUtil.get(toUserInfoKey(userId));
  88. // logger.error("getUserInfo temp:" + temp);
  89. if (!org.springframework.util.StringUtils.isEmpty(temp)) {
  90. UserVo d = JSON.parseObject(temp, UserVo.class);
  91. return d;
  92. } else {
  93. return null;
  94. }
  95. }
  96. public static void putUserInfo(UserVo userVo) {
  97. JedisUtil.set(toUserInfoKey(userVo.getId()), fromUserCacheString(userVo), wsTokenExpireTime);
  98. }
  99. private static String fromUserCacheString(UserVo d) {
  100. if (d == null)
  101. return null;
  102. return JSON.toJSONString(d, filter);
  103. }
  104. }