1
zj
2025-06-23 dc9bd22833255bc602dd42c7f603ecb50842ab35
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
package kernel.cache;
 
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
 
import project.redis.RedisHandler;
 
@SuppressWarnings("unchecked")
public class RedisLocalCache {
 
    private ConcurrentHashMap<String, Object> cache = new ConcurrentHashMap<String, Object>();
    
    private RedisHandler redisHandler;
 
    public Object get(String key) {
        Object obj = cache.get(key);
        if (obj != null) {
            return obj;
        }
        obj = redisHandler.get(key);
        if (obj != null) {
            cache.put(key, obj);
            return obj;
        }
        return null;
    }
    
    public <V> HashMap<String,V> getMap(Set<String> keys) {
        HashMap<String,V> resultMap=new HashMap<String,V>();
        
        HashSet<String> noValueKeys=new HashSet<String>();
        for(String key:keys) {
            Object obj = cache.get(key);
            if(null==obj) {
                noValueKeys.add(key);
            }else {
                resultMap.put(key,(V)obj);
            }
        }
        
        if(noValueKeys.isEmpty()) return resultMap;
        
        HashMap<String,V> redisResultMap=redisHandler.getMap(noValueKeys);
        if(null==redisResultMap || redisResultMap.isEmpty()) return resultMap;
        
        resultMap.putAll(redisResultMap);
        
        return resultMap;
    }
 
    public void put(String key, Object obj) {
        cache.put(key, obj);
    }
    
    public void putAll(Map<String,Object> keyValMaps) {
        cache.putAll(keyValMaps);
    }
 
    public void setRedisHandler(RedisHandler redisHandler) {
        this.redisHandler = redisHandler;
    }
 
}