Browse Source

增加缓存,优化性能

main
zhouxin5253@163.com 4 years ago
parent
commit
9cd0c8b3f0
  1. 48
      src/main/java/com/github/zxbu/webdavteambition/store/TeambitionClientService.java

48
src/main/java/com/github/zxbu/webdavteambition/store/TeambitionClientService.java

@ -15,7 +15,6 @@ import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.*; import java.util.*;
@ -29,6 +28,7 @@ public class TeambitionClientService {
private static int chunkSize = 10485760; // 10MB private static int chunkSize = 10485760; // 10MB
private TFile rootTFile = null; private TFile rootTFile = null;
private Map<String, TFile> nodeIdMap = new ConcurrentHashMap<>(); private Map<String, TFile> nodeIdMap = new ConcurrentHashMap<>();
private Map<String, Set<TFile>> tFilesCache = new ConcurrentHashMap<>();
private final TeambitionClient client; private final TeambitionClient client;
@ -38,28 +38,31 @@ public class TeambitionClientService {
} }
public Set<TFile> getTFiles(String nodeId) { public Set<TFile> getTFiles(String nodeId) {
NodeQuery nodeQuery = new NodeQuery(); return tFilesCache.computeIfAbsent(nodeId, key -> {
nodeQuery.setOrgId(client.getOrgId()); NodeQuery nodeQuery = new NodeQuery();
nodeQuery.setOffset(0); nodeQuery.setOrgId(client.getOrgId());
nodeQuery.setLimit(10000); nodeQuery.setOffset(0);
nodeQuery.setOrderBy("updateTime"); nodeQuery.setLimit(10000);
nodeQuery.setOrderDirection("desc"); nodeQuery.setOrderBy("updateTime");
nodeQuery.setDriveId(client.getDriveId()); nodeQuery.setOrderDirection("desc");
nodeQuery.setSpaceId(client.getSpaceId()); nodeQuery.setDriveId(client.getDriveId());
nodeQuery.setParentId(nodeId); nodeQuery.setSpaceId(client.getSpaceId());
String json = client.get("/pan/api/nodes", toMap(nodeQuery)); nodeQuery.setParentId(nodeId);
ListResult<TFile> tFileListResult = JsonUtil.readValue(json, new TypeReference<ListResult<TFile>>() { String json = client.get("/pan/api/nodes", toMap(nodeQuery));
}); ListResult<TFile> tFileListResult = JsonUtil.readValue(json, new TypeReference<ListResult<TFile>>() {
List<TFile> tFileList = tFileListResult.getData(); });
tFileList.sort(Comparator.comparing(TFile::getUpdated).reversed()); List<TFile> tFileList = tFileListResult.getData();
Set<TFile> tFileSets = new LinkedHashSet<>(); tFileList.sort(Comparator.comparing(TFile::getUpdated).reversed());
for (TFile tFile : tFileList) { Set<TFile> tFileSets = new LinkedHashSet<>();
if (!tFileSets.add(tFile)) { for (TFile tFile : tFileList) {
LOGGER.info("当前目录下{} 存在同名文件:{},文件大小:{}", nodeId, tFile.getName(), tFile.getSize()); if (!tFileSets.add(tFile)) {
LOGGER.info("当前目录下{} 存在同名文件:{},文件大小:{}", nodeId, tFile.getName(), tFile.getSize());
}
} }
} // 对文件名进行去重,只保留最新的一个
// 对文件名进行去重,只保留最新的一个 return tFileSets;
return tFileSets; });
} }
@ -310,5 +313,6 @@ public class TeambitionClientService {
private void clearCache(String path) { private void clearCache(String path) {
nodeIdMap.remove(path); nodeIdMap.remove(path);
tFilesCache.clear();
} }
} }

Loading…
Cancel
Save