RedisUtils.java
6.34 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
package com.jevon.utils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import javax.annotation.PostConstruct;
/**
* @author chen
* @version 1.0
* @date 2019/11/5 0005 8:51
*/
@Component
@PropertySource(value = "classpath:conf.properties")
public class RedisUtils {
@Value("${redis.host}")
private String host;
@Value("${redis.port}")
private int port;
@Value("${redis.password}")
private String password;
@PostConstruct
public void getApiToken() {
ADDR = this.host ;
PORT = this.port ;
AUTH = this.password ;
}
//Redis服务器IP
private static String ADDR;
//Redis的端口号
private static int PORT;
//访问密码
private static String AUTH;
//可用连接实例的最大数目,默认值为8;
//如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
private static int MAX_ACTIVE = 1024;
//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
private static int MAX_IDLE = 200;
//等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
private static int MAX_WAIT = 10000;
private static int TIMEOUT = 10000;
//在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
private static boolean TEST_ON_BORROW = true;
private static JedisPool jedisPool = null;
/**
* 获取Jedis实例
* @return
*/
public synchronized static Jedis getJedis() {
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
} else {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH);
Jedis resource = jedisPool.getResource();
return resource;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
RedisUtils.setString("test","test");
}
/**
* 释放jedis资源
* @param jedis
*/
public static void returnResource(final Jedis jedis) {
if (jedis != null) {
jedisPool.returnResource(jedis);
}
}
public static void setString(String key ,String value){
Jedis jedis = null;
try {
jedis = getJedis();
jedis.set(key,value);
} catch (Exception e) {
System.out.println("setString设置redis键值异常:key=" + key + " value=" + value + " cause:" + e.getMessage());
} finally {
if(jedis != null)
{
jedis.close();
}
}
}
public static void setObject(String key,Object object) {
Jedis jedis = null;
try {
jedis = getJedis();
jedis.set(key.getBytes(), SerializeUtil.serialize(object));
} catch (Exception e) {
e.printStackTrace();
System.out.println("setString设置redis键值异常:key=" + key + " value=" + object + " cause:" + e.getMessage());
} finally {
if(jedis != null)
{
jedis.close();
}
}
}
public static void setString(String key ,String value , int expireTime){
String result = "OK";
Jedis jedis = null;
try {
jedis = getJedis();
jedis.set(key,value);
if(result.equals("OK")) {
jedis.expire(key.getBytes(), expireTime);
}
} catch (Exception e) {
System.out.println("setString设置redis键值异常:key=" + key + " value=" + value + " cause:" + e.getMessage());
} finally {
if(jedis != null)
{
jedis.close();
}
}
}
public static String getString(String key) {
Jedis jedis = null;
try {
jedis = getJedis();
String value = jedis.get(key);
return value;
} catch (Exception e) {
System.out.println("getString获取redis键值异常:key=" + key + " cause:" + e.getMessage());
} finally {
jedis.close();
}
return null;
}
public static Object getObject(String key) {
Jedis jedis = null;
try {
jedis = getJedis();
byte[] value = jedis.get(key.getBytes());
return SerializeUtil. unserialize(value);
} catch (Exception e) {
System.out.println("getString获取redis键值异常:key=" + key + " cause:" + e.getMessage());
} finally {
jedis.close();
}
return null;
}
/**
* 删除key
*/
public static Long delkeyObject(String key) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.del(key.getBytes());
}catch(Exception e) {
e.printStackTrace();
return null;
}finally{
if(jedis != null)
{
jedis.close();
}
}
}
public static Boolean existsObject(String key) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.exists(key);
}catch(Exception e) {
e.printStackTrace();
return false;
}finally{
if(jedis != null)
{
jedis.close();
}
}
}
public static void flushAll() {
Jedis jedis = null;
try {
jedis = getJedis();
jedis.flushAll();
} catch (Exception e) {
System.out.println("Cache清空失败:" + e);
}
}
}