feat: guava缓存/登录拦截器

This commit is contained in:
2024-03-04 17:39:22 +08:00
parent c0ec4e34f8
commit 9f36b9a126
30 changed files with 503 additions and 98 deletions

View File

@@ -3,11 +3,11 @@ package com.landaiqing.subject.domain.service.impl;
import com.alibaba.fastjson.JSON;
import com.landaiqing.subject.common.enums.CategoryTypeEnum;
import com.landaiqing.subject.common.enums.IsDeletedFlagEnum;
import com.landaiqing.subject.domain.config.ThreadPoolConfig;
import com.landaiqing.subject.domain.convert.SubjectCategoryConverter;
import com.landaiqing.subject.domain.entity.SubjectCategoryBO;
import com.landaiqing.subject.domain.entity.SubjectLabelBO;
import com.landaiqing.subject.domain.service.SubjectCategoryDomainService;
import com.landaiqing.subject.domain.util.CacheUtil;
import com.landaiqing.subject.infra.basic.entity.SubjectCategory;
import com.landaiqing.subject.infra.basic.entity.SubjectLabel;
import com.landaiqing.subject.infra.basic.entity.SubjectMapping;
@@ -26,10 +26,8 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
@Slf4j
@@ -45,6 +43,10 @@ public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainSe
@Resource
private ThreadPoolExecutor labelThreadPool;
@Resource
private CacheUtil cacheUtil;
/**
* @description: 新增分类
* @param: [subjectCategoryBO]
@@ -127,9 +129,17 @@ public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainSe
@SneakyThrows
@Override
public List<SubjectCategoryBO> queryCategoryAndLabel(SubjectCategoryBO subjectCategoryBO) {
Long id = subjectCategoryBO.getId();
String cacheKey = "categoryAndLabel." + subjectCategoryBO.getId();
List<SubjectCategoryBO> subjectCategoryBOS = cacheUtil.getResult(cacheKey,
SubjectCategoryBO.class, (key) -> getSubjectCategoryBOS(id));
return subjectCategoryBOS;
}
private List<SubjectCategoryBO> getSubjectCategoryBOS(Long categoryId) {
// 查询当前分类下的所以的分类
SubjectCategory subjectCategory = new SubjectCategory();
subjectCategory.setParentId(subjectCategoryBO.getId());
subjectCategory.setParentId(categoryId);
subjectCategory.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
List<SubjectCategory> subjectCategoryList = subjectCategoryService.queryCategory(subjectCategory);
if (log.isInfoEnabled()) {
@@ -142,30 +152,18 @@ public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainSe
CompletableFuture.supplyAsync(() -> getLabelBOList(category), labelThreadPool)
).collect(Collectors.toList());
completableFutureList.forEach(future -> {
Map<Long, List<SubjectLabelBO>> resultMap = null;
try {
Map<Long, List<SubjectLabelBO>> resultMap = future.get();
map.putAll(resultMap);
} catch (Exception e) {
e.printStackTrace();
resultMap = future.get();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
map.putAll(resultMap);
});
// // 一次获取标签信息
// List<FutureTask<Map<Long, List<SubjectLabelBO>>>> futureTaskList = new LinkedList<>();
// Map<Long, List<SubjectLabelBO>> map = new HashMap<>();
// //线程池并发调用
// categoryBOList.forEach(category -> {
// FutureTask<Map<Long, List<SubjectLabelBO>>> futureTask = new FutureTask<>(() ->
// getLabelBOList(category));
// futureTaskList.add(futureTask);
// labelThreadPool.submit(futureTask);
// });
// for (FutureTask<Map<Long, List<SubjectLabelBO>>> futureTask : futureTaskList) {
// Map<Long, List<SubjectLabelBO>> resultMap = futureTask.get();
// if (CollectionUtils.isEmpty(resultMap)) {
// continue;
// }
// map.putAll(resultMap);
// }
categoryBOList.forEach(categoryBO -> {
categoryBO.setLabelBOList(map.get(categoryBO.getId()));
});

View File

@@ -0,0 +1,53 @@
package com.landaiqing.subject.domain.util;
import com.alibaba.fastjson.JSON;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
/**
* @Classname CacheUtil
* @BelongsProject: qing-yu-club
* @BelongsPackage: com.landaiqing.subject.domain.util
* @Author: landaiqing
* @CreateTime: 2024-03-03 21:31
* @Description: 缓存工具类
* @Version: 1.0
*/
@Component
public class CacheUtil<K, V> {
private Cache<String, String> localCache =
CacheBuilder.newBuilder()
.maximumSize(5000)
.expireAfterWrite(10, TimeUnit.SECONDS)
.build();
public List<V> getResult(String cacheKey, Class<V> clazz,
Function<String, List<V>> function) {
List<V> resultList = new ArrayList<>();
String content = localCache.getIfPresent(cacheKey);
if (StringUtils.isNotBlank(content)) {
resultList = JSON.parseArray(content, clazz);
} else {
resultList = function.apply(cacheKey);
if (!CollectionUtils.isEmpty(resultList)) {
localCache.put(cacheKey, JSON.toJSONString(resultList));
}
}
return resultList;
}
public Map<K, V> getMapResult(String cacheKey, Class<V> clazz,
Function<String, Map<K, V>> function) {
return new HashMap<>();
}
}