修复bug,优化对象池

This commit is contained in:
石皮幼鸟 2024-04-10 15:03:57 +08:00
parent 9027f82f4b
commit 71a01cfd87
8 changed files with 48 additions and 57 deletions

View File

@ -1,7 +1,7 @@
package com.example.catchTheLetters.controller; package com.example.catchTheLetters.controller;
import com.example.catchTheLetters.entity.LoginDto; import com.example.catchTheLetters.model.dto.LoginDto;
import com.example.catchTheLetters.entity.RegisterDto; import com.example.catchTheLetters.model.dto.RegisterDto;
import com.example.catchTheLetters.entity.User; import com.example.catchTheLetters.entity.User;
import com.example.catchTheLetters.model.vo.UserVo; import com.example.catchTheLetters.model.vo.UserVo;
import com.example.catchTheLetters.service.AuthService; import com.example.catchTheLetters.service.AuthService;
@ -11,7 +11,6 @@ import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.math3.genetics.Fitness;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
/** /**

View File

@ -1,7 +1,7 @@
package com.example.catchTheLetters.controller; package com.example.catchTheLetters.controller;
import com.example.catchTheLetters.entity.Level; 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.entity.ScoreInfo;
import com.example.catchTheLetters.utils.R; import com.example.catchTheLetters.utils.R;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;

View File

@ -1,4 +1,4 @@
package com.example.catchTheLetters.entity; package com.example.catchTheLetters.model.dto;
import lombok.Data; import lombok.Data;

View File

@ -1,4 +1,4 @@
package com.example.catchTheLetters.entity; package com.example.catchTheLetters.model.dto;
import lombok.Data; import lombok.Data;

View File

@ -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 lombok.Data;
import java.io.Serializable; import java.io.Serializable;

View File

@ -1,7 +1,7 @@
package com.example.catchTheLetters.service; package com.example.catchTheLetters.service;
import com.example.catchTheLetters.entity.LoginDto; import com.example.catchTheLetters.model.dto.LoginDto;
import com.example.catchTheLetters.entity.RegisterDto; import com.example.catchTheLetters.model.dto.RegisterDto;
import com.example.catchTheLetters.entity.User; import com.example.catchTheLetters.entity.User;
import com.example.catchTheLetters.model.vo.UserVo; import com.example.catchTheLetters.model.vo.UserVo;
import com.example.catchTheLetters.utils.R; import com.example.catchTheLetters.utils.R;

View File

@ -1,9 +1,8 @@
package com.example.catchTheLetters.service.impl; package com.example.catchTheLetters.service.impl;
import com.auth0.jwt.interfaces.Claim;
import com.example.catchTheLetters.constant.CommonConstant; import com.example.catchTheLetters.constant.CommonConstant;
import com.example.catchTheLetters.entity.LoginDto; import com.example.catchTheLetters.model.dto.LoginDto;
import com.example.catchTheLetters.entity.RegisterDto; import com.example.catchTheLetters.model.dto.RegisterDto;
import com.example.catchTheLetters.entity.User; import com.example.catchTheLetters.entity.User;
import com.example.catchTheLetters.model.vo.UserVo; import com.example.catchTheLetters.model.vo.UserVo;
import com.example.catchTheLetters.service.AuthService; import com.example.catchTheLetters.service.AuthService;
@ -19,7 +18,6 @@ import org.springframework.stereotype.Service;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@Service @Service
@ -34,20 +32,19 @@ public class AuthServiceImpl implements AuthService {
@Override @Override
public R<String> login(LoginDto loginDto) { public R<String> login(LoginDto loginDto) {
// 根据用户名查询用户信息 // 根据用户名查询用户信息
String username = loginDto.getUsername(); var username = loginDto.getUsername();
// 判断用户名是否为邮箱 // 判断用户名是否为邮箱
Pattern patternEmail = Pattern.compile(CommonConstant.EMAIL_REGEX); var patternEmail = Pattern.compile(CommonConstant.EMAIL_REGEX);
Matcher matcherEmail = patternEmail.matcher(username); var matcherEmail = patternEmail.matcher(username);
// 判断用户名是否为手机号 // 判断用户名是否为手机号
Pattern patternPhone = Pattern.compile(CommonConstant.PHONE_REGEX); var patternPhone = Pattern.compile(CommonConstant.PHONE_REGEX);
Matcher matcherPhone = patternPhone.matcher(username); var matcherPhone = patternPhone.matcher(username);
User user; User user;
if (matcherEmail.matches()){ if (matcherEmail.matches()){
user = mongoTemplate.findOne(new Query(Criteria.where(CommonConstant.EMAIL).is(username).and(CommonConstant.STATUS).ne(0)), User.class); user = mongoTemplate.findOne(new Query(Criteria.where(CommonConstant.EMAIL).is(username).and(CommonConstant.STATUS).ne(0)), User.class);
} }else if (matcherPhone.matches()){
if (matcherPhone.matches()){
user = mongoTemplate.findOne(new Query(Criteria.where(CommonConstant.PHONE).is(username).and(CommonConstant.STATUS).ne(0)), User.class); user = mongoTemplate.findOne(new Query(Criteria.where(CommonConstant.PHONE).is(username).and(CommonConstant.STATUS).ne(0)), User.class);
}else { }else {
user = mongoTemplate.findOne(new Query(Criteria.where(CommonConstant.USERNAME).is(username).and(CommonConstant.STATUS).ne(0)),User.class); 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<String, String> map = new HashMap<>();
map.put("id",user.getId().toString()); 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); redisService.hSet(token, user.getId().toString(), CommonConstant.REDIS_EXPIRE_TIME);
return R.ok(token); return R.ok(token);
} }
@ -72,9 +69,12 @@ public class AuthServiceImpl implements AuthService {
@Override @Override
public User verify(String token) { public User verify(String token) {
Map<String, Claim> map = JwtUtil.getPayload(token); var map = JwtUtil.getPayload(token);
String id = map.get("id").asString().replaceAll("\"", ""); String id = null;
User user = mongoTemplate.findOne(new Query(Criteria.where("id").is(id).and(CommonConstant.STATUS).ne(0)),User.class); 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)){ if (Objects.isNull(user)){
return null; return null;
} }
@ -83,19 +83,17 @@ public class AuthServiceImpl implements AuthService {
@Override @Override
public R<UserVo> register(RegisterDto registerDto) { public R<UserVo> register(RegisterDto registerDto) {
User regedUser = getUserByName(registerDto.getUsername()); var regedUser = getUserByName(registerDto.getUsername());
R<UserVo> result = new R<>();
// 用户名重复 // 用户名重复
if (regedUser != null) { if (regedUser != null) {
R.fail("用户名重复"); R.fail("用户名重复");
} }
User user = new User(); var user = new User();
user.setUsername(registerDto.getUsername()); user.setUsername(registerDto.getUsername());
user.setPassword(registerDto.getPassword()); user.setPassword(registerDto.getPassword());
User insert = mongoTemplate.insert(user); var insert = mongoTemplate.insert(user);
if (insert != null && insert.getId() != null){ if (insert.getId() != null){
return R.ok(user.toVo()); return R.ok(user.toVo());
}else { }else {
return R.fail("注册失败,请重试"); return R.fail("注册失败,请重试");

View File

@ -10,6 +10,7 @@ import java.util.function.Supplier;
/** /**
* 对象池防止频繁创建对象触发GC * 对象池防止频繁创建对象触发GC
*
* @param <T> 对象类型 * @param <T> 对象类型
* @author spyn * @author spyn
*/ */
@ -37,11 +38,11 @@ public class ObjectPool<T> implements Iterable<T> {
private Integer limit; private Integer limit;
/** /**
* 是否阻塞 * 是否需要上限
*/ */
@Getter @Getter
@Setter @Setter
private Boolean block; private Boolean needLimit;
/** /**
* 当前对象数 * 当前对象数
@ -50,16 +51,17 @@ public class ObjectPool<T> implements Iterable<T> {
/** /**
* 构造函数 * 构造函数
*
* @param factory 创建对象委托 * @param factory 创建对象委托
* @param reset 重置对象委托 * @param reset 重置对象委托
* @param block 是否阻塞 * @param needLimit 是否需要上限
* @param limit 对象池上限 * @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.pool = new ConcurrentLinkedDeque<>();
this.factory = factory; this.factory = factory;
this.reset = reset; this.reset = reset;
this.block = block; this.needLimit = needLimit;
this.limit = limit; this.limit = limit;
// 预先创建对象 // 预先创建对象
for (int i = 0; i < limit; i++) { for (int i = 0; i < limit; i++) {
@ -70,6 +72,7 @@ public class ObjectPool<T> implements Iterable<T> {
/** /**
* 构造函数 * 构造函数
*
* @param factory 创建对象委托 * @param factory 创建对象委托
* @param reset 重置对象委托 * @param reset 重置对象委托
*/ */
@ -79,28 +82,20 @@ public class ObjectPool<T> implements Iterable<T> {
/** /**
* 取出一个对象 * 取出一个对象
*
* @return 对象 * @return 对象
*/ */
public T borrowObject() throws InterruptedException { public T borrowObject() throws InterruptedException {
if (!pool.isEmpty()) return pool.pop(); if (!pool.isEmpty()) return pool.pop();
// 如果对象池为空且此时没到上限创建新对象否则查看是否阻塞若不阻塞则返回null否则等待 // 如果对象池为空且此时没到上限创建新对象否则如果需要上限约束的情况下返回null
if (count < limit) { if (needLimit && count >= limit) return null;
count++; count++;
return factory.get(); return factory.get();
} }
if (!block) {
return null;
}
synchronized (this) {
while (pool.isEmpty()) {
this.wait();
}
}
return pool.pop();
}
/** /**
* 存入一个对象 * 存入一个对象
*
* @param object 对象 * @param object 对象
*/ */
public void returnObject(T object) { public void returnObject(T object) {
@ -109,10 +104,7 @@ public class ObjectPool<T> implements Iterable<T> {
} }
// 重置对象 // 重置对象
reset.accept(object); reset.accept(object);
synchronized (this) {
this.pool.push(object); this.pool.push(object);
this.notify();
}
} }
/** /**
@ -124,6 +116,7 @@ public class ObjectPool<T> implements Iterable<T> {
/** /**
* 覆写迭代器便于遍历对象池 * 覆写迭代器便于遍历对象池
*
* @return 迭代器 * @return 迭代器
*/ */
@NotNull @NotNull