[测评系统]--测评系统核心代码库
linzhijie
2021-07-12 3392e903116c7a754298618ce7b75cfd9c0b2a41
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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() {
    }
 
    /**
     * 获取缓存实例
     * @return
     */
    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);
    }
 
    /**
     * 获取所有报告缓存字典
     * @return
     */
    public List<TLibraryCode> getMAQReportLibrary() {
        List<TLibraryCode> maqReportLibraries = lruCache.get(MAQ_REPORT_LIBRARY);
        if (Objects.isNull(maqReportLibraries)) {
            maqReportLibraries = loadExcel();
        }
        return maqReportLibraries;
    }
 
    /**
     * 根据报告字典ID获取报告字典
     * @param coreId
     * @return
     */
    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;
    }
 
    /**
     * 获取所有报告字典 加入缓存
     * @return
     */
    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;
    }
}