redis使用

环境

jdk1.8、redis数据库
commons-pool2-2.6.0.jar
jedis-2.9.0.jar
commons-logging-1.0.4.jar
log4j-1.2.16.jar

代码

1.com.redis.RedisUtil.java

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
package com.redis;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* log4j(commons-logging/log4j)
* redis(jedis/commons-pool2)
* @author wangming
*
*/
public class RedisUtil {
private static JedisPool pool=null;
//最大连接数
private static int max_active=50;
//最大空闲数
private static int max_idle=40;
//等待时间
private static long max_waitmillis=3000;
//redis主机地址
private static String host;
//redis端口
private static int port;
//redis用户密码
private static String password;
//过期时间
private static int expireTime = 2*60*60*1000;

static Logger logger = Logger.getLogger(RedisUtil.class);
static{
try {
//获取配置文件内容
InputStream in = RedisUtil.class.getResourceAsStream("redis.properties");
Properties prop = new Properties();
prop.load(in);
RedisUtil.max_active = Integer.parseInt(prop.getProperty("max_active"));
RedisUtil.max_idle = Integer.parseInt(prop.getProperty("max_idle"));
RedisUtil.max_waitmillis = Long.parseLong(prop.getProperty("max_waitmillis").toString());
RedisUtil.host = prop.getProperty("host");
RedisUtil.port = Integer.parseInt(prop.getProperty("port"));
RedisUtil.password = prop.getProperty("password");
//初始化连接池
RedisUtil.pool = initPool();
} catch (IOException e) {
e.printStackTrace();
}
}

/***
* 初始化连接池
* @return
*/
private static JedisPool initPool(){
JedisPool result = null;
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(max_idle);
config.setMaxWaitMillis(max_waitmillis);
config.setTestOnBorrow(true);
if(RedisUtil.password==null||"".equals(RedisUtil.password)){
result = new JedisPool(config,host,port);
}else{
result = new JedisPool(config,host,port,3000,RedisUtil.password);
}
} catch (Exception e) {
e.printStackTrace();
}

return result;
}

/**
* 向redis添加String,成功返回0,失败返回1
* @param String
*/
public static int set(String key,String value){

if(value==null || key==null){
logger.debug("redis存储对象时传入参数为空");
return 1;
}
Jedis jedis=null;
try{
jedis = RedisUtil.getJedis();
jedis.set(key, value);
}catch(Exception e){
logger.debug("对象存储redis失败");
return 1;
}finally{
RedisUtil.closeJedis(jedis);
}
return 0;
}

/**
* redis数据库查询
*/
public static String get(String key){
if(key==null){
logger.debug("redis查询方法,传入参数为空");
return null;
}
String result="";
Jedis jedis=null;
try{
jedis = RedisUtil.getJedis();
result = jedis.get(key);
}catch(Exception e){
logger.debug("redis查询方法执行失败"+e);
}finally{
RedisUtil.closeJedis(jedis);
}
return result;
}

/**
* 封装jedis的hset方法解释如下
* @param key
* @param field 实体类的类名
* @param value 对应分项内容的json串
* @return
*/
public static long hset(String key,String field,String value){
long result =0;
Jedis jedis = null;
try {
jedis = RedisUtil.getJedis();
result = jedis.hset(key, field, value);
//设置两小时过期
jedis.expire(key,expireTime);
} catch (Exception e) {
e.printStackTrace();
}finally{
closeJedis(jedis);
}
return result;
}

/**
* 封装redis的hget方法,,解释如下
* @param key
* @param field 对应实体类的类名
* @return result 对应分项内容的json串
*/
public static String hget(String key,String field){
String result =null;
Jedis jedis = null;
try {
jedis = RedisUtil.getJedis();
result = jedis.hget(key, field);
} catch (Exception e) {
e.printStackTrace();
}finally{
closeJedis(jedis);
}
return result;
}


/**
* 设置过期时间
* @param value 秒
* @return 成功返回0,失败返回1
*/
public static int expire(String key,int value){
if(key==null){
logger.debug("redis设置失效时间,传入key参数为空");
return 1;
}
Jedis jedis =null;
try{
jedis = RedisUtil.getJedis();
jedis.expire(key, value);
}catch(Exception e){
logger.debug("redis设置"+key+"失效时间失败");
return 1;
}finally{
RedisUtil.closeJedis(jedis);
}
return 0;
}

/***
* 获取Jedis实例
* @return Jedis实例
*/
public static Jedis getJedis(){
Jedis result = null;
try {
result = RedisUtil.pool.getResource();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

/***
* 关闭 Jedis
* @param jedis
*/
public static void closeJedis(Jedis jedis){
if(jedis!=null){
jedis.close();
}
}

public static void main(String[] args) {
// Jedis jedis = getJedis();
String openid = "o5lHPww7MK_ca1-LFLzWUjTRJ7oU";
RedisUtil.set(openid, "20180920/1537413108683.jpg|20180920/1537413108633.jpg|");
String str = RedisUtil.get(openid);
//RedisUtil.expire(RedisKeys.PERSONALINCOMETAX, 10*60*60*1000);
System.out.println(str);
String[] strs = str.split("\\|");
System.out.println(strs[0]);
/*int i = RedisUtil.expire(RedisKeys.IMAGEACQUISITION, 0);
System.out.println(i);*/
}
}

2.com.redis下配置文件redis.properties
1
2
3
4
5
6
7
8
9
10
11
max_active=100
#\u6700\u5927\u7a7a\u95f2\u6570
max_idle=40
#\u6700\u5927\u7b49\u5f85\u65f6\u95f4
max_waitmillis=3000
#redis\u5730\u5740
host=127.0.0.1
#redis\u7aef\u53e3
port=6379
#redis\u5bc6\u7801
password=xxx

3.com.redis下配置文件log4j.properties
1
2
3
4
5
6
7
8
9
10
# Global logging configuration
log4j.rootLogger=INFO, stdout

# My logging configuration...
log4j.logger.com.geong=INFO

## Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n

git克隆地址

https://github.com/syxiaowanzi/redis-util.git

本文标题:redis使用

文章作者:wangming

发布时间:2019年01月20日 - 19:14:53

最后更新:2019年01月22日 - 23:38:28

原始链接:https://syxiaowanzi.github.io/2019/01/20/redis使用/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%