[测评系统]--测评系统核心代码库
linzhijie
2021-03-11 84fea994d2db7dc313ad1774f34eb12a45f8d6e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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;
    }
}