feat: guava缓存/登录拦截器
This commit is contained in:
@@ -3,10 +3,12 @@ package com.landaiqing.subject.application.config;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.landaiqing.subject.application.interceptor.LoginInterceptor;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
|
||||
|
||||
import java.util.List;
|
||||
@@ -34,4 +36,10 @@ public class GlobalConfig extends WebMvcConfigurationSupport {
|
||||
MappingJackson2HttpMessageConverter converter=new MappingJackson2HttpMessageConverter(objectMapper);
|
||||
return converter;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(new LoginInterceptor())
|
||||
.addPathPatterns("/**");
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,44 @@
|
||||
package com.landaiqing.subject.application.context;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
/**
|
||||
* @Classname LoginContextHolder
|
||||
* @BelongsProject: qing-yu-club
|
||||
* @BelongsPackage: com.landaiqing.subject.application.context
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-03-03 18:11
|
||||
* @Description: 登录上下文对象
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class LoginContextHolder {
|
||||
private static final InheritableThreadLocal <Map<String,Object>> THREAD_LOCAL=new InheritableThreadLocal<>();
|
||||
|
||||
public static void set(String key,Object val){
|
||||
Map<String,Object> map=getThreadLoacalMap();
|
||||
map.put(key, val);
|
||||
}
|
||||
public static Object get(String key){
|
||||
Map<String,Object> threadLoacalMap=getThreadLoacalMap();
|
||||
return threadLoacalMap.get(key);
|
||||
}
|
||||
public static void remove(){
|
||||
THREAD_LOCAL.remove();
|
||||
}
|
||||
|
||||
public static String getLoginId(){
|
||||
return (String) getThreadLoacalMap().get("loginId");
|
||||
}
|
||||
public static Map<String,Object> getThreadLoacalMap(){
|
||||
Map<String,Object> map =THREAD_LOCAL.get();
|
||||
if (Objects.isNull(map)){
|
||||
map=new ConcurrentHashMap<>();
|
||||
THREAD_LOCAL.set(map);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
package com.landaiqing.subject.application.controller;
|
||||
|
||||
import com.landaiqing.subject.infra.entity.UserInfo;
|
||||
import com.landaiqing.subject.infra.rpc.UserRpc;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 刷题分类controller
|
||||
*
|
||||
* @author: landaiqing
|
||||
* @date: 2024/3/3
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/subject/category")
|
||||
@Slf4j
|
||||
public class TestFeignController {
|
||||
|
||||
@Resource
|
||||
private UserRpc userRpc;
|
||||
|
||||
@GetMapping("testFeign")
|
||||
public void testFeign() {
|
||||
UserInfo userInfo = userRpc.getUserInfo("jichi");
|
||||
log.info("testFeign.userInfo:{}", userInfo);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
package com.landaiqing.subject.application.interceptor;
|
||||
|
||||
import feign.RequestInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @Classname FeignConfiguration
|
||||
* @BelongsProject: qing-yu-club
|
||||
* @BelongsPackage: com.landaiqing.subject.application.interceptor
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-03-03 21:11
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Configuration
|
||||
public class FeignConfiguration {
|
||||
@Bean
|
||||
public RequestInterceptor requestInterceptor(){
|
||||
return new FeignRequestInterceptor();
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
package com.landaiqing.subject.application.interceptor;
|
||||
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestAttributes;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @Classname FeignRequestInterceptor
|
||||
* @BelongsProject: qing-yu-club
|
||||
* @BelongsPackage: com.landaiqing.subject.application.interceptor
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-03-03 21:04
|
||||
* @Description: Feign请求拦截器
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Component
|
||||
public class FeignRequestInterceptor implements RequestInterceptor {
|
||||
|
||||
@Override
|
||||
public void apply(RequestTemplate requestTemplate) {
|
||||
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
HttpServletRequest request = requestAttributes.getRequest();
|
||||
if (Objects.nonNull(request)) {
|
||||
String loginId = request.getHeader("loginId");
|
||||
if (StringUtils.isNotBlank(loginId)) {
|
||||
requestTemplate.header("loginId", loginId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,33 @@
|
||||
package com.landaiqing.subject.application.interceptor;
|
||||
|
||||
import com.landaiqing.subject.application.context.LoginContextHolder;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.Enumeration;
|
||||
|
||||
/**
|
||||
* @Classname LoginInterceptor
|
||||
* @BelongsProject: qing-yu-club
|
||||
* @BelongsPackage: com.landaiqing.subject.application.interceptor
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-03-03 18:05
|
||||
* @Description: 登录拦截器
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class LoginInterceptor implements HandlerInterceptor {
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
String loginId = request.getHeader("loginId");
|
||||
LoginContextHolder.set("loginId",loginId);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
|
||||
LoginContextHolder.remove();
|
||||
}
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
package com.landaiqing.subject.application.util;
|
||||
|
||||
import com.landaiqing.subject.application.context.LoginContextHolder;
|
||||
|
||||
/**
|
||||
* @Classname LoginUtil
|
||||
* @BelongsProject: qing-yu-club
|
||||
* @BelongsPackage: com.landaiqing.subject.application.util
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-03-03 18:33
|
||||
* @Description: 用户登录util
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
public class LoginUtil {
|
||||
public static String getLoginId() {
|
||||
return LoginContextHolder.getLoginId();
|
||||
}
|
||||
}
|
@@ -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()));
|
||||
});
|
||||
|
@@ -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<>();
|
||||
}
|
||||
}
|
@@ -51,5 +51,10 @@
|
||||
<artifactId>qing-yu-club-common</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.landaiqing</groupId>
|
||||
<artifactId>qing-yu-club-auth-api</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@@ -0,0 +1,19 @@
|
||||
package com.landaiqing.subject.infra.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @Classname UserInfo
|
||||
* @BelongsProject: qing-yu-club
|
||||
* @BelongsPackage: com.landaiqing.subject.infra.entity
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-03-03 20:13
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Data
|
||||
public class UserInfo {
|
||||
private String userName;
|
||||
|
||||
private String nickName;
|
||||
}
|
@@ -0,0 +1,39 @@
|
||||
package com.landaiqing.subject.infra.rpc;
|
||||
|
||||
import com.landaiqing.auth.api.UserFeignService;
|
||||
import com.landaiqing.auth.entity.AuthUserDTO;
|
||||
import com.landaiqing.auth.entity.Result;
|
||||
import com.landaiqing.subject.infra.entity.UserInfo;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* @Classname UserRpc
|
||||
* @BelongsProject: qing-yu-club
|
||||
* @BelongsPackage: com.landaiqing.subject.infra.rpc
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-03-03 20:11
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Component
|
||||
public class UserRpc {
|
||||
@Resource
|
||||
private UserFeignService userFeignService;
|
||||
|
||||
public UserInfo getUserInfo(String userName) {
|
||||
AuthUserDTO authUserDTO = new AuthUserDTO();
|
||||
authUserDTO.setUserName(userName);
|
||||
Result<AuthUserDTO> result = userFeignService.getUserInfo(authUserDTO);
|
||||
UserInfo userInfo = new UserInfo();
|
||||
if (!result.getSuccess()) {
|
||||
return userInfo;
|
||||
|
||||
}
|
||||
AuthUserDTO data = result.getData();
|
||||
userInfo.setUserName(data.getUserName());
|
||||
userInfo.setNickName(data.getNickName());
|
||||
return userInfo;
|
||||
}
|
||||
}
|
@@ -3,6 +3,7 @@ package com.landaiqing.subject;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
/**
|
||||
@@ -14,6 +15,7 @@ import org.springframework.context.annotation.ComponentScan;
|
||||
@SpringBootApplication
|
||||
@ComponentScan("com.landaiqing")
|
||||
@MapperScan("com.landaiqing.**.mapper")
|
||||
@EnableFeignClients(basePackages = "com.landaiqing")
|
||||
public class SubjectApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SubjectApplication.class);
|
||||
|
Reference in New Issue
Block a user