【发布时间】:2018-10-09 07:28:48
【问题描述】:
Service 在构造函数中从 DB 中获取数据并将其存储在 HashMap 中,然后从 HashMap 返回数据。请看:
@RestController
@RequestMapping("/scheduler/api")
@Transactional(readOnly = true, transactionManager = "cnmdbTm")
public class RestApiController {
private final Set<String> cache;
@Autowired
public RestApiController(CNMDBFqdnRepository cnmdbRepository, CNMTSFqdnRepository cnmtsRepository) {
cache = new HashSet<>();
cache.addAll(getAllFqdn(cnmdbRepository.findAllFqdn()));
cache.addAll(getAllFqdn(cnmtsRepository.findAllFqdn()));
}
@RequestMapping(value = "/fqdn", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public List<SchedulerRestDto> checkFqdn(@RequestBody List<SchedulerRestDto> queryList) throws ExecutionException {
for (SchedulerRestDto item : queryList) {
item.setFound(1);
if (!cache.contains(item.getFqdn())) {
item.setFound(0);
}
}
return queryList;
}
private Set<String> getAllFqdn(List<String> fqdnList) {
Set<String> result = new HashSet<>();
for (String fqdn : fqdnList) {
result.add(fqdn);
}
return result;
}
}
但我总是在大约 2 秒内得到结果。我认为从 DB 获得的 35K 字符串有点慢。
我试图找出问题所在。我将序列化的HashMap 存储到文件中,并将构造函数修改为:
@Autowired
public RestApiController(CNMDBFqdnRepository cnmdbRepository, CNMTSFqdnRepository cnmtsRepository) {
try (final InputStream fis = getResourceAsStream("cache-hashset.ser");
final ObjectInputStream ois = new ObjectInputStream(fis)) {
cache = (Set<String>) ois.readObject();
}
}
该服务在不到 100 毫秒后返回结果。
我认为它与 DB 有关,但我不知道如何修复它。
有什么想法吗?
【问题讨论】:
标签: java rest spring-boot spring-data