Helius
2020-12-17 f76c9c5beb39916771402de95f05be18f39a9db6
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package com.matrix.component.redis;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.StringUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONNull;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
 
import java.util.*;
 
import static org.springframework.util.Assert.notNull;
 
/**
 * redis连接
 * 
 * @author jiangyouyao
 * @email 512061637@qq.com
 * @date 2019年2月25日
 */
 
@Component
public class RedisClient implements CommandLineRunner {
 
    /**
     * redis服务器地址
     */
    @Value("${redis.hostname}")
    private String hostname;
 
    /**
     * redis 访问端口
     */
    @Value("${redis.port}")
    private int port;
 
    /**
     * 默认失效时间
     */
    @Value("${redis.timeout}")
    private int timeOut;
 
    @Value("${redis.password}")
    private String password;
 
    /**
     * 非切片连接池
     */
    private JedisPool jedisPool;
 
    /**
     * 数据库标号
     */
    @Value("${redis.database}")
    private int database;
 
    private RedisClient() {
    }
 
    /**
     * 保存一个对象,待超时时间
     * 
     * @author JIANGYOUYAO
     * @email 935090232@qq.com
     * @date 2018年1月19日
     * @param key
     * @param bean
     * @param timeOut
     */
    public void saveValue(String key, String value, int timeOut) {
        if (StringUtils.isNotBlank(value)) {
            Jedis jedis = getJedis();
            jedis.setex(key, timeOut, value);
            jedis.close();
        }
    }
 
    /**
     * 保存一个string,无过期时间
     * 
     * @author zhangh
     * @date 2018年7月19日
     * @param key
     * @param value
     * @param timeOut
     */
    public void saveValueForever(String key, String value) {
        if (StringUtils.isNotBlank(value)) {
            Jedis jedis = getJedis();
            jedis.set(key, value);
            jedis.close();
        }
    }
 
    /**
     * 删除一个对象
     * 
     * @author JIANGYOUYAO
     * @email 935090232@qq.com
     * @date 2018年1月19日
     * @param key
     */
    public void removeObject(String key) {
        Jedis jedis = getJedis();
        jedis.del(key);
        jedis.close();
    }
 
    /**
     * 根据key前缀批量删除缓存
     *
     * @author wanglizhu
     * @email 935090232@qq.com
     * @date 2019年8月6日
     * @param key
     */
    public void batchDel(String key) {
        Jedis jedis = getJedis();
        Set<String> set = jedis.keys(key +"*");
        Iterator<String> it = set.iterator();
        while(it.hasNext()){
            String keyStr = it.next();
            System.out.println(keyStr);
            jedis.del(keyStr);
        }
        jedis.close();
    }
 
 
    /**
     * 保存一个对象使用默认超时时间
     * 
     * @author JIANGYOUYAO
     * @email 935090232@qq.com
     * @date 2018年1月19日
     * @param key
     * @param jsonObject
     */
    public void saveValue(String key, Object jsonObject) {
        if (jsonObject != null) {
            Jedis jedis = getJedis();
            Gson g = new Gson();
            jedis.setex(key, timeOut, g.toJson(jsonObject));
            jedis.close();
        }
    }
 
    /**
     * 保存一个对象,不设置过期时间
     * 
     * @author zhangh
     * @date 2018年7月19日
     * @param key
     * @param jsonObject
     */
    public void saveValueForever(String key, Object jsonObject) {
        if (jsonObject != null) {
            Jedis jedis = getJedis();
            Gson g = new Gson();
            jedis.set(key, g.toJson(jsonObject));
            jedis.close();
        }
    }
 
    public void saveMapValue(String key, Map jsonObject) {
        if (jsonObject != null) {
            Jedis jedis = getJedis();
            Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
            jedis.setex(key, timeOut, gson.toJson(jsonObject));
            jedis.close();
        }
    }
 
