释放双眼,带上耳机,听听看~!
一、添加jedis的maven依赖
1
2
3
4
5
6 1<dependency>
2 <groupId>redis.clients</groupId>
3 <artifactId>jedis</artifactId>
4 <version>2.9.0</version>
5</dependency>
6
二、在配置cache.properties文件中配置单节点redis信息
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 1# 单节点配置
2redis.host = 127.0.0.1
3redis.port = 6379
4
5# 集群节点,集群模式下配置
6#redis.cluster.nodes = 12.2.3.14:7001,12.2.3.14:7002,12.2.3.14:7003,12.2.3.14:7004
7
8# 哨兵节点,哨兵模式下配置
9#redis.sentinel.nodes = 12.2.3.14:7001,12.2.3.14:7002,12.2.3.14:7003,12.2.3.14:7004
10#redis.sentinel.master = mymaster
11
12# ----redis common begin----
13# 密码
14#redis.password = 123456
15# 连接超时时间 单位 ms(毫秒)
16redis.timeout = 6000
17# 连接池中的最大空闲连接,默认值也是8
18redis.pool.max-idle = 8
19# 连接池中的最小空闲连接,默认值也是0
20redis.pool.min-idle = 0
21# 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
22redis.pool.max-active = 8
23# 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出
24redis.pool.max-wait = -1
25# ----redis common end----
26
三、定义CacheConfig类来加载配置信息
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 1package com.ldy.common.cache;
2
3/**
4 * @author lidongyang
5 * @Description 缓存配置类
6 * @date 2019/6/21 8:38
7 */
8public class CacheConfig {
9
10 /**
11 * 单节点REDIS 地址
12 */
13 public static String REDIS_HOST = PropertiesUtils.getProperty("cache-config", "redis.host");
14
15 /**
16 * 单节点REDIS 端口
17 */
18 public static int REDIS_PORT = Integer.parseInt(PropertiesUtils.getProperty("cache-config", "redis.port"));
19
20 /**
21 * 集群模式节点
22 */
23 public static String REDIS_CLUSTER_NODES = PropertiesUtils.getProperty("cache-config", "redis.cluster.nodes");
24
25 /**
26 * 哨兵模式节点
27 */
28 public static String REDIS_SENTINEL_NODES = PropertiesUtils.getProperty("cache-config", "redis.sentinel.nodes");
29
30 /**
31 * 哨兵模式 master
32 */
33 public static String REDIS_SENTINEL_MASTER = PropertiesUtils.getProperty("cache-config", "redis.sentinel.master");
34
35 /**
36 * REDIS 密码
37 */
38 public static String REDIS_PASSWORD = PropertiesUtils.getProperty("cache-config", "redis.password");
39
40 /**
41 * REDIS 连接超时时间 单位 ms(毫秒)
42 */
43 public static int REDIS_TIMEOUT = Integer.parseInt(PropertiesUtils.getProperty("cache-config", "redis.timeout"));
44
45 /**
46 * 连接池中的最大空闲连接,默认值也是8
47 */
48 public static int REDIS_POOL_MAX_IDLE = Integer.parseInt(PropertiesUtils.getProperty("cache-config", "redis.pool.max-idle"));
49
50 /**
51 * 连接池中的最小空闲连接,默认值也是0
52 */
53 public static int REDIS_POOL_MIN_IDLE = Integer.parseInt(PropertiesUtils.getProperty("cache-config", "redis.pool.min-idle"));
54
55
56 /**
57 * # 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
58 */
59 public static int REDIS_POOL_MAX_ACTIVE = Integer.parseInt(PropertiesUtils.getProperty("cache-config", "redis.pool.max-active"));
60
61 /**
62 * 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出
63 */
64 public static int REDIS_POOL_MAX_WAIT = Integer.parseInt(PropertiesUtils.getProperty("cache-config", "redis.pool.max-wait"));
65
66}
67
68
1
2
3 1上面使用到的PropertiesUtils代码如下:
2
3
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 1package com.ldy.common.cache;
2import java.io.InputStream;
3import java.util.Properties;
4
5
6/**
7 *
8 * PropertiesUtils.java
9 * @desc properties 资源文件解析工具
10 * @author lidongyang
11 *
12 */
13public class PropertiesUtils {
14 private static Properties props;
15
16 private static void readProperties(String fileName) {
17 InputStream fis = null;
18 try {
19 props = new Properties();
20 fis =PropertiesUtils.class.getClassLoader().getResourceAsStream(fileName);
21 props.load(fis);
22 } catch (Exception e) {
23 e.printStackTrace();
24 } finally{
25 if(fis != null){
26 try {
27 fis.close();
28 }catch(Exception e) {
29 e.printStackTrace();
30 }
31 }
32
33 }
34 }
35
36 /**
37 * 根据文件名和属性名获取属性值
38 * @author lidongyang
39 * @param fileName
40 * @param key
41 * @return
42 */
43 public synchronized static String getProperty(String fileName,String key){
44 readProperties(fileName+".properties");
45 return props.getProperty(key);
46 }
47
48 public static void main(String[] args) {
49 String value = PropertiesUtils.getProperty("webconfig", "zip_path");
50 System.out.println("value:"+value);
51 }
52}
53
54
四、编写RedisSingleCache类用来操作redis
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 1package com.ldy.common.cache;
2
3import redis.clients.jedis.Jedis;
4import redis.clients.jedis.JedisPool;
5import redis.clients.jedis.JedisPoolConfig;
6
7import java.util.Date;
8
9/**
10 * @author lidongyang
11 * @Description 单节点模式redis缓存接口
12 * @date 2019/6/20 16:38
13 */
14public class RedisSingleCache {
15
16 private static JedisPool jedisPool = null;
17
18 public RedisSingleCache() {
19 if (null == jedisPool) {
20 initPool();
21 }
22 }
23
24 private void initPool() {
25 JedisPoolConfig config = new JedisPoolConfig();
26 //最大连接数,如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
27 config.setMaxTotal(CacheConfig.REDIS_POOL_MAX_ACTIVE);
28 //最大空闲数,控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
29 config.setMaxIdle(CacheConfig.REDIS_POOL_MAX_IDLE);
30 //最小空闲数
31 config.setMinIdle(CacheConfig.REDIS_POOL_MIN_IDLE);
32 //等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
33 config.setMaxWaitMillis(CacheConfig.REDIS_POOL_MAX_WAIT);
34
35 //是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
36 config.setTestOnBorrow(true);
37 //在return给pool时,是否提前进行validate操作
38 config.setTestOnReturn(true);
39 //在空闲时检查有效性,默认false
40 config.setTestWhileIdle(true);
41
42 if (null == CacheConfig.REDIS_PASSWORD || CacheConfig.REDIS_PASSWORD == "") {
43 jedisPool = new JedisPool(config, CacheConfig.REDIS_HOST, CacheConfig.REDIS_PORT, CacheConfig.REDIS_TIMEOUT);
44 } else {
45 jedisPool = new JedisPool(config, CacheConfig.REDIS_HOST, CacheConfig.REDIS_PORT, CacheConfig.REDIS_TIMEOUT, CacheConfig.REDIS_PASSWORD);
46 }
47 }
48
49
50 /**
51 * 同步获取Jedis实例
52 *
53 * @return Jedis
54 */
55 private synchronized static Jedis getJedis() {
56 return jedisPool.getResource();
57 }
58
59 public String get(String key) {
60 Jedis jedis = getJedis();
61 if (jedis == null) {
62 System.out.println("Jedis实例获取失败!");
63 throw new RuntimeException();
64 }
65
66 try {
67 return jedis.get(key);
68 } catch (Exception e) {
69 System.out.println("获取值失败:" + e.getMessage());
70 throw new RuntimeException();
71 } finally {
72 if (null != jedis) {
73 jedis.close();
74 }
75 }
76 }
77
78
79 public Object getObject(String key) {
80 Jedis jedis = getJedis();
81 if (jedis == null) {
82 System.out.println("Jedis实例获取失败!");
83 throw new RuntimeException();
84 }
85
86 try {
87 byte[] data = jedis.get(key.getBytes());
88 return SerializeUtils.unserialize(data);
89 } catch (Exception e) {
90 System.out.println("获取值失败:" + e.getMessage());
91 throw new RuntimeException();
92 } finally {
93 if (null != jedis) {
94 jedis.close();
95 }
96 }
97 }
98
99 public void set(String key, String value) {
100 Jedis jedis = getJedis();
101 try {
102 jedis.set(key, value);
103 } catch (Exception e) {
104 System.out.println("设置值失败:" + e.getMessage());
105 throw new RuntimeException();
106 } finally {
107 if (null != jedis) {
108 jedis.close();
109 }
110 }
111 }
112
113 public void set(String key, String value, int second) {
114 Jedis jedis = getJedis();
115 try {
116 jedis.set(key, value);
117 jedis.expire(key, second);
118 } catch (Exception e) {
119 System.out.println("设置值失败:" + e.getMessage());
120 throw new RuntimeException();
121 } finally {
122 if (null != jedis) {
123 jedis.close();
124 }
125 }
126 }
127
128 public void setObject(String key, Object value) {
129 Jedis jedis = getJedis();
130 try {
131 jedis.set(key.getBytes(), SerializeUtils.serialize(value));
132 } catch (Exception e) {
133 System.out.println("设置值失败:" + e.getMessage());
134 throw new RuntimeException();
135 } finally {
136 if (null != jedis) {
137 jedis.close();
138 }
139 }
140 }
141
142 public void setObject(String key, Object value, int second) {
143 Jedis jedis = getJedis();
144 try {
145 jedis.set(key.getBytes(), SerializeUtils.serialize(value));
146 jedis.expire(key, second);
147 } catch (Exception e) {
148 System.out.println("设置值失败:" + e.getMessage());
149 throw new RuntimeException();
150 } finally {
151 if (null != jedis) {
152 jedis.close();
153 }
154 }
155 }
156
157 public void del(String key) {
158 Jedis jedis = getJedis();
159 try {
160 jedis.del(key);
161 } catch (Exception e) {
162 System.out.println("删除失败:" + e.getMessage());
163 throw new RuntimeException();
164 } finally {
165 if (null != jedis) {
166 jedis.close();
167 }
168 }
169 }
170
171
172 public static void main(String[] args) {
173 RedisSingleCache cache = new RedisSingleCache();
174
175 cache.set("lisi", "李四");
176 System.out.println(cache.get("lisi"));
177 cache.del("lisi");
178
179 cache.setObject("date", new Date());
180 System.out.println(cache.getObject("date"));
181 }
182
183}
184
185
辅助工具类SerializeUtils代码如下
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 1package com.ldy.common.cache;
2
3import java.io.ByteArrayInputStream;
4import java.io.ByteArrayOutputStream;
5import java.io.ObjectInputStream;
6import java.io.ObjectOutputStream;
7
8/**
9 * @author lidongyang
10 * @Description 对象序列化工具类
11 * @date 2019/6/20 18:51
12 */
13public class SerializeUtils {
14
15 /**
16 * 序列化
17 * @param obj
18 * @return
19 */
20 public static byte[] serialize(Object obj) {
21 ByteArrayOutputStream byteOutputStream = null;
22 ObjectOutputStream objectOutputStream = null;
23
24 try {
25 byteOutputStream = new ByteArrayOutputStream();
26 objectOutputStream = new ObjectOutputStream(byteOutputStream);
27
28 objectOutputStream.writeObject(obj);
29 objectOutputStream.flush();
30
31 return byteOutputStream.toByteArray();
32 } catch (Exception e) {
33 e.printStackTrace();
34 } finally {
35 if (null != objectOutputStream) {
36 try {
37 objectOutputStream.close();
38 byteOutputStream.close();
39 } catch (Exception e) {
40 e.printStackTrace();
41 }
42 }
43 }
44
45 return null;
46 }
47
48
49 /**
50 * 反序列化
51 * @param bytes
52 * @return
53 */
54 public static Object unserialize(byte[] bytes) {
55 ByteArrayInputStream byteInputStream = null;
56 ObjectInputStream objectInputStream = null;
57
58 try {
59 byteInputStream = new ByteArrayInputStream(bytes);
60 objectInputStream = new ObjectInputStream(byteInputStream);
61
62 return objectInputStream.readObject();
63
64 } catch (Exception e) {
65 e.printStackTrace();
66 } finally {
67 if (null != objectInputStream) {
68 try {
69 objectInputStream.close();
70 byteInputStream.close();
71 } catch (Exception e) {
72 e.printStackTrace();
73 }
74 }
75 }
76 return null;
77 }
78
79}
80