0%

REDISUTILS说明

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
/**

* 演示Jedis连接池

* 客户端

*/

public class Demo01 {

public static void main(String[] args) {

//NO1)创建Jedis连接池配置对象,类似到c3p0-config.xml文件

JedisPoolConfig config = new JedisPoolConfig();

//NO2)创建Jedis连接池

//设置连接池最多存入10个Jedis对象

config.setMaxTotal(10);

//从连接池中取出Jedis对象最多等2秒

config.setMaxWaitMillis(2000);

//NO3)从Jedis连接池中取出一个Jedis对象

JedisPool pool = new JedisPool(config,“127.0.0.1”,6379);

//NO4)使用Jedis对象操作Redis数据库

Jedis jedis = pool.getResource();

//选择15号数据库

jedis.select(15);

//根据key找value

String username = jedis.get(“username”);

//如果value找到了

if(username != null){

//删除该key

jedis.del(“username”);

}

//NO5)将Jedis对象还回给Jedis连接池

jedis.close();

}

}

/**

* 演示通过JedisUtils来获取,操作,关闭Jedis对象

* 客户端

*/

public class Demo01 {

public static void main(String[] args) throws Exception{

//获取Jedis对象

Jedis jedis = JedisUtil.getJedis();

//使用Jedis对象操作Redis数据库

jedis.set(“address”,“长沙”);

//关闭Jedis对象

JedisUtil.close(jedis);

}

}



package com.itheima.redis.util;



import java.util.ResourceBundle;

import redis.clients.jedis.Jedis;

import redis.clients.jedis.JedisPool;

import redis.clients.jedis.JedisPoolConfig;



/**

* Jedis工具类

*/

public final class JedisUtil {

private static JedisPool pool;

private static String maxTotal;

private static String maxWaitMillis;

private static String host;

private static String port;

/**

* 读取src/jedis.properties属性文件中的内容

*/

static{

//ResourceBundle专用于去类路径下找*.properties文件,所以扩展名properties可以不写

//建议项目中的配置文件放在src目录下

ResourceBundle rb = ResourceBundle.getBundle(“jedis”);

//根据key找value

maxTotal = rb.getString(“maxTotal”);

maxWaitMillis = rb.getString(“maxWaitMillis”);

host = rb.getString(“host”);

port = rb.getString(“port”);

}

/**

* 创建连接池对象

*/

static{

JedisPoolConfig config = new JedisPoolConfig();

config.setMaxTotal(Integer.parseInt(maxTotal));

config.setMaxWaitMillis(Integer.parseInt(maxWaitMillis));

pool = new JedisPool(config,host,Integer.parseInt(port));

}

private JedisUtil(){

}

/**

* 获取Jedis对象

*/

public static Jedis getJedis() throws Exception{

//如果连接池非空

if(pool != null){

//从连接池中获取Jedis对象

return pool.getResource();

}else{

//return null;

//抛异常

throw new Exception(“Jedis不能为空”);

}

}

/**

* 关闭Jedis对象

*/

public static void close(Jedis jedis) {

if(jedis != null){

jedis.close();

}

}

}