yuxc
2025-01-15 2bea732496b4f5051233ed94e206160992351596
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
package com.vci.server.framework.cache;
 
import java.util.List;
 
import org.apache.commons.lang3.StringUtils;
import org.hibernate.HibernateException;
 
import com.alibaba.fastjson.JSONObject;
import com.vci.common.log.ServerWithLog4j;
import com.vci.corba.framework.data.PvolumeInfo;
import com.vci.server.base.persistence.dao.HibernateCallback;
import com.vci.server.base.persistence.dao.HibernateTemplate;
import com.vci.server.cache.CacheNames;
import com.vci.server.cache.VolumeCacheProvider;
import com.vci.server.cache.redis.RedisUtil;
import com.vci.server.framework.volume.dao.Pvolume;
import com.vci.server.framework.volume.dao.PvolumeDaoImp;
 
 
public class VolumeCatch {
    private static VolumeCatch _instance = null;
//    
//    private static String CACHENAME = "VOLUME"; 
//    
    private static VolumeCatch getInstance(){
        if (_instance == null){
            _instance = new VolumeCatch();
        }
        
        return _instance;
    }
    
//    
//    private VolumeCatch(){
//        super(CACHENAME);
//        InitCatch();
//    }
 
    
    public synchronized static void InitCatch()
    {
        clearCatch();
        
        getInstance().InitVolumes();
    }
    
 
    public static void clearCatch() {
        RedisUtil.getInstance().del(CacheNames.VOLUMES);
    }
    
//    public synchronized static void refreshCache() {
//        try {
//            clearCatch();
//            InitCatch();
//        }catch (Throwable e) {
//            ServerWithLog4j.logger.error("VolumeCatch-refreshCache", e);
//            //throw getLocalVciError("P0010PLMAP-00012", e);
//        }
//    }
    
    public static void setVolume(PvolumeInfo item) {
        if (item == null)
            return;
        
        String jsonObj = JSONObject.toJSONString(item);
        
        RedisUtil.getInstance().hset(CacheNames.VOLUMES, item.name.toLowerCase(), jsonObj);
    }
    
 
    public static void delVolume(String vol) {
        if (StringUtils.isBlank(vol))
            return;
        
        RedisUtil.getInstance().hdel(CacheNames.VOLUMES, vol.toLowerCase());
    }
 
    
    @SuppressWarnings("unchecked")
    private void InitVolumes() {
        List<Pvolume> lstVolume = (List<Pvolume>) new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                PvolumeDaoImp impl = new PvolumeDaoImp();
                String hsql = "from Pvolume p order by p.name";
                return impl.findEntities(hsql);
            }
        });
        
        for (Pvolume item : lstVolume){
            setVolume(changePvolumeToPvolumeInfo(item));
        }
    }
    
    private PvolumeInfo changePvolumeToPvolumeInfo(Pvolume pvolume) {
        PvolumeInfo pvoInfo = new PvolumeInfo();
        pvoInfo.id = pvolume.getId() == null ? "" : pvolume.getId();
        pvoInfo.name = pvolume.getName() == null ? "" : pvolume.getName();
        pvoInfo.service = pvolume.getService() == null ? "" : pvolume.getService();
        pvoInfo.host = pvolume.getHost() == null ? "" : pvolume.getHost();
        pvoInfo.type = (short)(pvolume.getType() == 0 ? 0 : 1);
        pvoInfo.path = pvolume.getPath() == null ? "" : pvolume.getPath();
        pvoInfo.isvalid = pvolume.getIsvalid();
        return pvoInfo;
    }
}