ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
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
package com.vci.server.portal.cache;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
import org.apache.commons.lang3.StringUtils;
 
import com.alibaba.fastjson.JSONObject;
import com.vci.server.cache.redis.RedisUtil;
 
public abstract class UICacheBaseUtil<T> {//extends BaseCacheTimer {
    protected static String TYPEMAP = "MAP-";
    
    private String _cacheName = null;
    private Class<T> _classType = null;
 
    
    protected UICacheBaseUtil(String objType, Class<T> classType){
        _cacheName = objType;
        //super(objType);
        
        _classType = classType;
    }
    
    protected String getName() {
        return _cacheName;
    }
    
 
    protected List<T> getObjectList(){
        List<T> lstObj = new ArrayList<T>();
        
        Map<String, String> map = RedisUtil.getInstance().hgetAll(getName());
        
        String[] keys = map.keySet().toArray(new String[0]);
        for (int i = 0; i < keys.length; i++) {
            if (keys[i].startsWith(TYPEMAP))
                continue;
            
            String json = map.get(keys[i]);
            
            if (StringUtils.isBlank(json))
                continue;
            try {
                T obj = JSONObject.parseObject(json, _classType);
                lstObj.add(obj);
            } catch (Exception e) {
                //System.out.println(json);
                e.printStackTrace();
            }
        }
        
        return lstObj;        
    }
    
    public T getObject(String oid){
        if (StringUtils.isBlank(oid))
            return null;
 
        String jsonObj = RedisUtil.getInstance().hget(getName(), oid);
        if (StringUtils.isBlank(jsonObj))
            return null;
        
        return JSONObject.parseObject(jsonObj, _classType);    
    }
    
//    public T getObjectById(String oid) {
//        if (StringUtils.isBlank(oid))
//            return null;
//        
//        String jsonObj = RedisUtil.getInstance().hget(getName(), oid);
//        if (StringUtils.isBlank(jsonObj))
//            return null;
//        
//        return JSONObject.parseObject(jsonObj, _classType);
//    }
    
    
    public void delItem(String name){
        if (StringUtils.isBlank(name))
            return;
        
        RedisUtil.getInstance().hdel(getName(), name.toLowerCase());
    }
    
    public abstract T[] getObjects();
    public abstract void delObject(T obj);
    public abstract void setObject(T obj);
}