From 2bc9115ed0d055a00e87e1820f2bced0caf9b99f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johnny=20Miller=20=28=E9=94=BA=E4=BF=8A=29?= Date: Mon, 28 Jun 2021 15:52:57 +0800 Subject: [PATCH] perf($auth-center): cache user info by login token --- .../user/entity/constant/UserRedisKey.java | 21 ++++++++++++++++++ .../user/service/impl/UserServiceImpl.java | 22 ++++++++++++++++--- 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/entity/constant/UserRedisKey.java diff --git a/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/entity/constant/UserRedisKey.java b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/entity/constant/UserRedisKey.java new file mode 100644 index 00000000..6712f4f9 --- /dev/null +++ b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/entity/constant/UserRedisKey.java @@ -0,0 +1,21 @@ +package com.jmsoftware.maf.authcenter.user.entity.constant; + +import lombok.Getter; + +/** + * Description: UserRedisKey, change description here. + * + * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 6/28/2021 3:18 PM + **/ +@Getter +public enum UserRedisKey { + /** + * Get user by login token key pattern, expired in random [1, 7) days + */ + GET_USER_BY_LOGIN_TOKEN(":user:get_user_by_login_token:%s"); + private final String keyInfixFormat; + + UserRedisKey(String keyInfixFormat) { + this.keyInfixFormat = keyInfixFormat; + } +} diff --git a/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/service/impl/UserServiceImpl.java b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/service/impl/UserServiceImpl.java index 156a1d55..4fcd9140 100644 --- a/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/service/impl/UserServiceImpl.java +++ b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/service/impl/UserServiceImpl.java @@ -1,20 +1,25 @@ package com.jmsoftware.maf.authcenter.user.service.impl; import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.util.BooleanUtil; import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.StrUtil; +import cn.hutool.json.JSONUtil; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import com.jmsoftware.maf.common.bean.PageResponseBodyBean; -import com.jmsoftware.maf.common.domain.authcenter.user.*; -import com.jmsoftware.maf.common.exception.SecurityException; import com.jmsoftware.maf.authcenter.security.service.JwtService; import com.jmsoftware.maf.authcenter.user.entity.GetUserPageListPayload; import com.jmsoftware.maf.authcenter.user.entity.GetUserStatusPayload; +import com.jmsoftware.maf.authcenter.user.entity.constant.UserRedisKey; import com.jmsoftware.maf.authcenter.user.entity.persistence.User; import com.jmsoftware.maf.authcenter.user.mapper.UserMapper; import com.jmsoftware.maf.authcenter.user.service.UserService; +import com.jmsoftware.maf.common.bean.PageResponseBodyBean; +import com.jmsoftware.maf.common.domain.authcenter.user.*; +import com.jmsoftware.maf.common.exception.SecurityException; +import com.jmsoftware.maf.springcloudstarter.configuration.MafProjectProperty; import com.jmsoftware.maf.springcloudstarter.util.UsernameUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -22,6 +27,7 @@ import org.springframework.cache.annotation.CacheConfig; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.http.HttpStatus; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; @@ -30,6 +36,7 @@ import javax.validation.Valid; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; +import java.util.concurrent.TimeUnit; /** *

UserServiceImpl

@@ -47,9 +54,17 @@ public class UserServiceImpl extends ServiceImpl implements Us private final BCryptPasswordEncoder bCryptPasswordEncoder; private final JwtService jwtService; private final MessageSource messageSource; + private final MafProjectProperty mafProjectProperty; + private final RedisTemplate redisTemplate; @Override public GetUserByLoginTokenResponse getUserByLoginToken(@NotBlank String loginToken) { + val key = String.format(mafProjectProperty.getProjectParentArtifactId() + + UserRedisKey.GET_USER_BY_LOGIN_TOKEN.getKeyInfixFormat(), loginToken); + val hasKey = redisTemplate.hasKey(key); + if (BooleanUtil.isTrue(hasKey)) { + return JSONUtil.toBean(redisTemplate.opsForValue().get(key), GetUserByLoginTokenResponse.class); + } val wrapper = Wrappers.lambdaQuery(User.class); wrapper.and(queryWrapper -> queryWrapper.eq(User::getUsername, loginToken) .or() @@ -62,6 +77,7 @@ public GetUserByLoginTokenResponse getUserByLoginToken(@NotBlank String loginTok } val response = new GetUserByLoginTokenResponse(); BeanUtil.copyProperties(userPersistence, response); + redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(response), RandomUtil.randomLong(1, 7), TimeUnit.DAYS); return response; }