修复bug,优化对象池
This commit is contained in:
parent
9027f82f4b
commit
71a01cfd87
|
@ -1,7 +1,7 @@
|
|||
package com.example.catchTheLetters.controller;
|
||||
|
||||
import com.example.catchTheLetters.entity.LoginDto;
|
||||
import com.example.catchTheLetters.entity.RegisterDto;
|
||||
import com.example.catchTheLetters.model.dto.LoginDto;
|
||||
import com.example.catchTheLetters.model.dto.RegisterDto;
|
||||
import com.example.catchTheLetters.entity.User;
|
||||
import com.example.catchTheLetters.model.vo.UserVo;
|
||||
import com.example.catchTheLetters.service.AuthService;
|
||||
|
@ -11,7 +11,6 @@ import io.swagger.annotations.ApiOperation;
|
|||
import io.swagger.annotations.ApiParam;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.math3.genetics.Fitness;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package com.example.catchTheLetters.controller;
|
||||
|
||||
import com.example.catchTheLetters.entity.Level;
|
||||
import com.example.catchTheLetters.entity.RankVo;
|
||||
import com.example.catchTheLetters.model.vo.RankVo;
|
||||
import com.example.catchTheLetters.entity.ScoreInfo;
|
||||
import com.example.catchTheLetters.utils.R;
|
||||
import io.swagger.annotations.Api;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.example.catchTheLetters.entity;
|
||||
package com.example.catchTheLetters.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.example.catchTheLetters.entity;
|
||||
package com.example.catchTheLetters.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
package com.example.catchTheLetters.entity;
|
||||
package com.example.catchTheLetters.model.vo;
|
||||
|
||||
import com.example.catchTheLetters.entity.ScoreInfo;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
|
@ -1,7 +1,7 @@
|
|||
package com.example.catchTheLetters.service;
|
||||
|
||||
import com.example.catchTheLetters.entity.LoginDto;
|
||||
import com.example.catchTheLetters.entity.RegisterDto;
|
||||
import com.example.catchTheLetters.model.dto.LoginDto;
|
||||
import com.example.catchTheLetters.model.dto.RegisterDto;
|
||||
import com.example.catchTheLetters.entity.User;
|
||||
import com.example.catchTheLetters.model.vo.UserVo;
|
||||
import com.example.catchTheLetters.utils.R;
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
package com.example.catchTheLetters.service.impl;
|
||||
|
||||
import com.auth0.jwt.interfaces.Claim;
|
||||
import com.example.catchTheLetters.constant.CommonConstant;
|
||||
import com.example.catchTheLetters.entity.LoginDto;
|
||||
import com.example.catchTheLetters.entity.RegisterDto;
|
||||
import com.example.catchTheLetters.model.dto.LoginDto;
|
||||
import com.example.catchTheLetters.model.dto.RegisterDto;
|
||||
import com.example.catchTheLetters.entity.User;
|
||||
import com.example.catchTheLetters.model.vo.UserVo;
|
||||
import com.example.catchTheLetters.service.AuthService;
|
||||
|
@ -19,7 +18,6 @@ import org.springframework.stereotype.Service;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Service
|
||||
|
@ -34,20 +32,19 @@ public class AuthServiceImpl implements AuthService {
|
|||
@Override
|
||||
public R<String> login(LoginDto loginDto) {
|
||||
// 根据用户名查询用户信息
|
||||
String username = loginDto.getUsername();
|
||||
var username = loginDto.getUsername();
|
||||
|
||||
// 判断用户名是否为邮箱
|
||||
Pattern patternEmail = Pattern.compile(CommonConstant.EMAIL_REGEX);
|
||||
Matcher matcherEmail = patternEmail.matcher(username);
|
||||
var patternEmail = Pattern.compile(CommonConstant.EMAIL_REGEX);
|
||||
var matcherEmail = patternEmail.matcher(username);
|
||||
|
||||
// 判断用户名是否为手机号
|
||||
Pattern patternPhone = Pattern.compile(CommonConstant.PHONE_REGEX);
|
||||
Matcher matcherPhone = patternPhone.matcher(username);
|
||||
var patternPhone = Pattern.compile(CommonConstant.PHONE_REGEX);
|
||||
var matcherPhone = patternPhone.matcher(username);
|
||||
User user;
|
||||
if (matcherEmail.matches()){
|
||||
user = mongoTemplate.findOne(new Query(Criteria.where(CommonConstant.EMAIL).is(username).and(CommonConstant.STATUS).ne(0)), User.class);
|
||||
}
|
||||
if (matcherPhone.matches()){
|
||||
}else if (matcherPhone.matches()){
|
||||
user = mongoTemplate.findOne(new Query(Criteria.where(CommonConstant.PHONE).is(username).and(CommonConstant.STATUS).ne(0)), User.class);
|
||||
}else {
|
||||
user = mongoTemplate.findOne(new Query(Criteria.where(CommonConstant.USERNAME).is(username).and(CommonConstant.STATUS).ne(0)),User.class);
|
||||
|
@ -60,7 +57,7 @@ public class AuthServiceImpl implements AuthService {
|
|||
}
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("id",user.getId().toString());
|
||||
String token = JwtUtil.getToken(map, CommonConstant.TOKEN_EXPIRE_TIME);
|
||||
var token = JwtUtil.getToken(map, CommonConstant.TOKEN_EXPIRE_TIME);
|
||||
redisService.hSet(token, user.getId().toString(), CommonConstant.REDIS_EXPIRE_TIME);
|
||||
return R.ok(token);
|
||||
}
|
||||
|
@ -72,9 +69,12 @@ public class AuthServiceImpl implements AuthService {
|
|||
|
||||
@Override
|
||||
public User verify(String token) {
|
||||
Map<String, Claim> map = JwtUtil.getPayload(token);
|
||||
String id = map.get("id").asString().replaceAll("\"", "");
|
||||
User user = mongoTemplate.findOne(new Query(Criteria.where("id").is(id).and(CommonConstant.STATUS).ne(0)),User.class);
|
||||
var map = JwtUtil.getPayload(token);
|
||||
String id = null;
|
||||
if (map != null) {
|
||||
id = map.get("id").asString().replaceAll("\"", "");
|
||||
}
|
||||
var user = mongoTemplate.findOne(new Query(Criteria.where("id").is(id).and(CommonConstant.STATUS).ne(0)),User.class);
|
||||
if (Objects.isNull(user)){
|
||||
return null;
|
||||
}
|
||||
|
@ -83,19 +83,17 @@ public class AuthServiceImpl implements AuthService {
|
|||
|
||||
@Override
|
||||
public R<UserVo> register(RegisterDto registerDto) {
|
||||
User regedUser = getUserByName(registerDto.getUsername());
|
||||
|
||||
R<UserVo> result = new R<>();
|
||||
var regedUser = getUserByName(registerDto.getUsername());
|
||||
|
||||
// 用户名重复
|
||||
if (regedUser != null) {
|
||||
R.fail("用户名重复");
|
||||
}
|
||||
User user = new User();
|
||||
var user = new User();
|
||||
user.setUsername(registerDto.getUsername());
|
||||
user.setPassword(registerDto.getPassword());
|
||||
User insert = mongoTemplate.insert(user);
|
||||
if (insert != null && insert.getId() != null){
|
||||
var insert = mongoTemplate.insert(user);
|
||||
if (insert.getId() != null){
|
||||
return R.ok(user.toVo());
|
||||
}else {
|
||||
return R.fail("注册失败,请重试");
|
||||
|
|
|
@ -10,6 +10,7 @@ import java.util.function.Supplier;
|
|||
|
||||
/**
|
||||
* 对象池(防止频繁创建对象触发GC)
|
||||
*
|
||||
* @param <T> 对象类型
|
||||
* @author spyn
|
||||
*/
|
||||
|
@ -37,11 +38,11 @@ public class ObjectPool<T> implements Iterable<T> {
|
|||
private Integer limit;
|
||||
|
||||
/**
|
||||
* 是否阻塞
|
||||
* 是否需要上限
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
private Boolean block;
|
||||
private Boolean needLimit;
|
||||
|
||||
/**
|
||||
* 当前对象数
|
||||
|
@ -50,16 +51,17 @@ public class ObjectPool<T> implements Iterable<T> {
|
|||
|
||||
/**
|
||||
* 构造函数
|
||||
* @param factory 创建对象委托
|
||||
* @param reset 重置对象委托
|
||||
* @param block 是否阻塞
|
||||
* @param limit 对象池上限
|
||||
*
|
||||
* @param factory 创建对象委托
|
||||
* @param reset 重置对象委托
|
||||
* @param needLimit 是否需要上限
|
||||
* @param limit 对象池上限
|
||||
*/
|
||||
public ObjectPool(Supplier<T> factory, Consumer<T> reset, Boolean block, Integer limit) {
|
||||
public ObjectPool(Supplier<T> factory, Consumer<T> reset, Boolean needLimit, Integer limit) {
|
||||
this.pool = new ConcurrentLinkedDeque<>();
|
||||
this.factory = factory;
|
||||
this.reset = reset;
|
||||
this.block = block;
|
||||
this.needLimit = needLimit;
|
||||
this.limit = limit;
|
||||
// 预先创建对象
|
||||
for (int i = 0; i < limit; i++) {
|
||||
|
@ -70,8 +72,9 @@ public class ObjectPool<T> implements Iterable<T> {
|
|||
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @param factory 创建对象委托
|
||||
* @param reset 重置对象委托
|
||||
* @param reset 重置对象委托
|
||||
*/
|
||||
public ObjectPool(Supplier<T> factory, Consumer<T> reset) {
|
||||
this(factory, reset, false, 10);
|
||||
|
@ -79,28 +82,20 @@ public class ObjectPool<T> implements Iterable<T> {
|
|||
|
||||
/**
|
||||
* 取出一个对象
|
||||
*
|
||||
* @return 对象
|
||||
*/
|
||||
public T borrowObject() throws InterruptedException {
|
||||
if (!pool.isEmpty()) return pool.pop();
|
||||
// 如果对象池为空,且此时没到上限,创建新对象,否则查看是否阻塞,若不阻塞,则返回null,否则等待
|
||||
if (count < limit) {
|
||||
count++;
|
||||
return factory.get();
|
||||
}
|
||||
if (!block) {
|
||||
return null;
|
||||
}
|
||||
synchronized (this) {
|
||||
while (pool.isEmpty()) {
|
||||
this.wait();
|
||||
}
|
||||
}
|
||||
return pool.pop();
|
||||
// 如果对象池为空,且此时没到上限,创建新对象,否则如果需要上限约束的情况下返回null
|
||||
if (needLimit && count >= limit) return null;
|
||||
count++;
|
||||
return factory.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 存入一个对象
|
||||
*
|
||||
* @param object 对象
|
||||
*/
|
||||
public void returnObject(T object) {
|
||||
|
@ -109,10 +104,7 @@ public class ObjectPool<T> implements Iterable<T> {
|
|||
}
|
||||
// 重置对象
|
||||
reset.accept(object);
|
||||
synchronized (this) {
|
||||
this.pool.push(object);
|
||||
this.notify();
|
||||
}
|
||||
this.pool.push(object);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -124,6 +116,7 @@ public class ObjectPool<T> implements Iterable<T> {
|
|||
|
||||
/**
|
||||
* 覆写迭代器,便于遍历对象池
|
||||
*
|
||||
* @return 迭代器
|
||||
*/
|
||||
@NotNull
|
||||
|
|
Loading…
Reference in New Issue