-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRedisUtils.java
More file actions
266 lines (235 loc) · 6.65 KB
/
RedisUtils.java
File metadata and controls
266 lines (235 loc) · 6.65 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import com.huawei.common.constants.PathCons;
import com.huawei.common.constants.RedisCons;
import com.huawei.jredis.client.GlobalConfig;
import com.huawei.jredis.client.auth.AuthConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class RedisUtils {
private final static Log LOG = LogFactory.getLog(RedisUtils.class.getName());
private static RedisUtils redisUtils;
//kerberos用户名
private static String PRNCIPAL_NAME;
private static String PATH_TO_KEYTAB = PathCons.PATH_TO_KEYTAB;
private static String PATH_TO_KRB5_CONF = PathCons.PATH_TO_KRB5_CONF;
private static JedisCluster client = null;
private RedisUtils() {
}
public static RedisUtils getInstance(String kerberosUser) {
PRNCIPAL_NAME = kerberosUser;
if (redisUtils == null) {
login();
initClient();
return new RedisUtils();
} else {
return redisUtils;
}
}
private static void initClient() {
Set<HostAndPort> hosts = new HashSet<HostAndPort>();
// hosts.add(new HostAndPort(RedisCons.IP_1, RedisCons.PORT_1));
hosts.add(new HostAndPort(RedisCons.IP_2, RedisCons.PORT_2));
// hosts.add(new HostAndPort(RedisCons.IP_3, RedisCons.PORT_3));
client = new JedisCluster(hosts, 5000);
}
private static void login() {
String userdir = System.getProperty("user.dir") + File.separator + "conf" + File.separator;
System.setProperty("java.security.krb5.conf", PATH_TO_KRB5_CONF);
AuthConfiguration authConfiguration = new AuthConfiguration(PATH_TO_KEYTAB, PRNCIPAL_NAME);
GlobalConfig.setAuthConfiguration(authConfiguration);
}
/**
* 设置字符串类型
*
* @param key
* @param value
*/
public void setString(String key, String value) {
client.set(key, value);
LOG.info("key:" + key + " value: " + value + " has been set");
}
/**
* 根据key获取值。不存在返回null
*
* @param key
* @return
*/
public String getString(String key) {
return client.get(key);
}
/**
* 追加字符串
*
* @param key
* @param value 追加内容
*/
public void appendString(String key, String value) {
Long append = client.append(key, value);
if (append == 0) {
LOG.warn("key:" + key + " is not exist");
} else {
LOG.info("key:" + key + " has append value, After append value: " + client.get(key));
}
}
/**
* 删除key
*
* @param key
*/
public void deleteKey(String key) {
Long del = client.del(key);
if (del == 0) {
LOG.warn("key:" + key + " is not exist");
} else {
LOG.info("key:" + key + " has been delete");
}
}
/**
* 添加数据到List
*
* @param key
* @param values
*/
public void putDataList(String key, String... values) {
Long rpush = client.rpush(key, values);
if (rpush == 0) {
LOG.warn("key:" + key + " is not exist");
} else {
LOG.info(values + " have been added to " + key);
}
}
/**
* 获取所有list数据
*
* @param key
* @return
*/
public List<String> getList(String key) {
List<String> list = client.lrange(key, 0, -1);
for (String s : list) {
LOG.info("list data is " + s);
}
return list;
}
/**
* 获取List类型数据的个数
*
* @param key
* @return
*/
public long getListCount(String key) {
Long len = client.llen(key);
LOG.info("Message count: " + len);
return len;
}
/**
* 创建Hash添加数据
*
* @param key
* @param field
* @param value
*/
public void putDataHash(String key, String field, String value) {
Long hset = client.hset(key, field, value);
LOG.info("data field:" + field + " value:" + value + " have been added to " + key);
}
/**
* 获取所有Hash值
*
* @param key
* @return
*/
public Map<String, String> getAllDataFromHash(String key) {
Map<String, String> map = client.hgetAll(key);
LOG.info("data are" + map);
return map;
}
/**
* 根据key获取Hash类型field值
*
* @param key
* @param fieldName
* @return
*/
public String getFieldDataHash(String key, String fieldName) {
String value = client.hget(key, "id");
LOG.info("value:" + value);
return value;
}
/**
* 根据key获取Hash类型所有key
*
* @param key
* @return
*/
public Set<String> getHashKeys(String key) {
Set<String> hkeys = client.hkeys(key);
LOG.info(hkeys);
return hkeys;
}
/**
* 根据key获取Hash类型所有value
*
* @param key
* @return
*/
public List<String> getHashVals(String key) {
List<String> hvals = client.hvals(key);
LOG.info(hvals);
return hvals;
}
/**
* 为set添加数据
* @param key
* @param members
*/
public void putDataSet(String key, String... members) {
client.sadd(key, members);
LOG.info("data have been added to " + key);
}
/**
* 根据key获取set
* @param key
* @return
*/
public Set<String> getDataSet(String key) {
Set<String> sets = client.smembers(key);
LOG.info("Set: " + sets);
return sets;
}
/**
* 给sortedSet类型添加数据
* @param key
* @param score 权重
* @param value
*/
public void putDataSortedSet(String key, double score, String value) {
client.zadd(key, score, value);
LOG.info("data " + value + " have been add to sortedset");
}
public Set<String> getAllDataSortedSet(String key) {
Set<String> setValues = client.zrange(key, 0, -1);
LOG.info("All values: " + setValues);
return setValues;
}
/**
* 为key设置过期时间
*
* @param key
* @param seconds
*/
public void setExpiredTime(String key, int seconds) {
Long expire = client.expire(key, seconds);
if (expire == 0) {
LOG.warn("key:" + key + " is not exist");
} else {
LOG.info("key:" + key + " has been set expire time to " + seconds + " seconds");
}
}
}