123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package com.kmall.api.cache;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
- import com.kmall.api.entity.TokenEntity;
- import com.kmall.api.entity.UserVo;
- import com.kmall.common.utils.StringUtils;
- import com.kmall.common.utils.redis.JedisUtil;
- import org.apache.log4j.Logger;
- /**
- * 会员缓存
- *
- * @author Scott
- */
- public class UserTokenCache {
- private static Logger logger = Logger.getLogger(UserTokenCache.class);
- static SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
- /**
- * 存放user和token的关联关系
- */
- public static final String XCX_TOKEN_USER_PREFIXX = "xcx_token_user_";
- public static final String XCX_USER_TOKEN_PREFIXX = "xcx_user_token_";
- public static final String XCX_USER_INFO_PREFIXX = "xcx_user_info_";
- public static int wsTokenExpireTime = 3600 * 24 * 30;
- /**
- * 根据用户Id
- *
- * @param userId
- * @return
- */
- public static TokenEntity getUserTokenByUserId(Long userId) {
- String temp = JedisUtil.get(toUserTokenKey(userId));
- if (!org.springframework.util.StringUtils.isEmpty(temp)) {
- TokenEntity d = JSON.parseObject(temp, TokenEntity.class);
- return d;
- } else {
- return null;
- }
- }
- /**
- * 根据token
- *
- * @param token
- * @return
- */
- public static TokenEntity getUserInfoByToken(String token) {
- String temp = JedisUtil.get(toTokenUserKey(token));
- if (StringUtils.isNullOrEmpty(temp)) {
- return null;
- }
- String userID = toUserTokenKey(Long.valueOf(temp));
- String tokenEntityJson = JedisUtil.get(userID);
- if (!org.springframework.util.StringUtils.isEmpty(tokenEntityJson)) {
- TokenEntity d = JSON.parseObject(tokenEntityJson, TokenEntity.class);
- return d;
- } else {
- return null;
- }
- }
- public static void del(Long userId) {
- JedisUtil.del(toUserTokenKey(userId));
- }
- /**
- * 放置用户Id和token关系
- *
- * @param tokenEntity
- */
- public static void putUserToken(TokenEntity tokenEntity) {
- JedisUtil.set(toUserTokenKey(tokenEntity.getUserId()), fromTokenCacheString(tokenEntity), wsTokenExpireTime);
- JedisUtil.set(toTokenUserKey(tokenEntity.getToken()), tokenEntity.getUserId().toString(), wsTokenExpireTime);
- }
- private static String fromTokenCacheString(TokenEntity d) {
- if (d == null)
- return null;
- return JSON.toJSONString(d, filter);
- }
- private static String toUserTokenKey(Long userId) {
- return XCX_TOKEN_USER_PREFIXX + userId;
- }
- private static String toTokenUserKey(String token) {
- return XCX_USER_TOKEN_PREFIXX + token;
- }
- private static String toUserInfoKey(Long userId) {
- return XCX_USER_INFO_PREFIXX + userId;
- }
- public static UserVo getUserInfo(Long userId) {
- // logger.error("getUserInfo key:" + toUserInfoKey(userId));
- String temp = JedisUtil.get(toUserInfoKey(userId));
- // logger.error("getUserInfo temp:" + temp);
- if (!org.springframework.util.StringUtils.isEmpty(temp)) {
- UserVo d = JSON.parseObject(temp, UserVo.class);
- return d;
- } else {
- return null;
- }
- }
- public static void putUserInfo(UserVo userVo) {
- JedisUtil.set(toUserInfoKey(userVo.getId()), fromUserCacheString(userVo), wsTokenExpireTime);
- }
- private static String fromUserCacheString(UserVo d) {
- if (d == null)
- return null;
- return JSON.toJSONString(d, filter);
- }
- }
|