田源
2024-03-07 4b4083fd73dc27ece42f4835483565eef0e4f608
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.vci.server.portal.cache;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.apache.commons.lang3.StringUtils;
 
import com.vci.server.cache.redis.RedisUtil;
import com.vci.server.portal.entity.PLPageDefinationEntity;
import com.vci.server.portal.service.PLPageDefinationEntityService;
import com.vci.server.portal.tools.ServerTool;
import com.alibaba.fastjson.JSONObject;
import com.vci.corba.common.VCIError;
import com.vci.corba.portal.data.PLPageDefination;
 
public class ComponentCacheUtil extends UICacheBaseUtil<PLPageDefination>{
 
    private static volatile ComponentCacheUtil instance = null;
    
    public static void initCache() {
        try {
            getInstance().initPLPageDefination();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
    
    private ComponentCacheUtil() {
        super(UICacheNames.PAGECOMPONENT, PLPageDefination.class);
    }
    
    public static ComponentCacheUtil getInstance() {
        if (instance == null) {
            synchronized (ComponentCacheUtil.class) {
                if (instance == null) {
                    instance = new ComponentCacheUtil();
                }
            }
        }
        
        return instance;
    }
    
    
    @Override
    public void setObject(PLPageDefination obj)    {
        if (obj == null)
            return;
        
        String jsonObj = JSONObject.toJSONString(obj);
        
        RedisUtil.getInstance().hset(getName(), obj.plOId, jsonObj);
    }
    
    @Override
    public PLPageDefination[] getObjects() {
        List<PLPageDefination> list = getObjectList();
        
        return list.toArray(new PLPageDefination[0]);
    }
 
    
    @Override
    public void delObject(PLPageDefination obj) {
        if (obj == null || StringUtils.isBlank(obj.plOId))
            return;
        
        RedisUtil.getInstance().hdel(getName(), obj.plOId);
    }
//
//    private void initPLPageDefination() throws Throwable{
//        lstPageDef.clear();
//        mapTab2PageDefs.clear();
//        mapID2PageDef.clear();
//        System.gc();
//        
//        List<PLPageDefinationEntity> list = PLPageDefinationEntityService.getInstance().getAllPLPageDefinations();
//        for(int i = 0; i < list.size(); i++){
//            PLPageDefinationEntity obj = list.get(i);
//            PLPageDefination pageDef = ServerTool.getPLPageDefination(obj);
//            if(pageDef != null){
//                lstPageDef.add(pageDef);
//                mapID2PageDef.put(pageDef.plOId, pageDef);
//                
//                List<PLPageDefination> pageDefinationList = null;
//                if (mapTab2PageDefs.get(pageDef.plPageContextOId) == null) {
//                    pageDefinationList = new ArrayList<PLPageDefination>(); 
//                    mapTab2PageDefs.put(pageDef.plPageContextOId, pageDefinationList);
//                } else {
//                    pageDefinationList = mapTab2PageDefs.get(pageDef.plPageContextOId); 
//                }
//                pageDefinationList.add(pageDef);
//            }
//        }
//    }
//    
    private void initPLPageDefination() throws Throwable{
        RedisUtil.getInstance().del(getName());
        
        List<PLPageDefinationEntity> listEnt = PLPageDefinationEntityService.getInstance().getAllPLPageDefinations();
 
        Map<String, List<String>> mapTemp = new HashMap<String, List<String>>();
 
        for(PLPageDefinationEntity btn : listEnt){
            PLPageDefination pageDef = ServerTool.getPLPageDefination(btn);
 
            if(pageDef != null){
                setObject(pageDef);
            }
            List<String> lstTemp = mapTemp.get(pageDef.plTabPageOId);
            if (lstTemp == null) {
                lstTemp = new ArrayList<String>();
                mapTemp.put(pageDef.plTabPageOId, lstTemp);
            }
            lstTemp.add(pageDef.plOId);
        }
        
        for (String key : mapTemp.keySet()) {
            List<String> lstTemp = mapTemp.get(key);
            RedisUtil.getInstance().hset(getName(), "MAP-" + key, String.join(";", lstTemp));
        }
    }
    
    public PLPageDefination[] getComopnent(String tabId) throws VCIError {
        String temp = RedisUtil.getInstance().hget(getName(), "MAP-" + tabId);
        if (StringUtils.isBlank(temp))
            return new PLPageDefination[0];
        
        String oids[] = temp.split(";");
        
        List<PLPageDefination> lstObj = new ArrayList<PLPageDefination>();
        
        for (String oid : oids) {
            PLPageDefination ap = getObject(oid);
            if (ap != null)
                lstObj.add(ap);
        }
        
        return lstObj.toArray(new PLPageDefination[0]);
    }
    
}