reload自动注册优化

This commit is contained in:
yandanyang 2021-09-25 21:53:21 +08:00
parent eee13c36ea
commit 5153558326
57 changed files with 935 additions and 996 deletions

View File

@ -4,7 +4,7 @@ package net.lab1024.smartadmin.service.common.constant;
* @author 罗伊
* @date 2021-01-31 0:00
*/
public class CacheModuleBaseConst {
public class CacheModuleConst {
public static class Employee {
/**

View File

@ -13,7 +13,11 @@ import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 认证失败处理
*
* [ 认证失败处理 ]
*
* @author
* @date
*/
public class SmartSecurityAuthenticationFailHandler implements AuthenticationEntryPoint {

View File

@ -33,17 +33,22 @@ public class SmartSecurityMetadataSource extends PrePostAnnotationSecurityMetada
private final PrePostInvocationAttributeFactory attributeFactory;
private List<String> noValidUrlList;
private SmartSecurityUrlMatchers smartSecurityUrlMatchers;
public SmartSecurityMetadataSource(PrePostInvocationAttributeFactory attributeFactory, List<String> noValidUrlList) {
public SmartSecurityMetadataSource(PrePostInvocationAttributeFactory attributeFactory, SmartSecurityUrlMatchers smartSecurityUrlMatchers) {
super(attributeFactory);
this.attributeFactory = attributeFactory;
this.noValidUrlList = noValidUrlList;
this.smartSecurityUrlMatchers = smartSecurityUrlMatchers;
}
@Override
public Collection<ConfigAttribute> getAttributes(Method method, Class<?> targetClass) {
//只对固定的包的所有接口进行控制
if (!targetClass.getName().startsWith(smartSecurityUrlMatchers.getValidPackage())) {
return super.getAttributes(method, targetClass);
}
//自己的控制
GetMapping getMapping = method.getAnnotation(GetMapping.class);
PostMapping postMapping = method.getAnnotation(PostMapping.class);
@ -77,6 +82,8 @@ public class SmartSecurityMetadataSource extends PrePostAnnotationSecurityMetada
AntPathMatcher antPathMatcher = new AntPathMatcher();
antPathMatcher.setCaseSensitive(false);
antPathMatcher.setTrimTokens(true);
//无需验证的URL集合
List<String> noValidUrlList = smartSecurityUrlMatchers.getNoValidUrl();
if (this.contain(antPathMatcher, noValidUrlList, annotationValueList)) {
return super.getAttributes(method, targetClass);
}

View File

@ -1,20 +1,18 @@
package net.lab1024.smartadmin.service.common.security;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.smartadmin.service.common.anno.NoNeedLogin;
import net.lab1024.smartadmin.service.common.constant.CommonConst;
import org.apache.commons.collections4.CollectionUtils;
import org.reflections.Reflections;
import org.reflections.scanners.MethodAnnotationsScanner;
import org.reflections.scanners.TypeAnnotationsScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@ -24,67 +22,85 @@ import java.util.Set;
* @author 罗伊
* @date 2021/8/31 10:20
*/
public class SmartSecurityUrlMatchers implements BeanPostProcessor {
@Slf4j
@Component
public class SmartSecurityUrlMatchers {
@Value("${project.module}")
private String scanPackage;
/**
* 匿名访问URL
*/
private List<String> ANONYMOUS_URL;
private List<String> anonymousUrl = Lists.newArrayList();
/**
* 忽略的URL(注意加入忽略的URL无法进入Security filter)
*/
private static List<String> IGNORE_URL;
private List<String> ignoreUrl = Lists.newArrayList();
/**
* 需要登录的
*/
private static List<String> AUTHENTICATED_URL;
static {
IGNORE_URL = new ArrayList<>();
IGNORE_URL.add("/swagger-ui.html");
IGNORE_URL.add("/swagger-resources/**");
IGNORE_URL.add("/webjars/**");
IGNORE_URL.add("/*/api-docs");
IGNORE_URL.add(CommonConst.ApiUrl.API_PREFIX_SUPPORT + "/**");
AUTHENTICATED_URL = new ArrayList<>();
AUTHENTICATED_URL.add("/admin/**");
}
/**
* 构造函数
*
*/
public SmartSecurityUrlMatchers() {
}
private List<String> authenticatedUrl = Lists.newArrayList();
/**
* 获取忽略的URL集合
*
* @return
*/
public List<String> getIgnoreUrlList() {
return IGNORE_URL;
public List<String> getIgnoreUrl() {
if (CollectionUtils.isNotEmpty(ignoreUrl)) {
return ignoreUrl;
}
ignoreUrl.add("/swagger-ui.html");
ignoreUrl.add("/swagger-resources/**");
ignoreUrl.add("/webjars/**");
ignoreUrl.add("/*/api-docs");
ignoreUrl.add(CommonConst.ApiUrl.API_PREFIX_SUPPORT + "/**");
log.info("忽略URL{}",ignoreUrl);
return ignoreUrl;
}
/**
* 获取需要匿名访问的url集合
*
* @return
*/
public List<String> getAnonymousUrlList() {
return ANONYMOUS_URL;
}
/**
* 获取需要认证的url集合
* 需要登录认证的URL集合
*
* @return
*/
public List<String> getAuthenticatedUrlList() {
return AUTHENTICATED_URL;
if (CollectionUtils.isNotEmpty(authenticatedUrl)) {
return authenticatedUrl;
}
authenticatedUrl.add("/admin/**");
log.info("认证URL{}",authenticatedUrl);
return authenticatedUrl;
}
/**
* 获取无需登录可以匿名访问的url信息
*
* @return
*/
private List<String> getAnonymousUrl() {
if (CollectionUtils.isNotEmpty(anonymousUrl)) {
return anonymousUrl;
}
Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage(scanPackage)).setScanners(new MethodAnnotationsScanner()));
Set<Method> methodSet = reflections.getMethodsAnnotatedWith(NoNeedLogin.class);
for (Method method : methodSet) {
String uriPrefix = SmartSecurityUrl.getUriPrefix(method);
List<String> valueList = SmartSecurityUrl.getAnnotationValueList(method, uriPrefix);
anonymousUrl.addAll(valueList);
}
log.info("匿名URL{}",anonymousUrl);
return anonymousUrl;
}
/**
* 获取需要校验的包路径
* @return
*/
public String getValidPackage() {
return scanPackage;
}
/**
@ -92,10 +108,10 @@ public class SmartSecurityUrlMatchers implements BeanPostProcessor {
*
* @return
*/
public List<String> getNoValidUrlList() {
public List<String> getNoValidUrl() {
List<String> noValidUrl = Lists.newArrayList();
noValidUrl.addAll(IGNORE_URL);
noValidUrl.addAll(ANONYMOUS_URL);
noValidUrl.addAll(this.getIgnoreUrl());
noValidUrl.addAll(this.getAnonymousUrl());
return noValidUrl;
}
@ -105,7 +121,8 @@ public class SmartSecurityUrlMatchers implements BeanPostProcessor {
* @return
*/
public String[] getIgnoreUrlArray() {
String[] ignoreUrlArray = IGNORE_URL.toArray(new String[IGNORE_URL.size()]);
List<String> ignoreUrl = this.getIgnoreUrl();
String[] ignoreUrlArray = ignoreUrl.toArray(new String[ignoreUrl.size()]);
return ignoreUrlArray;
}
@ -115,7 +132,8 @@ public class SmartSecurityUrlMatchers implements BeanPostProcessor {
* @return
*/
public String[] getAnonymousUrlArray() {
String[] anonymousUrlArray = ANONYMOUS_URL.toArray(new String[ANONYMOUS_URL.size()]);
List<String> anonymousUrl = this.getAnonymousUrl();
String[] anonymousUrlArray = anonymousUrl.toArray(new String[anonymousUrl.size()]);
return anonymousUrlArray;
}
@ -125,28 +143,9 @@ public class SmartSecurityUrlMatchers implements BeanPostProcessor {
* @return
*/
public String[] getAuthenticatedUrlArray() {
String[] anonymousUrlArray = AUTHENTICATED_URL.toArray(new String[AUTHENTICATED_URL.size()]);
return anonymousUrlArray;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
Method[] methods = ReflectionUtils.getAllDeclaredMethods(bean.getClass());
if (methods == null) {
return bean;
}
//方法级别无需登录
for (Method method : methods) {
NoNeedLogin noNeedLogin = method.getAnnotation(NoNeedLogin.class);
if(noNeedLogin != null){
String uriPrefix = SmartSecurityUrl.getUriPrefix(method);
List<String> valueList = SmartSecurityUrl.getAnnotationValueList(method, uriPrefix);
this.ANONYMOUS_URL.addAll(valueList);
}
}
return bean;
List<String> authenticatedUrl = this.getAuthenticatedUrlList();
String[] authenticatedUrlArray = authenticatedUrl.toArray(new String[authenticatedUrl.size()]);
return authenticatedUrlArray;
}
}

View File

@ -5,11 +5,13 @@ import com.google.common.collect.Multimap;
import io.swagger.models.Operation;
import io.swagger.models.Path;
import net.lab1024.smartadmin.service.config.SystemEnvironmentConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.service.ApiDescription;
import springfox.documentation.service.ApiListing;
import springfox.documentation.swagger2.mappers.ModelMapper;
import springfox.documentation.swagger2.mappers.ServiceModelToSwagger2MapperImpl;
import java.lang.reflect.Field;
@ -24,7 +26,7 @@ import static springfox.documentation.builders.BuilderDefaults.nullToEmptyList;
* @author Turbolisten
* @date 2021/8/11 16:05
*/
@Conditional(SystemEnvironmentConfig.class)
@ConditionalOnBean(ModelMapper.class)
@Component
@Primary
public class Swagger2MapperImplExtension extends ServiceModelToSwagger2MapperImpl {

View File

@ -3,6 +3,7 @@ package net.lab1024.smartadmin.service.config;
import com.github.benmanes.caffeine.cache.LoadingCache;
import net.lab1024.smartadmin.service.module.support.beancache.cache.AbstractCaffeineCache;
import net.lab1024.smartadmin.service.module.support.beancache.cache.AbstractDisableCache;
import net.lab1024.smartadmin.service.module.support.beancache.cache.CacheLoadMethodRegister;
import net.lab1024.smartadmin.service.module.support.beancache.cache.IBeanCache;
import net.lab1024.smartadmin.service.module.support.beancache.domain.CacheData;
import org.springframework.beans.factory.annotation.Value;
@ -17,33 +18,44 @@ import org.springframework.context.annotation.Primary;
* @author zhuoda
*/
@Configuration
public class CaffeineCacheConfig {
public class CacheConfig {
@Value("${cache.maximumSize:5000}")
private Integer cacheMaximumSize;
@Value("${cache.expireDays:5}")
private Integer expireDays;
@Value("${cache.scanPath}")
private String scanPath;
@Bean
public CacheLoadMethodRegister methodRegister(){
return new CacheLoadMethodRegister();
}
@Bean
@Primary
public IBeanCache beanCache() {
public IBeanCache beanCache(CacheLoadMethodRegister methodRegister) {
return new AbstractCaffeineCache() {
LoadingCache<String, CacheData> cache = this.initCache(cacheMaximumSize, expireDays, scanPath);
LoadingCache<String, CacheData> cache = this.initCache(cacheMaximumSize, expireDays);
@Override
public LoadingCache getCache() {
return cache;
}
@Override
public CacheLoadMethodRegister methodRegister() {
return methodRegister;
}
};
}
@Bean
@ConditionalOnMissingBean(IBeanCache.class)
public IBeanCache beanDisableCache() {
return new AbstractDisableCache(scanPath);
public IBeanCache beanDisableCache(CacheLoadMethodRegister methodRegister) {
return new AbstractDisableCache() {
@Override
public CacheLoadMethodRegister methodRegister() {
return methodRegister;
}
};
}
}

View File

@ -20,7 +20,7 @@ import org.springframework.context.annotation.Configuration;
@Data
@Configuration
@ConditionalOnProperty(prefix = "file.storage", name = {"mode"}, havingValue = "cloud")
public class SmartStorageCloudConfig {
public class FileCloudConfig {
@Value("${file.storage.cloud.region}")
private String region;

View File

@ -90,7 +90,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
public void configure(WebSecurity web) {
// 忽略url
WebSecurity.IgnoredRequestConfigurer ignoring = web.ignoring();
List<String> ignoreUrlListList = smartSecurityUrlMatchers.getIgnoreUrlList();
List<String> ignoreUrlListList = smartSecurityUrlMatchers.getIgnoreUrl();
for (String url : ignoreUrlListList) {
ignoring.antMatchers(url);
}

View File

@ -4,6 +4,7 @@ import net.lab1024.smartadmin.service.common.security.SmartSecurityMetadataSourc
import net.lab1024.smartadmin.service.common.security.SmartSecurityUrlMatchers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.security.access.expression.method.ExpressionBasedAnnotationAttributeFactory;
import org.springframework.security.access.method.MethodSecurityMetadataSource;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
@ -18,15 +19,12 @@ import org.springframework.security.config.annotation.method.configuration.Globa
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityMethodConfig extends GlobalMethodSecurityConfiguration {
/**
* 无需登录的url
*/
@Autowired
private SmartSecurityUrlMatchers smartSecurityUrlMatchers;
@Override
public MethodSecurityMetadataSource customMethodSecurityMetadataSource(){
ExpressionBasedAnnotationAttributeFactory attributeFactory = new ExpressionBasedAnnotationAttributeFactory(this.getExpressionHandler());
return new SmartSecurityMetadataSource(attributeFactory, smartSecurityUrlMatchers.getNoValidUrlList());
return new SmartSecurityMetadataSource(attributeFactory, smartSecurityUrlMatchers);
}
}

View File

@ -1,21 +0,0 @@
package net.lab1024.smartadmin.service.config;
import net.lab1024.smartadmin.service.common.security.SmartSecurityUrlMatchers;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* [ ]
*
* @author zhuoda
* @date 2021/9/1 21:40
*/
@Configuration
public class SecurityUrlConfig {
@Bean
public SmartSecurityUrlMatchers securityUrl() {
return new SmartSecurityUrlMatchers();
}
}

View File

@ -0,0 +1,42 @@
package net.lab1024.smartadmin.service.config;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.smartadmin.service.module.support.reload.SmartReloadCommand;
import net.lab1024.smartadmin.service.module.support.reload.core.SmartReloadLogger;
import net.lab1024.smartadmin.service.module.support.reload.core.SmartReloadManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* [ ]
*
* @author zhuoda
* @date 2021/9/1 21:40
*/
@Slf4j
@Configuration
public class SmartReloadConfig {
@Autowired
private SmartReloadCommand smartReloadCommand;
@Bean
public SmartReloadManager initSmartReloadManager() {
/**
* 创建 Reload Manager 调度器
*/
SmartReloadManager smartReloadManager = new SmartReloadManager(new SmartReloadLogger() {
@Override
public void error(String string) {
log.error(string);
}
@Override
public void error(String string, Throwable e) {
log.error(string, e);
}
},smartReloadCommand, 1);
return smartReloadManager;
}
}

View File

@ -2,7 +2,7 @@ package net.lab1024.smartadmin.service.module.business.category;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.smartadmin.service.common.constant.CacheModuleBaseConst;
import net.lab1024.smartadmin.service.common.constant.CacheModuleConst;
import net.lab1024.smartadmin.service.common.constant.CommonConst;
import net.lab1024.smartadmin.service.module.business.category.domain.CategoryEntity;
import net.lab1024.smartadmin.service.module.business.category.domain.CategorySimpleDTO;
@ -10,7 +10,7 @@ import net.lab1024.smartadmin.service.module.business.category.domain.CategoryTr
import net.lab1024.smartadmin.service.module.business.category.domain.CategoryTreeVO;
import net.lab1024.smartadmin.service.module.support.beancache.cache.IBeanCache;
import net.lab1024.smartadmin.service.module.support.beancache.key.CacheKey;
import net.lab1024.smartadmin.service.module.support.beancache.load.CacheLoad;
import net.lab1024.smartadmin.service.module.support.beancache.anno.CacheLoad;
import net.lab1024.smartadmin.service.util.SmartBeanUtil;
import net.lab1024.smartadmin.service.util.SmartStringUtil;
import org.apache.commons.collections4.CollectionUtils;
@ -44,7 +44,7 @@ public class CategoryQueryService {
* @param cacheKey
* @return
*/
@CacheLoad(CacheModuleBaseConst.Category.CATEGORY)
@CacheLoad(CacheModuleConst.Category.CATEGORY)
public CategoryEntity queryCategory(String cacheKey) {
String businessId = CacheKey.getBusinessIdByCacheKey(cacheKey);
return categoryDao.selectById(businessId);
@ -56,7 +56,7 @@ public class CategoryQueryService {
* @param cacheKey
* @return
*/
@CacheLoad(CacheModuleBaseConst.Category.CATEGORY_SUB)
@CacheLoad(CacheModuleConst.Category.CATEGORY_SUB)
public List<CategoryEntity> querySubCategory(String cacheKey) {
/**
* 下划线 分隔 key
@ -87,7 +87,7 @@ public class CategoryQueryService {
*/
public Map<Long, List<CategoryEntity>> querySubCategoryFromCache(List<Long> categoryIdList) {
return categoryIdList.stream().collect(Collectors.toMap(Function.identity(), e -> {
String cacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Category.CATEGORY_SUB, e.toString());
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Category.CATEGORY_SUB, e.toString());
return cache.get(cacheKey);
}));
}
@ -102,7 +102,7 @@ public class CategoryQueryService {
if (null == categoryId) {
return Optional.empty();
}
String cacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Category.CATEGORY, categoryId.toString());
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Category.CATEGORY, categoryId.toString());
CategoryEntity entity = cache.get(cacheKey);
if (null == entity || entity.getDeletedFlag()) {
return Optional.empty();
@ -120,7 +120,7 @@ public class CategoryQueryService {
if (null == categoryId) {
return CommonConst.EMPTY_LIST;
}
String cacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Category.CATEGORY_SUB, getCacheId(categoryId, categoryType));
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Category.CATEGORY_SUB, getCacheId(categoryId, categoryType));
return cache.get(cacheKey);
}
@ -137,7 +137,7 @@ public class CategoryQueryService {
categoryIdList = categoryIdList.stream().distinct().collect(Collectors.toList());
return categoryIdList.stream().collect(Collectors.toMap(Function.identity(), e -> {
String cacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Category.CATEGORY, e.toString());
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Category.CATEGORY, e.toString());
return cache.get(cacheKey);
}));
}
@ -146,10 +146,10 @@ public class CategoryQueryService {
* 根据类目id 移除缓存
*/
public void removeCache() {
cache.removeByModule(CacheModuleBaseConst.Category.CATEGORY);
cache.removeByModule(CacheModuleBaseConst.Category.CATEGORY_SUB);
cache.removeByModule(CacheModuleConst.Category.CATEGORY);
cache.removeByModule(CacheModuleConst.Category.CATEGORY_SUB);
// 移除整个类目树缓存
cache.removeByModule(CacheModuleBaseConst.Category.CATEGORY_TREE);
cache.removeByModule(CacheModuleConst.Category.CATEGORY_TREE);
}
/**
@ -198,7 +198,7 @@ public class CategoryQueryService {
// 查询缓存
Long parentId = queryDTO.getParentId();
Integer categoryType = queryDTO.getCategoryType();
String cacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Category.CATEGORY_TREE, getCacheId(parentId, categoryType));
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Category.CATEGORY_TREE, getCacheId(parentId, categoryType));
List<CategoryTreeVO> treeList = cache.get(cacheKey);
if (null != treeList) {
return treeList;
@ -251,7 +251,7 @@ public class CategoryQueryService {
* @return
*/
public CategorySimpleDTO queryCategoryInfo(Long categoryId) {
String cacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Category.CATEGORY, categoryId.toString());
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Category.CATEGORY, categoryId.toString());
CategoryEntity categoryEntity = cache.get(cacheKey);
if (null == categoryEntity || categoryEntity.getDeletedFlag()) {
return null;
@ -279,7 +279,7 @@ public class CategoryQueryService {
*/
public List<CategoryEntity> queryCategoryAndParent(Long categoryId) {
List<CategoryEntity> parentCategoryList = Lists.newArrayList();
String cacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Category.CATEGORY, categoryId.toString());
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Category.CATEGORY, categoryId.toString());
CategoryEntity categoryEntity = cache.get(cacheKey);
if (null == categoryEntity || categoryEntity.getDeletedFlag()) {
return parentCategoryList;
@ -322,7 +322,7 @@ public class CategoryQueryService {
* @return
*/
public String queryCategoryName(Long categoryId) {
String cacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Category.CATEGORY, categoryId.toString());
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Category.CATEGORY, categoryId.toString());
CategoryEntity categoryEntity = cache.get(cacheKey);
if (null == categoryEntity || categoryEntity.getDeletedFlag()) {
return null;

View File

@ -9,7 +9,7 @@ import net.lab1024.smartadmin.service.module.business.notice.domain.dto.NoticeRe
import net.lab1024.smartadmin.service.module.business.notice.domain.entity.NoticeEntity;
import net.lab1024.smartadmin.service.module.business.notice.domain.vo.NoticeDetailVO;
import net.lab1024.smartadmin.service.module.business.notice.domain.vo.NoticeVO;
import net.lab1024.smartadmin.service.module.system.datascope.DataScope;
import net.lab1024.smartadmin.service.module.system.datascope.anno.DataScope;
import net.lab1024.smartadmin.service.module.system.datascope.constant.DataScopeTypeEnum;
import net.lab1024.smartadmin.service.module.system.datascope.constant.DataScopeWhereInTypeEnum;
import org.apache.ibatis.annotations.Mapper;

View File

@ -1,4 +1,4 @@
package net.lab1024.smartadmin.service.module.support.beancache.load;
package net.lab1024.smartadmin.service.module.support.beancache.anno;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;

View File

@ -6,14 +6,13 @@ import com.github.benmanes.caffeine.cache.LoadingCache;
import net.lab1024.smartadmin.service.module.support.beancache.domain.CacheData;
import net.lab1024.smartadmin.service.module.support.beancache.key.CacheKey;
import net.lab1024.smartadmin.service.module.support.beancache.key.CacheKeyBuilder;
import net.lab1024.smartadmin.service.module.support.beancache.load.CacheLoadMethod;
import net.lab1024.smartadmin.service.module.support.beancache.domain.CacheLoadMethod;
import net.lab1024.smartadmin.service.third.SmartApplicationContext;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
@ -33,6 +32,13 @@ public abstract class AbstractCaffeineCache implements IBeanCache {
@Override
public abstract LoadingCache<String, CacheData> getCache();
/**
* 缓存加载方法
* @return
*/
@Override
public abstract CacheLoadMethodRegister methodRegister();
/**
* 移除某个缓存
* @param key
@ -176,9 +182,8 @@ public abstract class AbstractCaffeineCache implements IBeanCache {
* @param scanPath
* @return
*/
public LoadingCache<String, CacheData> initCache(Integer expireDays, Integer maximumSize, String scanPath) {
//加载缓存方法
Map<String, CacheLoadMethod> methodMap = cacheLoadFunction(scanPath);
public LoadingCache<String, CacheData> initCache(Integer expireDays, Integer maximumSize) {
//构建缓存对象
Caffeine<Object, Object> builder = Caffeine.newBuilder();
if(maximumSize != null){
@ -190,7 +195,9 @@ public abstract class AbstractCaffeineCache implements IBeanCache {
return builder.recordStats()
.build(key -> {
String cacheModule = CacheKey.getCacheModeByCacheKey(key);
CacheLoadMethod loadMethod = methodMap.get(cacheModule);
CacheLoadMethodRegister methodRegister = this.methodRegister();
CacheLoadMethod loadMethod = methodRegister.getCacheLoadMethod(cacheModule);
if (loadMethod == null) {
return null;
}

View File

@ -4,7 +4,7 @@ import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.smartadmin.service.module.support.beancache.key.CacheKey;
import net.lab1024.smartadmin.service.module.support.beancache.load.CacheLoadMethod;
import net.lab1024.smartadmin.service.module.support.beancache.domain.CacheLoadMethod;
import net.lab1024.smartadmin.service.third.SmartApplicationContext;
import java.lang.reflect.Method;
@ -18,18 +18,14 @@ import java.util.Set;
* @date 2021/4/14 15:27
*/
@Slf4j
public class AbstractDisableCache implements IBeanCache {
private Map<String, CacheLoadMethod> methodMap;
public abstract class AbstractDisableCache implements IBeanCache {
/**
* 构造函数
* @param scanPath
* 缓存加载方法
* @return
*/
public AbstractDisableCache(String scanPath) {
//加载缓存方法
this.methodMap = cacheLoadFunction(scanPath);
}
@Override
public abstract CacheLoadMethodRegister methodRegister();
@Override
public Map<String, Object> getCache() {
@ -37,6 +33,7 @@ public class AbstractDisableCache implements IBeanCache {
return Maps.newHashMap();
}
@Override
public void remove(String key) {
log.warn("Cache is disable!");
@ -54,7 +51,9 @@ public class AbstractDisableCache implements IBeanCache {
log.warn("Cache is disable!");
String cacheModule = CacheKey.getCacheModeByCacheKey(key);
CacheLoadMethod loadMethod = methodMap.get(cacheModule);
CacheLoadMethodRegister methodRegister = this.methodRegister();
CacheLoadMethod loadMethod = methodRegister.getCacheLoadMethod(cacheModule);
if (loadMethod == null) {
throw null;
}

View File

@ -0,0 +1,48 @@
package net.lab1024.smartadmin.service.module.support.beancache.cache;
import net.lab1024.smartadmin.service.module.support.beancache.anno.CacheLoad;
import net.lab1024.smartadmin.service.module.support.beancache.domain.CacheLoadMethod;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
/**
* [ ]
*
* @author yandanyang
* @date 2021/9/25 16:32
*/
public class CacheLoadMethodRegister implements BeanPostProcessor {
private Map<String, CacheLoadMethod> cacheLoadMethodMap = new HashMap<>();
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
Method[] methods = ReflectionUtils.getAllDeclaredMethods(bean.getClass());
if (methods == null) {
return bean;
}
for (Method method : methods) {
CacheLoad cacheLoad = method.getAnnotation(CacheLoad.class);
if (cacheLoad != null) {
String cacheModule = cacheLoad.value();
net.lab1024.smartadmin.service.module.support.beancache.domain.CacheLoadMethod cacheLoadMethod = new net.lab1024.smartadmin.service.module.support.beancache.domain.CacheLoadMethod();
cacheLoadMethod.setCacheModule(cacheModule);
cacheLoadMethod.setExpireSecond(cacheLoad.expireSecond());
cacheLoadMethod.setLoadMethod(method);
cacheLoadMethodMap.put(cacheModule, cacheLoadMethod);
}
}
return bean;
}
public CacheLoadMethod getCacheLoadMethod(String key){
return cacheLoadMethodMap.get(key);
}
}

View File

@ -1,14 +1,5 @@
package net.lab1024.smartadmin.service.module.support.beancache.cache;
import com.google.common.collect.Maps;
import net.lab1024.smartadmin.service.module.support.beancache.load.CacheLoad;
import net.lab1024.smartadmin.service.module.support.beancache.load.CacheLoadMethod;
import org.reflections.Reflections;
import org.reflections.scanners.MethodAnnotationsScanner;
import org.reflections.util.ConfigurationBuilder;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;
/**
@ -19,6 +10,7 @@ import java.util.Set;
*/
public interface IBeanCache {
CacheLoadMethodRegister methodRegister();
/**
* 获取缓存
*
@ -63,6 +55,7 @@ public interface IBeanCache {
/**
* 待过期时间get
*
* @param key
* @param obj
* @param expireSecond
@ -99,30 +92,4 @@ public interface IBeanCache {
*/
void removeByModuleAndGroup(String module, String group);
/**
* 加载 CacheLoad注解的方法
* @param scanPath
* @return
*/
default Map<String, CacheLoadMethod> cacheLoadFunction(String scanPath) {
Reflections reflections = new Reflections(new ConfigurationBuilder()
.forPackages(scanPath)
.addScanners(new MethodAnnotationsScanner())
);
Map<String, CacheLoadMethod> methodMap = Maps.newHashMap();
Set<Method> methods = reflections.getMethodsAnnotatedWith(CacheLoad.class);
for (Method method : methods) {
CacheLoad cacheLoad = method.getAnnotation(CacheLoad.class);
if (cacheLoad != null) {
String cacheModule = cacheLoad.value();
CacheLoadMethod cacheLoadMethod = new CacheLoadMethod();
cacheLoadMethod.setCacheModule(cacheModule);
cacheLoadMethod.setExpireSecond(cacheLoad.expireSecond());
cacheLoadMethod.setLoadMethod(method);
methodMap.put(cacheModule, cacheLoadMethod);
}
}
return methodMap;
}
}

View File

@ -1,18 +0,0 @@
package net.lab1024.smartadmin.service.module.support.beancache.domain;
import lombok.Data;
/**
* [ ]
*
* @author 罗伊
* @date 2021/5/10 10:45
*/
@Data
public class CacheClear {
private String module;
private String group;
}

View File

@ -1,4 +1,4 @@
package net.lab1024.smartadmin.service.module.support.beancache.load;
package net.lab1024.smartadmin.service.module.support.beancache.domain;
import lombok.Data;

View File

@ -1,56 +0,0 @@
package net.lab1024.smartadmin.service.module.support.beancache.domain;
import lombok.Data;
/**
* [ ]
*
* @author 罗伊
* @date 2020/9/8 11:14
*/
@Data
public class CacheStatsVO {
/**
* 记录缓存请求数量
*/
private long requestCount;
/**
* 记录缓存命中
*/
private long hitCount;
/**
* 记录缓存未命中
*/
private long missCount;
/**
* CacheLoader加载成功
*/
private long loadSuccessCount;
/**
* CacheLoader加载成功加载失败
*/
private long loadFailureCount;
/**
* 总加载时间
*/
private long totalLoadTime;
/**
* 缓存失效的数量
*/
private long evictionCount;
/**
* 返回缓存命中率
*/
private double hitRate;
/**
* 返回缓存命中率
*/
private double missRate;
/**
* 加载新值的平均时间
*/
private double averageLoadPenalty;
}

View File

@ -9,7 +9,7 @@ import net.lab1024.smartadmin.service.common.codeconst.FileResponseCodeConst;
import net.lab1024.smartadmin.service.common.codeconst.ResponseCodeConst;
import net.lab1024.smartadmin.service.common.constant.CommonConst;
import net.lab1024.smartadmin.service.common.domain.ResponseDTO;
import net.lab1024.smartadmin.service.config.SmartStorageCloudConfig;
import net.lab1024.smartadmin.service.config.FileCloudConfig;
import net.lab1024.smartadmin.service.module.support.file.domain.dto.FileDownloadDTO;
import net.lab1024.smartadmin.service.module.support.file.domain.dto.FileMetadataDTO;
import net.lab1024.smartadmin.service.module.support.file.domain.vo.FileUploadVO;
@ -44,7 +44,7 @@ public class FileStorageCloudServiceImpl implements IFileStorageService {
private AmazonS3 amazonS3;
@Autowired
private SmartStorageCloudConfig cloudConfig;
private FileCloudConfig cloudConfig;
/**
* 自定义元数据 文件名称

View File

@ -0,0 +1,53 @@
package net.lab1024.smartadmin.service.module.support.reload;
import net.lab1024.smartadmin.service.module.support.reload.core.AbstractSmartReloadCommand;
import net.lab1024.smartadmin.service.module.support.reload.core.domain.ReloadItem;
import net.lab1024.smartadmin.service.module.support.reload.core.domain.SmartReloadResult;
import net.lab1024.smartadmin.service.module.support.reload.dao.ReloadItemDao;
import net.lab1024.smartadmin.service.module.support.reload.dao.ReloadResultDao;
import net.lab1024.smartadmin.service.module.support.reload.domain.ReloadItemEntity;
import net.lab1024.smartadmin.service.module.support.reload.domain.ReloadResultEntity;
import net.lab1024.smartadmin.service.util.SmartBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* Smart Reload 业务
*
* @author listen
* @date 2018/02/10 09:18
*/
@Component
public class SmartReloadCommand extends AbstractSmartReloadCommand {
@Autowired
private ReloadItemDao reloadItemDao;
@Autowired
private ReloadResultDao reloadResultDao;
/**
* 读取数据库中SmartReload项
*
* @return List<ReloadItem>
*/
@Override
public List<ReloadItem> readReloadItem() {
List<ReloadItemEntity> reloadItemEntityList = reloadItemDao.selectList(null);
return SmartBeanUtil.copyList(reloadItemEntityList, ReloadItem.class);
}
/**
* 保存reload结果
*
* @param smartReloadResult
*/
@Override
public void handleReloadResult(SmartReloadResult smartReloadResult) {
ReloadResultEntity reloadResultEntity = SmartBeanUtil.copy(smartReloadResult, ReloadResultEntity.class);
reloadResultDao.insert(reloadResultEntity);
}
}

View File

@ -1,134 +0,0 @@
package net.lab1024.smartadmin.service.module.support.reload;
import net.lab1024.smartadmin.service.module.support.reload.annotation.SmartReload;
import net.lab1024.smartadmin.service.module.support.reload.domain.AbstractSmartReloadObject;
import net.lab1024.smartadmin.service.module.support.reload.domain.AnnotationReloadObject;
import net.lab1024.smartadmin.service.module.support.reload.domain.InterfaceReloadObject;
import net.lab1024.smartadmin.service.module.support.reload.domain.entity.ReloadItem;
import net.lab1024.smartadmin.service.module.support.reload.domain.entity.SmartReloadResult;
import net.lab1024.smartadmin.service.module.support.reload.interfaces.SmartReloadCommandInterface;
import net.lab1024.smartadmin.service.module.support.reload.interfaces.SmartReloadThreadLogger;
import net.lab1024.smartadmin.service.module.support.reload.interfaces.SmartReloadable;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import static java.util.Objects.requireNonNull;
/**
* SmartReloadManager 管理器
* <p>
* 可以在此类中添加 检测任务 以及注册 处理程序
*
* @author zhuoda
*/
public class SmartReloadManager {
private Map<String, AbstractSmartReloadObject> tagReloadObject;
private SmartReloadScheduler reloadScheduler;
private SmartReloadThreadLogger logger;
public SmartReloadManager(SmartReloadThreadLogger logger, int threadCount) {
this.tagReloadObject = new ConcurrentHashMap<>();
if (logger == null) {
throw new ExceptionInInitializerError("SmartReloadLoggerImp cannot be null");
}
if (threadCount < 1) {
throw new ExceptionInInitializerError("threadCount must be greater than 1");
}
this.logger = logger;
this.reloadScheduler = new SmartReloadScheduler(this.logger, threadCount);
}
/**
* 默认创建单个线程
*
* @param logger
*/
public SmartReloadManager(SmartReloadThreadLogger logger) {
this(logger, 1);
}
/**
* 停止
*/
public void shutdown() {
reloadScheduler.shutdown();
}
/**
* 添加任务
*
* @param command SmartReloadCommand实现类
* @param initialDelay 第一次执行前的延迟时间
* @param delay 任务间隔时间
* @param unit 延迟单位 TimeUnit 小时秒等
*/
public void addCommand(SmartReloadCommandInterface command, long initialDelay, long delay, TimeUnit unit) {
reloadScheduler.addCommand(command, initialDelay, delay, unit);
}
/**
* 注册 实现接口的方式
*
* @param tag
* @param reloadable
*/
public void register(String tag, SmartReloadable reloadable) {
requireNonNull(reloadable);
requireNonNull(tag);
if (tagReloadObject.containsKey(tag)) {
logger.error("<<SmartReloadManager>> register duplicated tag reload : " + tag + " , and it will be cover!");
}
tagReloadObject.put(tag, new InterfaceReloadObject(reloadable));
}
/**
* 注册 要求此类必须包含使用了SmartReload注解的方法
*
* @param reloadObject
*/
public void register(Object reloadObject) {
requireNonNull(reloadObject);
Method[] declaredMethods = reloadObject.getClass().getDeclaredMethods();
if (declaredMethods != null) {
for (int i = 0; i < declaredMethods.length; i++) {
Method method = declaredMethods[i];
SmartReload annotation = method.getAnnotation(SmartReload.class);
if (annotation != null) {
String reloadTag = annotation.value();
this.register(reloadTag, new AnnotationReloadObject(reloadObject, method));
}
}
}
}
private void register(String tag, AbstractSmartReloadObject reloadObject) {
if (tagReloadObject.containsKey(tag)) {
logger.error("<<SmartReloadManager>> register duplicated tag reload : " + tag + " , and it will be cover!");
}
tagReloadObject.put(tag, reloadObject);
}
/**
* Reload 已注册的ReloadItem
*
* @param reloadItem
* @return SmartReloadResult
*/
public SmartReloadResult doReload(ReloadItem reloadItem) {
AbstractSmartReloadObject reloadObject = tagReloadObject.get(reloadItem.getTag());
if (reloadObject != null) {
return reloadObject.reload(reloadItem);
}
// 返回注册结果
return new SmartReloadResult(reloadItem.getTag(), reloadItem.getArgs(), reloadItem.getIdentification(), false, "No registered reload handler was found");
}
}

View File

@ -1,91 +0,0 @@
package net.lab1024.smartadmin.service.module.support.reload;
import net.lab1024.smartadmin.service.module.support.reload.interfaces.SmartReloadCommandInterface;
import net.lab1024.smartadmin.service.module.support.reload.interfaces.SmartReloadThreadLogger;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Reload 调度器
*
* @author zhuoda
*/
public class SmartReloadScheduler {
private ScheduledThreadPoolExecutor executor;
private SmartReloadThreadLogger logger;
SmartReloadScheduler(SmartReloadThreadLogger logger, int threadCount) {
this.executor = new ScheduledThreadPoolExecutor(threadCount, new SmartReloadSchedulerThreadFactory());
this.logger = logger;
}
void shutdown() {
try {
executor.shutdown();
} catch (Throwable e) {
logger.error("<<SmartReloadScheduler>> shutdown ", e);
}
}
void addCommand(SmartReloadCommandInterface command, long initialDelay, long delay, TimeUnit unit) {
executor.scheduleWithFixedDelay(new ScheduleRunnable(command, this.logger), initialDelay, delay, unit);
}
static class ScheduleRunnable implements Runnable {
private SmartReloadCommandInterface command;
private SmartReloadThreadLogger logger;
public ScheduleRunnable(SmartReloadCommandInterface command, SmartReloadThreadLogger logger) {
this.command = command;
this.logger = logger;
}
@Override
public void run() {
try {
command.doTask();
} catch (Throwable e) {
logger.error("", e);
}
}
}
static class SmartReloadSchedulerThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
SmartReloadSchedulerThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
namePrefix = "smart-reload-" + poolNumber.getAndIncrement() + "-thread-";
}
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
if (t.isDaemon()) {
t.setDaemon(false);
}
if (t.getPriority() != Thread.NORM_PRIORITY) {
t.setPriority(Thread.NORM_PRIORITY);
}
return t;
}
}
}

View File

@ -1,66 +0,0 @@
package net.lab1024.smartadmin.service.module.support.reload.abstracts;
import net.lab1024.smartadmin.service.module.support.reload.SmartReloadManager;
import net.lab1024.smartadmin.service.module.support.reload.domain.entity.ReloadItem;
import net.lab1024.smartadmin.service.module.support.reload.interfaces.SmartReloadCommandInterface;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
/**
* 检测是否 Reload 的类
*
* @author zhuoda
*/
public abstract class AbstractSmartReloadCommand implements SmartReloadCommandInterface {
/**
* 当前ReloadItem的存储器
*/
private ConcurrentHashMap<String, String> currentTags = null;
/**
* Reload的执行类
*/
private SmartReloadManager reloadManager;
public AbstractSmartReloadCommand(SmartReloadManager reloadManager) {
this.reloadManager = reloadManager;
this.currentTags = new ConcurrentHashMap<>();
// 初始获取ReloadItem数据
List<ReloadItem> readTagStatesFromDb = readReloadItem();
if (readTagStatesFromDb != null) {
for (ReloadItem reloadItem : readTagStatesFromDb) {
String tag = reloadItem.getTag();
String tagChangeIdentifier = reloadItem.getIdentification();
this.currentTags.put(tag, tagChangeIdentifier);
}
}
}
/**
* 任务
* 读取数据库中 ReloadItem 数据
* 校验是否发生变化
* 执行重加载动作
*/
@Override
public void doTask() {
// 获取数据库数据
List<ReloadItem> readTagStatesFromDb = readReloadItem();
String tag;
String tagIdentifier;
String preTagChangeIdentifier;
for (ReloadItem reloadItem : readTagStatesFromDb) {
tag = reloadItem.getTag();
tagIdentifier = reloadItem.getIdentification();
preTagChangeIdentifier = currentTags.get(tag);
// 数据不一致
if (preTagChangeIdentifier == null || ! preTagChangeIdentifier.equals(tagIdentifier)) {
// 更新map数据
currentTags.put(tag, tagIdentifier);
// 执行重新加载此项的动作
handleReloadResult(this.reloadManager.doReload(reloadItem));
}
}
}
}

View File

@ -1,65 +0,0 @@
package net.lab1024.smartadmin.service.module.support.reload.abstracts;
import net.lab1024.smartadmin.service.module.support.reload.SmartReloadManager;
import net.lab1024.smartadmin.service.module.support.reload.domain.entity.ReloadItem;
import net.lab1024.smartadmin.service.module.support.reload.interfaces.SmartReloadCommandInterface;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
/**
* 检测是否 Reload 的类
*
* @author zhuoda
*/
public abstract class AbstractSmartReloadCommand4Spring implements SmartReloadCommandInterface {
/**
* 当前ReloadItem的存储器
*/
protected ConcurrentHashMap<String, String> currentTags = new ConcurrentHashMap<>();
/**
* Reload的执行类
*/
@Autowired
protected SmartReloadManager reloadManager;
public void init() {
List<ReloadItem> readTagStatesFromDb = readReloadItem();
if (readTagStatesFromDb != null) {
for (ReloadItem reloadItem : readTagStatesFromDb) {
String tag = reloadItem.getTag();
String tagChangeIdentifier = reloadItem.getIdentification();
this.currentTags.put(tag, tagChangeIdentifier);
}
}
}
/**
* 任务
* 读取数据库中 ReloadItem 数据
* 校验是否发生变化
* 执行重加载动作
*/
@Override
public void doTask() {
// 获取数据库数据
List<ReloadItem> readTagStatesFromDb = readReloadItem();
String tag;
String tagIdentifier;
String preTagChangeIdentifier;
for (ReloadItem reloadItem : readTagStatesFromDb) {
tag = reloadItem.getTag();
tagIdentifier = reloadItem.getIdentification();
preTagChangeIdentifier = currentTags.get(tag);
// 数据不一致
if (preTagChangeIdentifier == null || ! preTagChangeIdentifier.equals(tagIdentifier)) {
// 更新map数据
currentTags.put(tag, tagIdentifier);
// 执行重新加载此项的动作
handleReloadResult(this.reloadManager.doReload(reloadItem));
}
}
}
}

View File

@ -0,0 +1,94 @@
package net.lab1024.smartadmin.service.module.support.reload.core;
import net.lab1024.smartadmin.service.module.support.reload.core.domain.ReloadItem;
import net.lab1024.smartadmin.service.module.support.reload.core.domain.ReloadObject;
import net.lab1024.smartadmin.service.module.support.reload.core.domain.SmartReloadResult;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* 检测是否 Reload 的类
*
* @author zhuoda
*/
public abstract class AbstractSmartReloadCommand {
/**
* 当前ReloadItem的存储器
*/
private ConcurrentHashMap<String, String> tagIdentifierMap = new ConcurrentHashMap<>();
private SmartReloadManager smartReloadManager;
/**
*
* @return
*/
public void setReloadManager(SmartReloadManager smartReloadManager){
this.smartReloadManager = smartReloadManager;
}
/**
* 该方法返回一个List<ReloadItem></>:<br>
* ReloadItem对象的tagIdentify为该tag的 状态状态其实就是个字符串如果该字符串跟上次有变化则进行reload操作<br>
* ReloadItem对象的args为 reload操作需要的参数<br><br>
*
* @return List<ReloadItem>
*/
public abstract List<ReloadItem> readReloadItem();
/**
* 处理Reload结果
*
* @param reloadResult
*/
public abstract void handleReloadResult(SmartReloadResult reloadResult);
/**
* 获取本地缓存tag标识
* @return
*/
public ConcurrentHashMap<String, String> getTagIdentifierMap() {
if (tagIdentifierMap != null) {
return tagIdentifierMap;
}
List<ReloadItem> reloadItemList = this.readReloadItem();
if (reloadItemList == null) {
return tagIdentifierMap;
}
for (ReloadItem reloadItem : reloadItemList) {
String tag = reloadItem.getTag();
String identification = reloadItem.getIdentification();
tagIdentifierMap.put(tag, identification);
}
return tagIdentifierMap;
}
/**
* 设置新的缓存标识
* @param tag
* @param identification
*/
public void putIdentifierMap(String tag, String identification) {
tagIdentifierMap.put(tag, identification);
}
/**
* 获取重载对象
* @return
*/
public ReloadObject reloadObject(String tag) {
if(this.smartReloadManager == null){
return null;
}
Map<String, ReloadObject> reloadObjectMap = smartReloadManager.reloadObjectMap();
return reloadObjectMap.get(tag);
}
}

View File

@ -1,9 +1,9 @@
package net.lab1024.smartadmin.service.module.support.reload.interfaces;
package net.lab1024.smartadmin.service.module.support.reload.core;
/**
* SmartReloadThreadLogger 日志类
*/
public interface SmartReloadThreadLogger {
public interface SmartReloadLogger {
void error(String string);

View File

@ -0,0 +1,92 @@
package net.lab1024.smartadmin.service.module.support.reload.core;
import net.lab1024.smartadmin.service.module.support.reload.core.anno.SmartReload;
import net.lab1024.smartadmin.service.module.support.reload.core.domain.ReloadObject;
import net.lab1024.smartadmin.service.module.support.reload.core.thread.SmartReloadScheduler;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* SmartReloadManager 管理器
* <p>
* 可以在此类中添加 检测任务 以及注册 处理程序
*
* @author zhuoda
*/
public class SmartReloadManager implements BeanPostProcessor {
private Map<String, ReloadObject> reloadObjectMap = new ConcurrentHashMap<>();
private SmartReloadScheduler reloadScheduler;
private SmartReloadLogger logger;
public SmartReloadManager(SmartReloadLogger logger,
AbstractSmartReloadCommand reloadCommand,
int threadCount) {
if (logger == null) {
throw new ExceptionInInitializerError("SmartReloadLoggerImp cannot be null");
}
if (threadCount < 1) {
throw new ExceptionInInitializerError("threadCount must be greater than 1");
}
this.logger = logger;
this.reloadScheduler = new SmartReloadScheduler(this.logger, threadCount);
this.reloadScheduler.addCommand(reloadCommand);
reloadCommand.setReloadManager(this);
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
Method[] methods = ReflectionUtils.getAllDeclaredMethods(bean.getClass());
if (methods == null) {
return bean;
}
for (Method method : methods) {
SmartReload smartReload = method.getAnnotation(SmartReload.class);
if (smartReload == null) {
continue;
}
int paramCount = method.getParameterCount();
if (paramCount > 1) {
logger.error("<<SmartReloadManager>> register tag reload : " + smartReload.value() + " , param count cannot greater than one !");
continue;
}
String reloadTag = smartReload.value();
this.register(reloadTag, new ReloadObject(bean, method));
}
return bean;
}
/**
* 注册reload
*
* @param tag
* @param reloadObject
*/
private void register(String tag, ReloadObject reloadObject) {
if (reloadObjectMap.containsKey(tag)) {
logger.error("<<SmartReloadManager>> register duplicated tag reload : " + tag + " , and it will be cover!");
}
reloadObjectMap.put(tag, reloadObject);
}
/**
* 获取重载对象
* @return
*/
public Map<String, ReloadObject> reloadObjectMap() {
return this.reloadObjectMap;
}
}

View File

@ -1,4 +1,4 @@
package net.lab1024.smartadmin.service.module.support.reload.annotation;
package net.lab1024.smartadmin.service.module.support.reload.core.anno;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;

View File

@ -0,0 +1,28 @@
package net.lab1024.smartadmin.service.module.support.reload.core.domain;
import lombok.Data;
/**
* ReloadItem
*
* @author zhuoda
*/
@Data
public class ReloadItem {
/**
* 项名称
*/
private String tag;
/**
* 参数
*/
private String args;
/**
* 标识
*/
private String identification;
}

View File

@ -0,0 +1,29 @@
package net.lab1024.smartadmin.service.module.support.reload.core.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.lang.reflect.Method;
/**
* Reload 处理程序的实现方法
* 用于包装以注解 SmartReload 实现的处理类
*
* @author zhuoda
*/
@Data
@AllArgsConstructor
public class ReloadObject {
/**
* 方法对应的实例化对象
*/
private Object reloadObject;
/**
* 重新加载执行的方法
*/
private Method method;
}

View File

@ -0,0 +1,39 @@
package net.lab1024.smartadmin.service.module.support.reload.core.domain;
import lombok.Data;
/**
* t_reload_result 实体类
*
* @author zhuoda
*/
@Data
public class SmartReloadResult {
/**
* 项名称
*/
private String tag;
/**
* 参数
*/
private String args;
/**
* 标识
*/
private String identification;
/**
* 处理结果
*/
private boolean result;
/**
* 异常说明
*/
private String exception;
}

View File

@ -0,0 +1,92 @@
package net.lab1024.smartadmin.service.module.support.reload.core.thread;
import net.lab1024.smartadmin.service.module.support.reload.core.AbstractSmartReloadCommand;
import net.lab1024.smartadmin.service.module.support.reload.core.SmartReloadLogger;
import net.lab1024.smartadmin.service.module.support.reload.core.domain.ReloadItem;
import net.lab1024.smartadmin.service.module.support.reload.core.domain.ReloadObject;
import net.lab1024.smartadmin.service.module.support.reload.core.domain.SmartReloadResult;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
public class SmartReloadRunnable implements Runnable {
private AbstractSmartReloadCommand abstractSmartReloadCommand;
private SmartReloadLogger logger;
public SmartReloadRunnable(AbstractSmartReloadCommand abstractSmartReloadCommand, SmartReloadLogger logger) {
this.abstractSmartReloadCommand = abstractSmartReloadCommand;
this.logger = logger;
}
@Override
public void run() {
try {
this.doTask();
} catch (Throwable e) {
logger.error("", e);
}
}
/**
* 检测Identifier变化执行reload
*/
private void doTask() {
List<ReloadItem> reloadItemList = this.abstractSmartReloadCommand.readReloadItem();
ConcurrentHashMap<String, String> tagIdentifierMap = this.abstractSmartReloadCommand.getTagIdentifierMap();
for (ReloadItem reloadItem : reloadItemList) {
String tag = reloadItem.getTag();
String tagIdentifier = reloadItem.getIdentification();
String preTagChangeIdentifier = tagIdentifierMap.get(tag);
// 数据不一致
if (preTagChangeIdentifier == null || !preTagChangeIdentifier.equals(tagIdentifier)) {
this.abstractSmartReloadCommand.putIdentifierMap(tag, tagIdentifier);
// 执行重新加载此项的动作
SmartReloadResult reloadResult = this.doReload(reloadItem);
this.abstractSmartReloadCommand.handleReloadResult(reloadResult);
}
}
}
/**
* 方法调用
* @param reloadItem
* @return
*/
private SmartReloadResult doReload(ReloadItem reloadItem) {
SmartReloadResult result = new SmartReloadResult();
ReloadObject reloadObject = this.abstractSmartReloadCommand.reloadObject(reloadItem.getTag());
Method method = reloadObject.getMethod();
result.setTag(reloadItem.getTag());
result.setArgs(reloadItem.getArgs());
result.setIdentification(reloadItem.getIdentification());
result.setResult(true);
int paramCount = method.getParameterCount();
if (paramCount > 1) {
result.setResult(false);
result.setException("reload方法" + method.getName() + "参数太多");
return result;
}
try {
if (paramCount == 0) {
method.invoke(reloadObject.getReloadObject());
} else {
method.invoke(reloadObject.getReloadObject(), reloadItem.getArgs());
}
} catch (Throwable throwable) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
throwable.printStackTrace(pw);
result.setResult(false);
result.setException(throwable.toString());
}
return result;
}
}

View File

@ -0,0 +1,44 @@
package net.lab1024.smartadmin.service.module.support.reload.core.thread;
import net.lab1024.smartadmin.service.module.support.reload.core.AbstractSmartReloadCommand;
import net.lab1024.smartadmin.service.module.support.reload.core.SmartReloadLogger;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Reload 调度器
*
* @author zhuoda
*/
public class SmartReloadScheduler {
private ScheduledThreadPoolExecutor executor;
private SmartReloadLogger logger;
public SmartReloadScheduler(SmartReloadLogger logger, int threadCount) {
this.executor = new ScheduledThreadPoolExecutor(threadCount, new SmartReloadThreadFactory());
this.logger = logger;
}
public void shutdown() {
try {
executor.shutdown();
} catch (Throwable e) {
logger.error("<<SmartReloadScheduler>> shutdown ", e);
}
}
public void addCommand(AbstractSmartReloadCommand command, long initialDelay, long delay, TimeUnit unit) {
executor.scheduleWithFixedDelay(new SmartReloadRunnable(command, this.logger), initialDelay, delay, unit);
}
public void addCommand(AbstractSmartReloadCommand command) {
executor.scheduleWithFixedDelay(new SmartReloadRunnable(command, this.logger), 10, 20, TimeUnit.SECONDS);
}
}

View File

@ -0,0 +1,35 @@
package net.lab1024.smartadmin.service.module.support.reload.core.thread;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
public class SmartReloadThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
SmartReloadThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
namePrefix = "smart-reload-" + poolNumber.getAndIncrement() + "-thread-";
}
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
if (t.isDaemon()) {
t.setDaemon(false);
}
if (t.getPriority() != Thread.NORM_PRIORITY) {
t.setPriority(Thread.NORM_PRIORITY);
}
return t;
}
}

View File

@ -0,0 +1,17 @@
package net.lab1024.smartadmin.service.module.support.reload.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import net.lab1024.smartadmin.service.module.support.reload.domain.ReloadItemEntity;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
/**
* t_reload_item 数据表dao
*
* @author listen
* @date 2018/02/10 09:23
*/
@Component
@Mapper
public interface ReloadItemDao extends BaseMapper<ReloadItemEntity> {
}

View File

@ -0,0 +1,23 @@
package net.lab1024.smartadmin.service.module.support.reload.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import net.lab1024.smartadmin.service.module.support.reload.domain.ReloadResultEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* t_reload_result 数据表dao
*
* @author listen
* @date 2018/02/10 09:23
*/
@Component
@Mapper
public interface ReloadResultDao extends BaseMapper<ReloadResultEntity> {
List<ReloadResultEntity> query(@Param("tag") String tag);
}

View File

@ -1,32 +0,0 @@
package net.lab1024.smartadmin.service.module.support.reload.domain;
import net.lab1024.smartadmin.service.module.support.reload.domain.entity.ReloadItem;
import net.lab1024.smartadmin.service.module.support.reload.domain.entity.SmartReloadResult;
import java.io.PrintWriter;
import java.io.StringWriter;
/**
* AbstractSmartReloadObject 处理程序的抽象类
*
* @author zhuoda
*/
public abstract class AbstractSmartReloadObject {
protected String getStackTrace(Throwable e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
return sw.toString();
}
/**
* 通过reloadItem参数reload获得结果
*
* @param reloadItem
* @return boolean
* @author zhuokongming
* @date 2016年10月21日 下午2:09:44
*/
public abstract SmartReloadResult reload(ReloadItem reloadItem);
}

View File

@ -1,57 +0,0 @@
package net.lab1024.smartadmin.service.module.support.reload.domain;
import net.lab1024.smartadmin.service.module.support.reload.annotation.SmartReload;
import net.lab1024.smartadmin.service.module.support.reload.domain.entity.ReloadItem;
import net.lab1024.smartadmin.service.module.support.reload.domain.entity.SmartReloadResult;
import java.lang.reflect.Method;
/**
* Reload 处理程序的实现类
* 用于包装以注解 SmartReload 实现的处理类
*
* @author zhuoda
*/
public class AnnotationReloadObject extends AbstractSmartReloadObject {
private Object reloadObject;
private Method method;
public AnnotationReloadObject(Object reloadObject, Method method) {
super();
this.reloadObject = reloadObject;
this.method = method;
this.method.setAccessible(true);
}
@Override
public SmartReloadResult reload(ReloadItem reloadItem) {
SmartReloadResult result = new SmartReloadResult();
String tag = ((SmartReload)this.method.getAnnotation(SmartReload.class)).value();
result.setTag(tag);
result.setArgs(reloadItem.getArgs());
result.setIdentification(reloadItem.getIdentification());
result.setResult(true);
int paramCount = this.method.getParameterCount();
if (paramCount > 1) {
result.setResult(false);
result.setException("reload方法" + this.method.getName() + "参数太多");
return result;
} else {
try {
if (paramCount == 0) {
this.method.invoke(this.reloadObject);
} else {
this.method.invoke(this.reloadObject, reloadItem.getArgs());
}
} catch (Throwable var6) {
result.setResult(false);
result.setException(this.getStackTrace(var6));
}
return result;
}
}
}

View File

@ -1,37 +0,0 @@
package net.lab1024.smartadmin.service.module.support.reload.domain;
import net.lab1024.smartadmin.service.module.support.reload.domain.entity.ReloadItem;
import net.lab1024.smartadmin.service.module.support.reload.domain.entity.SmartReloadResult;
import net.lab1024.smartadmin.service.module.support.reload.interfaces.SmartReloadable;
/**
* Reload 处理程序的实现类
* 用于处理以接口实现的处理类
*
* @author zhuoda
*/
public class InterfaceReloadObject extends AbstractSmartReloadObject {
private SmartReloadable object;
public InterfaceReloadObject(SmartReloadable object) {
super();
this.object = object;
}
@Override
public SmartReloadResult reload(ReloadItem reloadItem) {
SmartReloadResult reloadResult = new SmartReloadResult();
reloadResult.setArgs(reloadItem.getArgs());
reloadResult.setIdentification(reloadItem.getIdentification());
reloadResult.setTag(reloadItem.getTag());
try {
boolean res = object.reload(reloadItem);
reloadResult.setResult(res);
} catch (Throwable e) {
reloadResult.setException(getStackTrace(e));
}
return reloadResult;
}
}

View File

@ -0,0 +1,46 @@
package net.lab1024.smartadmin.service.module.support.reload.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.util.Date;
/**
* t_reload_item 数据表 实体类
*
* @author listen
* @date 2018/02/10 09:29
*/
@Data
@TableName("t_reload_item")
public class ReloadItemEntity {
/**
* 加载项标签
*/
@TableId(type = IdType.INPUT)
private String tag;
/**
* 参数
*/
private String args;
/**
* 运行标识
*/
private String identification;
/**
* 更新时间
*/
private Date updateTime;
/**
* 创建时间
*/
private Date createTime;
}

View File

@ -0,0 +1,48 @@
package net.lab1024.smartadmin.service.module.support.reload.domain;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.util.Date;
/**
* t_reload_result 数据表 实体类
*
* @author listen
* @date 2018/02/10 09:29
*/
@Data
@TableName("t_reload_result")
public class ReloadResultEntity {
/**
* 加载项标签
*/
private String tag;
/**
* 运行标识
*/
private String identification;
/**
* 参数
*/
private String args;
/**
* 运行结果
*/
private Boolean result;
/**
* 异常
*/
private String exception;
/**
* 创建时间
*/
private Date createTime;
}

View File

@ -1,55 +0,0 @@
package net.lab1024.smartadmin.service.module.support.reload.domain.entity;
/**
* ReloadItem
*
* @author zhuoda
*/
public class ReloadItem {
/**
* 项名称
*/
private String tag;
/**
* 参数
*/
private String args;
/**
* 标识
*/
private String identification;
public ReloadItem() {
}
public ReloadItem(String tag, String identification, String args) {
this.tag = tag;
this.identification = identification;
this.args = args;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public String getIdentification() {
return identification;
}
public void setIdentification(String identification) {
this.identification = identification;
}
public String getArgs() {
return args;
}
public void setArgs(String args) {
this.args = args;
}
@Override
public String toString() {
return "ReloadItem{" + "tag='" + tag + '\'' + ", identification='" + identification + '\'' + ", args='" + args + '\'' + '}';
}
}

View File

@ -1,102 +0,0 @@
package net.lab1024.smartadmin.service.module.support.reload.domain.entity;
/**
* t_reload_result 实体类
*
* @author zhuoda
*/
public class SmartReloadResult {
/**
* 项名称
*/
private String tag;
/**
* 参数
*/
private String args;
/**
* 标识
*/
private String identification;
/**
* 处理结果
*/
private boolean result;
/**
* 异常说明
*/
private String exception;
public SmartReloadResult() {
}
public SmartReloadResult(String tag, String args, boolean result, String exception) {
this.tag = tag;
this.args = args;
this.result = result;
this.exception = exception;
}
public SmartReloadResult(String tag, String args, String identification, boolean result, String exception) {
this.tag = tag;
this.args = args;
this.identification = identification;
this.result = result;
this.exception = exception;
}
public void setTag(String tag) {
this.tag = tag;
}
public void setArgs(String args) {
this.args = args;
}
public void setIdentification(String identification) {
this.identification = identification;
}
public void setResult(boolean result) {
this.result = result;
}
public void setException(String exception) {
this.exception = exception;
}
public String getTag() {
return tag;
}
public String getArgs() {
return args;
}
public String getIdentification() {
return identification;
}
public boolean isResult() {
return result;
}
public String getException() {
return exception;
}
@Override
public String toString() {
return "SmartReloadResult{" +
"tag='" + tag + '\'' +
", args='" + args + '\'' +
", identification='" + identification + '\'' +
", result=" + result +
", exception='" + exception + '\'' +
'}';
}
}

View File

@ -1,39 +0,0 @@
package net.lab1024.smartadmin.service.module.support.reload.interfaces;
import net.lab1024.smartadmin.service.module.support.reload.domain.entity.ReloadItem;
import net.lab1024.smartadmin.service.module.support.reload.domain.entity.SmartReloadResult;
import java.util.List;
/**
* 检测是否 Reload 的类
*
* @author zhuoda
*/
public interface SmartReloadCommandInterface {
/**
* 任务
* 读取数据库中 ReloadItem 数据
* 校验是否发生变化
* 执行重加载动作
*/
void doTask();
/**
* 该方法返回一个List<ReloadItem></>:<br>
* ReloadItem对象的tagIdentify为该tag的 状态状态其实就是个字符串如果该字符串跟上次有变化则进行reload操作<br>
* ReloadItem对象的args为 reload操作需要的参数<br><br>
*
* @return List<ReloadItem>
*/
List<ReloadItem> readReloadItem();
/**
* 处理Reload结果
*
* @param reloadResult
*/
void handleReloadResult(SmartReloadResult reloadResult);
}

View File

@ -1,19 +0,0 @@
package net.lab1024.smartadmin.service.module.support.reload.interfaces;
import net.lab1024.smartadmin.service.module.support.reload.domain.entity.ReloadItem;
/**
* reload 接口<br>
* 需要reload的业务实现类
*/
@FunctionalInterface
public interface SmartReloadable {
/**
* reload
*
* @param reloadItem
* @return boolean
*/
boolean reload(ReloadItem reloadItem);
}

View File

@ -1,4 +1,4 @@
package net.lab1024.smartadmin.service.module.system.datascope;
package net.lab1024.smartadmin.service.module.system.datascope.anno;
import net.lab1024.smartadmin.service.module.system.datascope.constant.DataScopeTypeEnum;

View File

@ -1,7 +1,7 @@
package net.lab1024.smartadmin.service.module.system.datascope.service;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.smartadmin.service.module.system.datascope.DataScope;
import net.lab1024.smartadmin.service.module.system.datascope.anno.DataScope;
import net.lab1024.smartadmin.service.module.system.datascope.constant.DataScopeTypeEnum;
import net.lab1024.smartadmin.service.module.system.datascope.constant.DataScopeViewTypeEnum;
import net.lab1024.smartadmin.service.module.system.datascope.constant.DataScopeWhereInTypeEnum;
@ -40,7 +40,7 @@ public class DataScopeSqlConfigService {
@Autowired
private DataScopeViewService dataScopeViewService;
@Value("${swagger.packAge}")
@Value("${project.module}")
private String scanPackage;
/**

View File

@ -1,9 +1,9 @@
package net.lab1024.smartadmin.service.module.system.department;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.smartadmin.service.common.constant.CacheModuleBaseConst;
import net.lab1024.smartadmin.service.common.constant.CacheModuleConst;
import net.lab1024.smartadmin.service.module.support.beancache.key.CacheKey;
import net.lab1024.smartadmin.service.module.support.beancache.load.CacheLoad;
import net.lab1024.smartadmin.service.module.support.beancache.anno.CacheLoad;
import net.lab1024.smartadmin.service.module.system.department.domain.vo.DepartmentTreeVO;
import net.lab1024.smartadmin.service.module.system.department.domain.vo.DepartmentVO;
import org.springframework.beans.factory.annotation.Autowired;
@ -32,7 +32,7 @@ public class DepartmentCacheService {
*
* @return
*/
@CacheLoad(CacheModuleBaseConst.Department.DEPARTMENT_CACHE)
@CacheLoad(CacheModuleConst.Department.DEPARTMENT_CACHE)
public List<DepartmentVO> departmentCache() {
List<DepartmentVO> departmentVOList = departmentDao.listAll();
return departmentVOList;
@ -45,7 +45,7 @@ public class DepartmentCacheService {
*
* @return
*/
@CacheLoad(CacheModuleBaseConst.Department.DEPARTMENT_TREE_CACHE)
@CacheLoad(CacheModuleConst.Department.DEPARTMENT_TREE_CACHE)
public List<DepartmentTreeVO> departmentTreeCache() {
List<DepartmentVO> departmentVOList = departmentDao.listAll();
List<DepartmentTreeVO> treeList = departmentTreeService.buildTree(departmentVOList);
@ -60,7 +60,7 @@ public class DepartmentCacheService {
* @param cacheKey
* @return
*/
@CacheLoad(CacheModuleBaseConst.Department.DEPARTMENT_TREE_ID_CACHE)
@CacheLoad(CacheModuleConst.Department.DEPARTMENT_TREE_ID_CACHE)
public List<Long> departmentTreeCache(String cacheKey) {
String businessId = CacheKey.getBusinessIdByCacheKey(cacheKey);
Long departmentId = Long.valueOf(businessId);

View File

@ -1,7 +1,7 @@
package net.lab1024.smartadmin.service.module.system.department;
import net.lab1024.smartadmin.service.common.codeconst.ResponseCodeConst;
import net.lab1024.smartadmin.service.common.constant.CacheModuleBaseConst;
import net.lab1024.smartadmin.service.common.constant.CacheModuleConst;
import net.lab1024.smartadmin.service.common.constant.CommonConst;
import net.lab1024.smartadmin.service.common.domain.ResponseDTO;
import net.lab1024.smartadmin.service.module.support.beancache.cache.IBeanCache;
@ -54,7 +54,7 @@ public class DepartmentService {
* @return
*/
public ResponseDTO<List<DepartmentTreeVO>> departmentTree() {
String cacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Department.DEPARTMENT_TREE_CACHE);
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Department.DEPARTMENT_TREE_CACHE);
List<DepartmentTreeVO> treeVOList = beanCache.get(cacheKey);
return ResponseDTO.succData(treeVOList);
}
@ -66,7 +66,7 @@ public class DepartmentService {
* @return
*/
public List<Long> selfAndChildrenIdList(Long departmentId) {
String cacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Department.DEPARTMENT_TREE_ID_CACHE, departmentId.toString());
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Department.DEPARTMENT_TREE_ID_CACHE, departmentId.toString());
return beanCache.get(cacheKey);
}
@ -76,7 +76,7 @@ public class DepartmentService {
* @return
*/
public ResponseDTO<List<DepartmentEmployeeTreeVO>> departmentEmployeeTree() {
String cacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Department.DEPARTMENT_TREE_CACHE);
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Department.DEPARTMENT_TREE_CACHE);
List<DepartmentTreeVO> treeVOList = beanCache.get(cacheKey);
if (CollectionUtils.isEmpty(treeVOList)) {
return ResponseDTO.succData(Lists.newArrayList());
@ -385,14 +385,14 @@ public class DepartmentService {
* 清除自身以及下级的id列表缓存
*/
private void clearSelfAndChildrenIdCache() {
beanCache.removeByModule(CacheModuleBaseConst.Department.DEPARTMENT_TREE_ID_CACHE);
beanCache.removeByModule(CacheModuleConst.Department.DEPARTMENT_TREE_ID_CACHE);
}
/**
* 清除树结构缓存
*/
private void clearTreeCache() {
String treeCacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Department.DEPARTMENT_TREE_CACHE);
String treeCacheKey = CacheKey.cacheKey(CacheModuleConst.Department.DEPARTMENT_TREE_CACHE);
beanCache.remove(treeCacheKey);
}
@ -436,7 +436,7 @@ public class DepartmentService {
* @return
*/
public String getParentNameTreeByDepartmentId(Long departmentId) {
String cacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Department.DEPARTMENT_CACHE);
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Department.DEPARTMENT_CACHE);
List<DepartmentVO> departmentList = beanCache.get(cacheKey);
//递归寻找上级直到校区(第二级)
List<String> departmentNameList = Lists.newArrayList();
@ -468,7 +468,7 @@ public class DepartmentService {
* @return
*/
public Long getSchoolIdByEmployeeId(Long employeeId) {
String cacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Employee.SINGLE_EMPLOYEE_CACHE, employeeId.toString());
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.SINGLE_EMPLOYEE_CACHE, employeeId.toString());
EmployeeEntity employeeEntity = beanCache.get(cacheKey);
Long departmentId = employeeEntity.getDepartmentId();
DepartmentVO schoolIdByDepartment = this.getSchoolIdByDepartment(departmentId);
@ -485,7 +485,7 @@ public class DepartmentService {
* @return
*/
public DepartmentVO getSchoolIdByDepartment(Long departmentId) {
String cacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Department.DEPARTMENT_CACHE);
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Department.DEPARTMENT_CACHE);
List<DepartmentVO> departmentList = beanCache.get(cacheKey);
//递归寻找校区(第二级)
return this.recursionFindSchoolDepartmentId(departmentList, departmentId);

View File

@ -1,9 +1,9 @@
package net.lab1024.smartadmin.service.module.system.employee;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.smartadmin.service.common.constant.CacheModuleBaseConst;
import net.lab1024.smartadmin.service.common.constant.CacheModuleConst;
import net.lab1024.smartadmin.service.module.support.beancache.key.CacheKey;
import net.lab1024.smartadmin.service.module.support.beancache.load.CacheLoad;
import net.lab1024.smartadmin.service.module.support.beancache.anno.CacheLoad;
import net.lab1024.smartadmin.service.module.system.employee.domain.entity.EmployeeEntity;
import net.lab1024.smartadmin.service.module.system.role.roleemployee.RoleEmployeeDao;
import org.apache.commons.collections4.CollectionUtils;
@ -34,7 +34,7 @@ public class EmployeeCacheService {
* @param cacheKey
* @return
*/
@CacheLoad(CacheModuleBaseConst.Employee.DEPARTMENT_EMPLOYEE_CACHE)
@CacheLoad(CacheModuleConst.Employee.DEPARTMENT_EMPLOYEE_CACHE)
public List<EmployeeEntity> departmentEmployeeCache(String cacheKey) {
String businessId = CacheKey.getBusinessIdByCacheKey(cacheKey);
Long departmentId = Long.valueOf(businessId);
@ -48,7 +48,7 @@ public class EmployeeCacheService {
* @param cacheKey
* @return
*/
@CacheLoad(CacheModuleBaseConst.Employee.SINGLE_EMPLOYEE_CACHE)
@CacheLoad(CacheModuleConst.Employee.SINGLE_EMPLOYEE_CACHE)
public EmployeeEntity singleEmployeeCache(String cacheKey) {
String businessId = CacheKey.getBusinessIdByCacheKey(cacheKey);
Long employeeId = Long.valueOf(businessId);
@ -68,7 +68,7 @@ public class EmployeeCacheService {
* @param cacheKey
* @return
*/
@CacheLoad(CacheModuleBaseConst.Employee.SINGLE_EMPLOYEE_ROLE_CACHE)
@CacheLoad(CacheModuleConst.Employee.SINGLE_EMPLOYEE_ROLE_CACHE)
public List<Long> singleEmployeeRoleCache(String cacheKey) {
String businessId = CacheKey.getBusinessIdByCacheKey(cacheKey);
Long employeeId = Long.valueOf(businessId);

View File

@ -3,7 +3,7 @@ package net.lab1024.smartadmin.service.module.system.employee;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import net.lab1024.smartadmin.service.common.codeconst.ResponseCodeConst;
import net.lab1024.smartadmin.service.common.constant.CacheModuleBaseConst;
import net.lab1024.smartadmin.service.common.constant.CacheModuleConst;
import net.lab1024.smartadmin.service.common.constant.CommonConst;
import net.lab1024.smartadmin.service.common.domain.PageResultDTO;
import net.lab1024.smartadmin.service.common.domain.ResponseDTO;
@ -67,10 +67,10 @@ public class EmployeeService {
* @return
*/
public EmployeeLoginInfoDTO getById(Long employeeId) {
String cacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Employee.SINGLE_EMPLOYEE_CACHE, employeeId.toString());
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.SINGLE_EMPLOYEE_CACHE, employeeId.toString());
EmployeeEntity employeeEntity = beanCache.get(cacheKey);
//获取员工角色缓存
String roleCacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Employee.SINGLE_EMPLOYEE_ROLE_CACHE, employeeId.toString());
String roleCacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.SINGLE_EMPLOYEE_ROLE_CACHE, employeeId.toString());
List<Long> roleIdList = beanCache.get(roleCacheKey);
if (employeeEntity != null) {
Boolean isSuperman = menuEmployeeService.isSuperman(employeeId);
@ -90,10 +90,10 @@ public class EmployeeService {
* @return
*/
public EmployeeLoginBO getBoById(Long employeeId) {
String cacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Employee.SINGLE_EMPLOYEE_CACHE, employeeId.toString());
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.SINGLE_EMPLOYEE_CACHE, employeeId.toString());
EmployeeEntity employeeEntity = beanCache.get(cacheKey);
//获取员工角色缓存
String roleCacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Employee.SINGLE_EMPLOYEE_ROLE_CACHE, employeeId.toString());
String roleCacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.SINGLE_EMPLOYEE_ROLE_CACHE, employeeId.toString());
List<Long> roleIdList = beanCache.get(roleCacheKey);
if (employeeEntity != null) {
Boolean isSuperman = menuEmployeeService.isSuperman(employeeId);
@ -399,7 +399,7 @@ public class EmployeeService {
* @return
*/
public ResponseDTO<List<EmployeeVO>> getEmployeeByDeptId(Long departmentId) {
String cacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Employee.DEPARTMENT_EMPLOYEE_CACHE, departmentId.toString());
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.DEPARTMENT_EMPLOYEE_CACHE, departmentId.toString());
List<EmployeeEntity> employeeEntityList = beanCache.get(cacheKey);
if (CollectionUtils.isEmpty(employeeEntityList)) {
return ResponseDTO.succData(CommonConst.EMPTY_LIST);
@ -416,7 +416,7 @@ public class EmployeeService {
*/
public ResponseDTO<List<EmployeeVO>> getEmployeeBySchoolId(Long departmentId) {
// 查询部门下所有部门包含子部门
String cacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Department.DEPARTMENT_CACHE);
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Department.DEPARTMENT_CACHE);
List<DepartmentVO> departmentList = beanCache.get(cacheKey);
// 先查询本部门的员工
ResponseDTO<List<EmployeeVO>> employeeByDeptId = getEmployeeByDeptId(departmentId);
@ -465,9 +465,9 @@ public class EmployeeService {
* @param employeeId
*/
public void clearCacheByEmployeeId(Long employeeId) {
String cacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Employee.SINGLE_EMPLOYEE_CACHE, employeeId.toString());
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.SINGLE_EMPLOYEE_CACHE, employeeId.toString());
beanCache.remove(cacheKey);
String roleCacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Employee.SINGLE_EMPLOYEE_ROLE_CACHE, employeeId.toString());
String roleCacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.SINGLE_EMPLOYEE_ROLE_CACHE, employeeId.toString());
beanCache.remove(roleCacheKey);
}
@ -477,7 +477,7 @@ public class EmployeeService {
* @param departmentId
*/
private void clearCacheByDepartmentId(Long departmentId) {
String cacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Employee.DEPARTMENT_EMPLOYEE_CACHE, departmentId.toString());
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.DEPARTMENT_EMPLOYEE_CACHE, departmentId.toString());
beanCache.remove(cacheKey);
}

View File

@ -2,7 +2,7 @@ package net.lab1024.smartadmin.service.module.system.role.roleemployee;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.smartadmin.service.common.codeconst.ResponseCodeConst;
import net.lab1024.smartadmin.service.common.constant.CacheModuleBaseConst;
import net.lab1024.smartadmin.service.common.constant.CacheModuleConst;
import net.lab1024.smartadmin.service.common.domain.PageResultDTO;
import net.lab1024.smartadmin.service.common.domain.ResponseDTO;
import net.lab1024.smartadmin.service.module.support.beancache.cache.IBeanCache;
@ -149,7 +149,7 @@ public class RoleEmployeeService {
* @param employeeId
*/
public void clearCacheByEmployeeId(Long employeeId) {
String roleCacheKey = CacheKey.cacheKey(CacheModuleBaseConst.Employee.SINGLE_EMPLOYEE_ROLE_CACHE, employeeId.toString());
String roleCacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.SINGLE_EMPLOYEE_ROLE_CACHE, employeeId.toString());
beanCache.remove(roleCacheKey);
}
}

View File

@ -6,6 +6,7 @@ import lombok.extern.slf4j.Slf4j;
import net.lab1024.smartadmin.service.common.codeconst.ResponseCodeConst;
import net.lab1024.smartadmin.service.common.domain.PageResultDTO;
import net.lab1024.smartadmin.service.common.domain.ResponseDTO;
import net.lab1024.smartadmin.service.module.support.reload.core.anno.SmartReload;
import net.lab1024.smartadmin.service.module.system.systemconfig.domain.*;
import net.lab1024.smartadmin.service.util.SmartBaseEnumUtil;
import net.lab1024.smartadmin.service.util.SmartBeanUtil;
@ -39,6 +40,11 @@ public class SystemConfigService {
@Autowired
private SystemConfigDao systemConfigDao;
@SmartReload("system_config")
public void configReload(String param) {
this.initConfigCache();
}
/**
* 初始化系统设置缓存
*/

View File

@ -22,9 +22,9 @@ spring.jackson.time-zone=GMT+8
spring.jackson.serialization.write-dates-as-timestamps=false
######################### database #########################
spring.datasource.url=jdbc:p6spy:mysql://115.29.150.222:11024/smart_admin_v2?autoReconnect=true&useServerPreparedStmts=false&rewriteBatchedStatements=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai
spring.datasource.url=jdbc:p6spy:mysql://127.0.0.1:3306/smart_admin_v2?autoReconnect=true&useServerPreparedStmts=false&rewriteBatchedStatements=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=11024Lab
spring.datasource.password=root
spring.datasource.initial-size=2
spring.datasource.min-idle=1
spring.datasource.max-active=10
@ -40,14 +40,14 @@ spring.datasource.druid.service.scanner=net.lab1024.smartadmin.module..*Service.
######################### redis #######################################
spring.redis.database=1
spring.redis.host=115.29.150.222
spring.redis.host=127.0.0.1
spring.redis.lettuce.pool.max-active=100
spring.redis.lettuce.pool.min-idle=5
spring.redis.lettuce.pool.max-idle=10
spring.redis.lettuce.pool.max-wait=30000ms
spring.redis.port=21024
spring.redis.port=1234
spring.redis.timeout=10000ms
spring.redis.password=21024Lab
spring.redis.password=root
######################### swagger #########################
swagger.apiGroupName=smartAdmin