    /**
     *
     * 此方法将ArrayList集合直接存储为一个字符串
     *
     * @param key
     *            存储的名字
     * @param list
     *            要存储的集合对象
     */
    public Boolean setArrayList(String key, List<Object> list) {
        if (list != null && key != null && key != "") {
            Jedis jedis = getJedis();
            jedis.set(key, JSONArray.fromObject(list).toString());
            jedis.close();
            return true;
        }
        return false;
    }
 
    /**
     * 此方法将会把存在redis中的数据取出来,并封装成相应的Arraylist集合
     *
     * @param key
     *            存储的名字
     * @param beanClass
     *            要封装成为的javaBean
     * @return List
     */
    public List<Object> getArraylist(String key, Class beanClass) {
        List<Object> list = new ArrayList<Object>();
        if (StringUtils.isNotBlank(key) && beanClass != null) {
                Jedis jedis = getJedis();
                JSONArray jsonArray = JSONArray.fromObject(jedis.get(key));
                if(!JSONNull.getInstance().equals(jsonArray.get(0))){
                    String o = jsonArray.get(0).toString();
                    JSONArray myJsonArray = JSONArray.fromObject(o);
                    for(int i = 0; i < myJsonArray.size(); i++){
                        Object object = JSONObject.toBean(
                                (JSONObject) myJsonArray.get(i), beanClass);
                        list.add(object);
                    }
                }
            jedis.close();
            }
        return list;
    }
 
 
    /**
     * 获取list
     * @param key
     * return :list<String>
     */
    public List getCachedValueList(String key) {
        List<Object> value = new ArrayList<>();
        Jedis jedis = null;
        try {
            jedis = getJedis();
            value = Collections.singletonList(jedis.lrange(key, 0, jedis.llen(key)));
        } catch (RuntimeException e) {
            LogUtil.error("获取redis 值 RuntimeException", e, "");
            if (jedis != null) {
                jedis.close();
            }
        } finally {
            if (jedis != null) {
                LogUtil.debug("释放jdedis 链接", jedis);
                jedis.close();
            }
        }
        return value;
    }
 
    // public List<String> saveList
    public String getCachedValue(String key) {
        String value = "";
        Jedis jedis = null;
        try {
            jedis = getJedis();
            value = jedis.get(key);
        } catch (RuntimeException e) {
            LogUtil.error("获取redis 值 RuntimeException", e, "");
            if (jedis != null) {
                jedis.close();
            }
        } finally {
            if (jedis != null) {
                LogUtil.debug("释放jdedis 链接", jedis);
                jedis.close();
            }
        }
        return value;
    }
 
    public void resetExpire(String key) {
        Jedis jedis = getJedis();
        jedis.expire(key, timeOut);
        jedis.close();
    }
 
    /**
     * bean创建后调用
     */
    // @Override
    public void afterPropertiesSet() throws Exception {
        // 校验参数
        notNull(hostname, "Property 'hostname' is required");
        notNull(port, "Property 'port' is required");
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(100);
        config.setMaxTotal(1000);
        config.setMaxWaitMillis(30000L);
        config.setTestOnBorrow(false);
        // 每次释放链接的数目
        config.setNumTestsPerEvictionRun(20);
 
        jedisPool = new JedisPool(config, hostname, port, 30000, password, database);
    }
 
    private Jedis getJedis() {
        Jedis jedis = jedisPool.getResource();
        return jedis;
    }
 
    @Override
    public void run(String... args) throws Exception {
        // 校验参数
        notNull(hostname, "Property 'hostname' is required");
        notNull(port, "Property 'port' is required");
        JedisPoolConfig config = new JedisPoolConfig();
        //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
        config.setMaxIdle(1000);
        //设置最大实例总数
        config.setMaxTotal(1);
        config.setMaxWaitMillis(30000L);
        config.setTestOnBorrow(false);
        // 每次释放链接的数目
        config.setNumTestsPerEvictionRun(20);
        config.setSoftMinEvictableIdleTimeMillis(1800000);
 
        jedisPool = new JedisPool(config, hostname, port, 30000, password, database);
 
    }
 
}