package com.ots.project.tool;
|
import cn.hutool.cache.Cache;
|
import cn.hutool.cache.CacheUtil;
|
import com.ots.project.exam.domain.TLibraryCode;
|
import com.ots.project.exam.service.impl.TLibraryCodeServiceImpl;
|
import lombok.extern.slf4j.Slf4j;
|
import java.util.List;
|
import java.util.Objects;
|
import java.util.stream.Collectors;
|
|
@Slf4j
|
public class CacheServiceFactory {
|
private static final String MAQ_REPORT_LIBRARY = "MAQ_REPORT_LIBRARY";
|
volatile private static CacheServiceFactory instance;
|
private static Cache<String, List<TLibraryCode>> lruCache = CacheUtil.newLRUCache(5);
|
private CacheServiceFactory() {
|
}
|
|
public static CacheServiceFactory getInstance() {
|
if (Objects.isNull(instance)) {
|
synchronized (CacheServiceFactory.class) {
|
if (Objects.isNull(instance)) {
|
instance = new CacheServiceFactory();
|
}
|
}
|
}
|
return instance;
|
}
|
public void reLoad() {
|
lruCache.clear();
|
lruCache.put(MAQ_REPORT_LIBRARY, null);
|
}
|
public List<TLibraryCode> getMAQReportLibrary() {
|
List<TLibraryCode> maqReportLibraries = lruCache.get(MAQ_REPORT_LIBRARY);
|
if (Objects.isNull(maqReportLibraries)) {
|
maqReportLibraries = loadExcel();
|
}
|
return maqReportLibraries;
|
}
|
public TLibraryCode getMAQReportLibrary(String coreId) {
|
List<TLibraryCode> maqReportLibrary = getMAQReportLibrary();
|
TLibraryCode library = null;
|
try {
|
List<TLibraryCode> collect = maqReportLibrary.stream().filter(item -> Objects.equals(item.getLibraryCode(), coreId)).collect(Collectors.toList());
|
if (collect.isEmpty()) {
|
log.error("未加载到Report_Library 参数coreId={} ", coreId);
|
return library;
|
}
|
library = collect.get(0);
|
} catch (Exception ex) {
|
log.error("加载MAQ_Report_Library模板参数coreId={} 失败:{}", coreId, ex);
|
}
|
return library;
|
}
|
private List<TLibraryCode> loadExcel() {
|
TLibraryCodeServiceImpl itLibraryCodeService = AppUtil.getObject("iTLibraryCodeService", TLibraryCodeServiceImpl.class);
|
TLibraryCode libraryCode = new TLibraryCode();
|
List<TLibraryCode> userList = itLibraryCodeService.selectTLibraryCodeList(libraryCode);
|
if (Objects.isNull(userList) && userList.size() == 0) {
|
return null;
|
}
|
lruCache.put(MAQ_REPORT_LIBRARY, userList);
|
return userList;
|
}
|
}
|