feat:指数倍提升性能,使用缓存
This commit is contained in:
parent
097c6210ed
commit
03e3a09af6
@ -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();
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.flyfish.framework.config.constants;
|
||||
|
||||
public interface UserCacheKeys {
|
||||
|
||||
/**
|
||||
* 获取缓存键
|
||||
*
|
||||
* @param username 用户名
|
||||
* @return 结果
|
||||
*/
|
||||
static String get(String username) {
|
||||
return "user-" + username;
|
||||
}
|
||||
}
|
@ -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("用户不存在!"))));
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user