feat:指数倍提升性能,使用缓存

This commit is contained in:
wangyu 2021-12-02 23:34:51 +08:00
parent 097c6210ed
commit 03e3a09af6
5 changed files with 84 additions and 65 deletions

View File

@ -0,0 +1,58 @@
package com.flyfish.framework.config.audit;
import com.flyfish.framework.auditor.BeanAuditor;
import com.flyfish.framework.auditor.BeanPoster;
import com.flyfish.framework.config.constants.UserCacheKeys;
import com.flyfish.framework.domain.po.User;
import com.flyfish.framework.enums.UserType;
import com.flyfish.framework.utils.Assert;
import com.flyfish.framework.utils.ReactiveRedisOperations;
import com.flyfish.framework.utils.StrengthUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public class UserBeanAuditor implements BeanAuditor<User>, BeanPoster<User> {
private final PasswordEncoder passwordEncoder;
private final ReactiveRedisOperations reactiveRedisOperations;
/**
* 对实体进行审查并补全相关字段
*
* @param entity 原数据
*/
@Override
public void audit(User entity) {
if (null == entity.getId() && StringUtils.isNotBlank(entity.getPassword())) {
Assert.isTrue(StrengthUtils.isValid(entity.getPassword()), "密码强度不够,至少应该包含数字、大小写字母、符号组合");
entity.setPassword(passwordEncoder.encode(entity.getPassword()));
}
if (null == entity.getType()) {
entity.setType(UserType.ADMIN);
}
if (null == entity.getEnable()) {
entity.setEnable(true);
}
if (null == entity.getApp()) {
entity.setApp(false);
}
if (null == entity.getCode()) {
entity.setCode(entity.getUsername());
}
}
/**
* 对入库的实体进行审查并执行额外功能
*
* @param data 原数据
*/
@Override
public void post(User data) {
// 更新缓存
reactiveRedisOperations.set(UserCacheKeys.get(data.getUsername()), data).subscribe();
}
}

View File

@ -0,0 +1,14 @@
package com.flyfish.framework.config.constants;
public interface UserCacheKeys {
/**
* 获取缓存键
*
* @param username 用户名
* @return 结果
*/
static String get(String username) {
return "user-" + username;
}
}

View File

@ -1,11 +1,13 @@
package com.flyfish.framework.service;
import com.flyfish.framework.config.constants.UserCacheKeys;
import com.flyfish.framework.domain.AdminUserDetails;
import com.flyfish.framework.domain.base.IUser;
import com.flyfish.framework.domain.po.User;
import com.flyfish.framework.enums.UserStatus;
import com.flyfish.framework.utils.Assert;
import com.flyfish.framework.utils.CopyUtils;
import com.flyfish.framework.utils.ReactiveRedisOperations;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.security.authentication.*;
@ -54,9 +56,12 @@ public class MongoUserDetailsServiceImpl implements MongoUserDetailsService {
@Resource
private ServerSecurityContextRepository contextRepository;
@Resource
private ReactiveRedisOperations reactiveRedisOperations;
private ReactiveAuthenticationManager authenticationManager;
@PostConstruct
@SuppressWarnings("deprecation")
private void init() {
@ -73,9 +78,12 @@ public class MongoUserDetailsServiceImpl implements MongoUserDetailsService {
@Override
public Mono<UserDetails> findByUsername(String s) {
return userService.findByUsername(s)
.flatMap(this::validate)
.map(this::mapToUserDetails)
String key = UserCacheKeys.get(s);
// 优先从缓存读取如果缓存不存在查询转换后放入缓存
return reactiveRedisOperations.hasKey(key).flatMap(exists -> exists ? reactiveRedisOperations.get(key) :
userService.findByUsername(s).flatMap(this::validate).map(this::mapToUserDetails)
.flatMap(detail -> reactiveRedisOperations.set(key, detail).thenReturn(detail))
)
.switchIfEmpty(Mono.defer(() -> Mono.error(new UsernameNotFoundException("用户不存在!"))));
}

View File

@ -3,12 +3,9 @@ package com.flyfish.framework.service;
import com.flyfish.framework.domain.po.User;
import com.flyfish.framework.repository.ReactiveUserRepository;
import com.flyfish.framework.service.impl.BaseReactiveServiceImpl;
import com.flyfish.framework.utils.ReactiveRedisOperations;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
import javax.annotation.Resource;
/**
* 异步用户service
*
@ -17,9 +14,6 @@ import javax.annotation.Resource;
@Service
public class ReactiveUserService extends BaseReactiveServiceImpl<User> {
@Resource
private ReactiveRedisOperations reactiveRedisOperations;
/**
* 获取用户数据
*
@ -27,19 +21,7 @@ public class ReactiveUserService extends BaseReactiveServiceImpl<User> {
* @return 结果
*/
public Mono<User> findByUsername(String username) {
String key = getCacheKey(username);
return reactiveRedisOperations.hasKey(key).flatMap(exists -> exists ? reactiveRedisOperations.get(key) :
((ReactiveUserRepository) repository).findByUsername(username));
}
/**
* 获取缓存键
*
* @param username 用户名
* @return 结果
*/
private String getCacheKey(String username) {
return "user-" + username;
return ((ReactiveUserRepository) repository).findByUsername(username);
}
}

View File

@ -1,26 +1,15 @@
package com.flyfish.framework.service;
import com.flyfish.framework.auditor.BeanAuditor;
import com.flyfish.framework.domain.po.User;
import com.flyfish.framework.enums.UserType;
import com.flyfish.framework.repository.UserRepository;
import com.flyfish.framework.service.impl.BaseServiceImpl;
import com.flyfish.framework.utils.Assert;
import com.flyfish.framework.utils.StrengthUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Optional;
@Service
public class UserService extends BaseServiceImpl<User> implements UserFindService {
@Resource
private PasswordEncoder passwordEncoder;
/**
* 获取用户数据
@ -32,36 +21,4 @@ public class UserService extends BaseServiceImpl<User> implements UserFindServic
public Optional<User> findByUsername(String username) {
return ((UserRepository) repository).findByUsername(username);
}
@Component
@RequiredArgsConstructor
public static class Auditor implements BeanAuditor<User> {
private final PasswordEncoder passwordEncoder;
/**
* 对实体进行审查并补全相关字段
*
* @param entity 原数据
*/
@Override
public void audit(User entity) {
if (null == entity.getId() && StringUtils.isNotBlank(entity.getPassword())) {
Assert.isTrue(StrengthUtils.isValid(entity.getPassword()), "密码强度不够,至少应该包含数字、大小写字母、符号组合");
entity.setPassword(passwordEncoder.encode(entity.getPassword()));
}
if (null == entity.getType()) {
entity.setType(UserType.ADMIN);
}
if (null == entity.getEnable()) {
entity.setEnable(true);
}
if (null == entity.getApp()) {
entity.setApp(false);
}
if (null == entity.getCode()) {
entity.setCode(entity.getUsername());
}
}
}
}