Browse Source

获取项目id对应的项目编码增加缓存

tags/24080901
WendyYang 7 months ago
parent
commit
4ba06a7b42
1 changed files with 26 additions and 6 deletions
  1. +26
    -6
      hz-pm-api/src/main/java/com/hz/pm/api/projectlib/service/IProjectService.java

+ 26
- 6
hz-pm-api/src/main/java/com/hz/pm/api/projectlib/service/IProjectService.java View File

@@ -13,7 +13,9 @@ import com.hz.pm.api.projectlib.model.req.ProjectListReq;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

/**
* <p>
@@ -25,13 +27,31 @@ import java.util.Optional;
*/
public interface IProjectService extends IService<Project> {

Map<Long, String> PROJECT_CODE_MAP_ID = new ConcurrentHashMap<>();

default Optional<String> getProjectCode(Long projectId) {
LambdaQueryWrapper<Project> query = Wrappers.lambdaQuery(Project.class)
.select(Project::getProjectCode)
.eq(Project::getId, projectId)
.last(BizConst.LIMIT_1);
return Optional.ofNullable(getOne(query))
.flatMap(w -> Optional.of(w.getProjectCode()));
if (projectId == null) {
return Optional.empty();
}
String projectCode = PROJECT_CODE_MAP_ID.get(projectId);
if (projectCode == null) {
synchronized (this) {
projectCode = PROJECT_CODE_MAP_ID.get(projectId);
if (projectCode == null) {
Wrapper<Project> query = Wrappers.lambdaQuery(Project.class)
.select(Project::getProjectCode)
.eq(Project::getId, projectId)
.last(BizConst.LIMIT_1);
Project project = getOne(query);
if (project != null) {
projectCode = project.getProjectCode();
PROJECT_CODE_MAP_ID.put(projectId, projectCode);
}
}

}
}
return Optional.ofNullable(projectCode);
}

default Optional<Long> getNewProjectId(String projectCode) {


Loading…
Cancel
Save