mirror of
https://gitee.com/ssssssss-team/magic-api.git
synced 2026-06-09 10:23:53 +08:00
优化加载速度
This commit is contained in:
@@ -119,6 +119,9 @@ public class DatabaseResource extends KeyValueResource {
|
||||
|
||||
@Override
|
||||
public Set<String> keys() {
|
||||
if(!cachedContent.isEmpty()){
|
||||
return cachedContent.keySet().stream().filter(it -> it.startsWith(this.path) && !it.equals(this.path)).collect(Collectors.toSet());
|
||||
}
|
||||
String sql = String.format("select file_path from %s where file_path like '%s%%'", tableName, isDirectory() ? this.path : (this.path + separator));
|
||||
return new HashSet<>(template.queryForList(sql, String.class));
|
||||
}
|
||||
|
||||
@@ -52,6 +52,8 @@ public interface GroupServiceProvider {
|
||||
*/
|
||||
List<Group> groupList(String type);
|
||||
|
||||
List<Group> cachedGroupList(String type);
|
||||
|
||||
/**
|
||||
* 根据分组Id获取分组路径
|
||||
*/
|
||||
|
||||
@@ -169,16 +169,20 @@ public abstract class StoreServiceProvider<T extends MagicEntity> {
|
||||
Map<String, Resource> mappings = new HashMap<>();
|
||||
Map<String, T> infos = new HashMap<>();
|
||||
List<T> result = resources.stream().map(r -> {
|
||||
T info = deserialize(r.read());
|
||||
T info = (T) deserialize(r.read()).clone();
|
||||
infos.put(info.getId(), info);
|
||||
mappings.put(info.getId(), r);
|
||||
return (T) info.clone();
|
||||
return info ;
|
||||
}).collect(Collectors.toList());
|
||||
this.mappings = mappings;
|
||||
this.infos = infos;
|
||||
return result;
|
||||
}
|
||||
|
||||
public List<T> cachedList(){
|
||||
return new ArrayList<>(this.infos.values());
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询详情(主要给页面使用)
|
||||
*
|
||||
|
||||
@@ -111,6 +111,10 @@ public class DefaultGroupServiceProvider implements GroupServiceProvider {
|
||||
public List<Group> groupList(String type) {
|
||||
Resource resource = this.workspace.getDirectory(Constants.GROUP_TYPE_API.equals(type) ? Constants.PATH_API : Constants.PATH_FUNCTION);
|
||||
resource.readAll();
|
||||
return getGroupList(resource);
|
||||
}
|
||||
|
||||
private List<Group> getGroupList(Resource resource){
|
||||
return resource.dirs().stream().map(it -> it.getResource(Constants.GROUP_METABASE)).filter(Resource::exists)
|
||||
.map(it -> {
|
||||
Group group = JsonUtils.readValue(it.read(), Group.class);
|
||||
@@ -120,6 +124,12 @@ public class DefaultGroupServiceProvider implements GroupServiceProvider {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Group> cachedGroupList(String type) {
|
||||
Resource resource = this.workspace.getDirectory(Constants.GROUP_TYPE_API.equals(type) ? Constants.PATH_API : Constants.PATH_FUNCTION);
|
||||
return getGroupList(resource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFullPath(String groupId) {
|
||||
StringBuilder path = new StringBuilder();
|
||||
|
||||
@@ -210,7 +210,7 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
|
||||
|
||||
@Override
|
||||
public List<ApiInfo> apiList() {
|
||||
return apiServiceProvider.list();
|
||||
return apiServiceProvider.cachedList();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -276,7 +276,7 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
|
||||
|
||||
@Override
|
||||
public List<FunctionInfo> functionList() {
|
||||
return functionServiceProvider.list();
|
||||
return functionServiceProvider.cachedList();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -387,7 +387,7 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
|
||||
|
||||
@Override
|
||||
public List<Group> groupList(String type) {
|
||||
return groupServiceProvider.groupList(type);
|
||||
return groupServiceProvider.cachedGroupList(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -703,7 +703,7 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
|
||||
|
||||
private boolean processApiNotify(String id, int action) {
|
||||
// 刷新缓存
|
||||
this.apiList();
|
||||
apiServiceProvider.listWithScript();
|
||||
if (action == NOTIFY_ACTION_DELETE) {
|
||||
mappingHandlerMapping.unregisterMapping(id, true);
|
||||
} else {
|
||||
@@ -714,7 +714,7 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
|
||||
|
||||
private boolean processFunctionNotify(String id, int action) {
|
||||
// 刷新缓存
|
||||
this.functionList();
|
||||
functionServiceProvider.listWithScript();
|
||||
if (action == NOTIFY_ACTION_DELETE) {
|
||||
magicFunctionManager.unregister(id);
|
||||
} else {
|
||||
@@ -757,12 +757,12 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
|
||||
treeNode = magicFunctionManager.findGroupTree(id);
|
||||
magicFunctionManager.deleteGroup(treeNode.flat().stream().map(Group::getId).collect(Collectors.toList()));
|
||||
// 刷新函数缓存
|
||||
this.functionList();
|
||||
functionServiceProvider.listWithScript();
|
||||
} else {
|
||||
// 删除接口分组
|
||||
mappingHandlerMapping.deleteGroup(treeNode.flat().stream().map(Group::getId).collect(Collectors.toList()));
|
||||
// 刷新接口缓存
|
||||
this.apiList();
|
||||
apiServiceProvider.listWithScript();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user