Skip to content

Commit

Permalink
perf($auth-center): cache role info by user id
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Jun 28, 2021
1 parent 2bc9115 commit e9d935b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.jmsoftware.maf.authcenter.role.entity.constant;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* <h1>RoleRedisKey</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com, 6/28/21 9:45 PM
**/
@Getter
@RequiredArgsConstructor
public enum RoleRedisKey {
/**
* Get user by login token key pattern, expired in random [1, 7) days
*/
GET_ROLE_LIST_BY_USER_ID(":role:get_role_list_by_user_id:%s");

private final String keyInfixFormat;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jmsoftware.maf.authcenter.role.entity.constant.RoleRedisKey;
import com.jmsoftware.maf.authcenter.role.entity.persistence.Role;
import com.jmsoftware.maf.authcenter.role.mapper.RoleMapper;
import com.jmsoftware.maf.authcenter.role.service.RoleService;
import com.jmsoftware.maf.common.domain.authcenter.role.GetRoleListByUserIdResponse;
import com.jmsoftware.maf.springcloudstarter.configuration.MafConfiguration;
import com.jmsoftware.maf.springcloudstarter.configuration.MafProjectProperty;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
Expand All @@ -32,17 +38,26 @@
@Service
@RequiredArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
private final MafProjectProperty mafProjectProperty;
private final MafConfiguration mafConfiguration;
private final RedisTemplate<String, String> redisTemplate;

@Override
public GetRoleListByUserIdResponse getRoleList(@NotNull Long userId) {
val key = String.format(mafProjectProperty.getProjectParentArtifactId()
+ RoleRedisKey.GET_ROLE_LIST_BY_USER_ID.getKeyInfixFormat(), userId);
val hasKey = redisTemplate.hasKey(key);
if (BooleanUtil.isTrue(hasKey)) {
return JSONUtil.toBean(redisTemplate.opsForValue().get(key), GetRoleListByUserIdResponse.class);
}
val roleList = this.getRoleListByUserId(userId);
GetRoleListByUserIdResponse response = new GetRoleListByUserIdResponse();
roleList.forEach(rolePersistence -> {
GetRoleListByUserIdResponse.Role role = new GetRoleListByUserIdResponse.Role();
BeanUtil.copyProperties(rolePersistence, role);
response.getRoleList().add(role);
});
redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(response), RandomUtil.randomLong(1, 7), TimeUnit.DAYS);
return response;
}

Expand All @@ -53,11 +68,11 @@ public List<Role> getRoleListByUserId(@NonNull Long userId) {

@Override
public boolean checkAdmin(@NotEmpty List<@NotNull Long> roleIdList) {
final LambdaQueryWrapper<Role> wrapper = Wrappers.lambdaQuery();
val wrapper = Wrappers.lambdaQuery(Role.class);
wrapper.select(Role::getName)
.in(Role::getId, roleIdList);
val rolePersistenceList = this.list(wrapper);
val roleNameSet = rolePersistenceList
val roleList = this.list(wrapper);
val roleNameSet = roleList
.stream()
.map(Role::getName)
.filter(roleName -> StrUtil.equals(mafConfiguration.getSuperUserRole(), roleName))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package com.jmsoftware.maf.authcenter.user.entity.constant;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* Description: UserRedisKey, change description here.
*
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 6/28/2021 3:18 PM
**/
@Getter
@RequiredArgsConstructor
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;
}
private final String keyInfixFormat;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CHANGE MASTER TO
MASTER_HOST = 'maf-mysql-server-master-test',
MASTER_HOST = 'maf-mysql-server-master-development-docker',
MASTER_PORT = 3306,
MASTER_USER = 'replication_user',
MASTER_PASSWORD = 'replication_password',
Expand Down
10 changes: 5 additions & 5 deletions oss-center/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
${AnsiStyle.BOLD}${AnsiColor.BRIGHT_GREEN}
_____ __ __ _ ____ ______ __
/ ___// /_____ _/ /_(_)____ / __ \___ _________ __ _______________ / ____/__ ____ / /____ _____
\__ \/ __/ __ `/ __/ / ___/ / /_/ / _ \/ ___/ __ \/ / / / ___/ ___/ _ \ / / / _ \/ __ \/ __/ _ \/ ___/
___/ / /_/ /_/ / /_/ / /__ / _, _/ __(__ ) /_/ / /_/ / / / /__/ __/ / /___/ __/ / / / /_/ __/ /
/____/\__/\__,_/\__/_/\___/ /_/ |_|\___/____/\____/\__,_/_/ \___/\___/ \____/\___/_/ /_/\__/\___/_/
____ __________ ______ __
/ __ \/ ___/ ___/ / ____/__ ____ / /____ _____
/ / / /\__ \\__ \ / / / _ \/ __ \/ __/ _ \/ ___/
/ /_/ /___/ /__/ / / /___/ __/ / / / /_/ __/ /
\____//____/____/ \____/\___/_/ /_/\__/\___/_/
${AnsiStyle.BOLD}Static Resource Center :: Powered by Spring Boot ::${spring-boot.formatted-version}
${AnsiColor.CYAN}Author: Johnny Miller (锺俊), email: johnnysviva@outlook.com
${AnsiStyle.NORMAL}${AnsiColor.MAGENTA}http://patorjk.com/software/taag/#p=display&f=Slant&t=Static%20Resource%20Center
Expand Down

0 comments on commit e9d935b

Please sign in to comment.