kimi
2020-05-18 c8aee7b9bfd79cfd741d7e5692520f4f51a31a86
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.highdatas.mdm.util;
 
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
 
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
 
@Component
public class RedisClient {
    long timeout = 30 * 60;
    private RedisClient() {
 
    }
 
    public boolean putRedisVal(String key, String value, long timeout) {
        try {
            RedisUtil.set(getRealRedisKey(key), value, timeout);
        }
        catch (Exception e) {
            return false;
        }
 
        return true;
    }
 
    public boolean putRedisVal(String key, String value) {
        try {
            if (value == null || StringUtils.isEmpty(value)) {
                return true;
            }
            RedisUtil.set(getRealRedisKey(key), value, timeout);
            return true;
        }
        catch (Exception e){
            e.printStackTrace();
            return false;
        }
 
    }
 
    public boolean putRedisValObj(String key, Object value) {
        try {
            if (value == null || StringUtils.isEmpty(value.toString())) {
                return true;
            }
            RedisUtil.set(getRealRedisKey(key), value, timeout);
            return true;
        }
        catch (Exception e){
            e.printStackTrace();
            return false;
        }
 
    }
 
    public String getRedisTimeOutVal(String key) {
        try{
            Object o = RedisUtil.get(getRealRedisKey(key));
            if (o == null) {
                return null;
            } else {
                return o.toString();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    public String getRedisVal(String key) {
        try {
            Object o = RedisUtil.get(getRealRedisKey(key));
            if (o == null) {
                return null;
            } else {
                RedisUtil.expire(getRealRedisKey(key), timeout, TimeUnit.MINUTES);
                return o.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public Object getRedisValObj(String key) {
        try {
            Object o = RedisUtil.get(getRealRedisKey(key));
            if (o == null) {
                return null;
            } else {
                RedisUtil.expire(getRealRedisKey(key), timeout, TimeUnit.MINUTES);
                return o;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    public boolean deleteRedisVal(String key) {
        try {
            boolean del = RedisUtil.del(getRealRedisKey(key));
            return del;
        }
        catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
 
    public boolean putRedisList(String key, List values) {
        try {
            if (values == null || values.isEmpty()) {
                return true;
            }
            RedisUtil.lPushAll(getRealRedisKey(key),values);
            RedisUtil.expire(getRealRedisKey(key), timeout, TimeUnit.MINUTES);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
 
    }
 
    public List<Object> getRedisList(String key) {
        try {
            List<Object> objects = RedisUtil.lGet(getRealRedisKey(key), 0, -1);
            if (objects == null || objects.isEmpty()) {
                return null;
            } else {
                RedisUtil.expire(getRealRedisKey(key), timeout, TimeUnit.MINUTES);
                return objects;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    public static String getRealRedisKey(String key) {
        String s = DbUtils.StrJoinLink(Constant.UnderLine, Constant.MainData, key);
 
        return s;
    }
 
    public void delByCharacter(String characterId) {
        String realRedisKey = getRealRedisKey(characterId);
        Set<String> keys = RedisUtil.getKeys(realRedisKey);
        if (keys != null && !keys.isEmpty()) {
            RedisUtil.del(keys);
        }
 
    }
 
    public void delByField() {
        Set<String> keys = RedisUtil.getKeys("*field");
        RedisUtil.del(keys);
    }
}