派生自 projectDept/qhighschool

yn147
2023-05-10 96286178ee1c257c130cb2ad964a781f36c4eee5
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package com.qxueyou.scc.base.dao;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
import org.apache.commons.lang3.StringUtils;
 
import com.qxueyou.scc.base.util.ReisUtils;
 
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ShardedJedis;
 
public abstract class AbstractRedisTemplate {
    // lock script
    private static String SET_NX_SCRIPT = "local key = KEYS[1] local ttl = KEYS[2] "
            + "local content = KEYS[3] local lockSet = redis.call('setnx', key, content) "
            + "if lockSet == 1 then redis.call('expire', key, ttl) end return lockSet ";
 
    protected abstract ShardedJedis getJedisClient();
 
    protected abstract void closeJedisClient(ShardedJedis jedisClient);
 
    public void set(String key, String value) {
        ShardedJedis client = null;
        try {
            client = this.getJedisClient();
            this.getJedisClient().set(key, value);
        } finally {
            this.closeJedisClient(client);
        }
    }
 
    /**
     * 设置过期时间的set方法
     * 
     * @param key
     *            键
     * @param value
     *            value
     * @param timeout
     *            过期时间 单位(s)
     */
    public void setex(String key, String value, int timeout) {
        ShardedJedis client = null;
        try {
            client = this.getJedisClient();
            client.setex(key, timeout, value);
        } finally {
            this.closeJedisClient(client);
        }
    }
 
    public boolean exists(String key) {
        ShardedJedis client = null;
        boolean result = false;
        try {
            client = this.getJedisClient();
            result = client.exists(key);
        } finally {
            this.closeJedisClient(client);
        }
        return result;
    }
 
    public boolean expire(String key, int secondTimeOut) {
        ShardedJedis client = null;
        Long ex = 0L;
        try {
            client = this.getJedisClient();
            ex = client.expire(key, secondTimeOut);
        } finally {
            this.closeJedisClient(client);
        }
        return ex.intValue() == 1;
    }
 
    public boolean expireAt(String key, long expireAt) {
        int timeOut = (int)(expireAt - System.currentTimeMillis()) / 1000;
        return expire(key, timeOut);
    }
 
    /** 根据键获取字符串值 */
    public String get(String key) {
        ShardedJedis client = null;
        String value = "";
        try {
            client = this.getJedisClient();
            value = client.get(key);
        } finally {
            this.closeJedisClient(client);
        }
        return value;
    }
 
    /** 获取并删除键 */
    public String getAndDel(String key) {
        ShardedJedis client = null;
        String value = "";
        try {
            client = this.getJedisClient();
            value = client.get(key);
            client.del(key);
        } finally {
            this.closeJedisClient(client);
        }
        return value;
    }
 
    /** 删除键 */
    public void del(String key) {
        ShardedJedis client = null;
        try {
            client = this.getJedisClient();
            client.del(key);
        } finally {
            this.closeJedisClient(client);
        }
    }
 
    /** 获取并删除键 */
    public void del(String[] keys) {
        ShardedJedis client = null;
        try {
            client = this.getJedisClient();
            for (String strKey : keys) {
                if (StringUtils.isNotEmpty(strKey)) {
                    client.del(strKey.getBytes());
                }
            }
        } finally {
            this.closeJedisClient(client);
        }
    }
 
    public long incr(String key) {
        ShardedJedis client = null;
        try {
            client = this.getJedisClient();
            return client.incr(key);
        } finally {
            this.closeJedisClient(client);
        }
    }
 
    public long decr(String key) {
        ShardedJedis client = null;
        try {
            client = this.getJedisClient();
            return client.decr(key);
        } finally {
            this.closeJedisClient(client);
        }
    }
 
    public <T> void setList(String key, List<T> lst) {
        ShardedJedis client = null;
        try {
            client = this.getJedisClient();
            client.set(key.getBytes(), ReisUtils.serialize(lst));
        } finally {
            this.closeJedisClient(client);
        }
    }
 
    @SuppressWarnings("unchecked")
    public <T> List<T> getList(String key) {
        ShardedJedis client = null;
        byte[] bytesArr = null;
        List<T> result = null;
        try {
            client = this.getJedisClient();
            bytesArr = client.get(key.getBytes());
            result = (List<T>) ReisUtils.unserialize(bytesArr);
        } finally {
            this.closeJedisClient(client);
        }
        return result;
    }
    
    public List<String> getStrList(String key) {
        ShardedJedis client = null;
        List<String> result = null;
        try {
            client = this.getJedisClient();
            result = client.lrange(key, 0, client.llen(key));
        } finally {
            this.closeJedisClient(client);
        }
        return result;
    }
    
    public void setMap(String key, Map<String,String> map) {
        ShardedJedis client = null;
        try {
            client = this.getJedisClient();
            client.hmset(key, map);
        } finally {
            this.closeJedisClient(client);
        }
    }
    
    public List<String> getMap(String key, String... mKey) {
        ShardedJedis client = null;
        List<String> result = null;
        try {
            client = this.getJedisClient();
            result = client.hmget(key, mKey);
        } finally {
            this.closeJedisClient(client);
        }
        return result;
    }
 
    public String hget(String key, String field) {
        ShardedJedis client = null;
        try {
            client = this.getJedisClient();
            return client.hget(key, field);
        } finally {
            this.closeJedisClient(client);
        }
    }
 
    public <T> Long hset(String key, String field, T value) {
        ShardedJedis client = null;
        try {
            client = this.getJedisClient();
            return client.hset(key.getBytes(), field.getBytes(), ReisUtils.serialize(value));
        } finally {
            this.closeJedisClient(client);
        }
    }
 
    public Long hdel(String key, String... field) {
        ShardedJedis client = null;
        try {
            client = this.getJedisClient();
            return client.hdel(key, field);
        } finally {
            this.closeJedisClient(client);
        }
    }
 
    public Long hdel(String key, byte[]... field) {
        ShardedJedis client = null;
        try {
            client = this.getJedisClient();
            return client.hdel(key.getBytes(), field);
        } finally {
            this.closeJedisClient(client);
        }
    }
 
    @SuppressWarnings("unchecked")
    public <T> T get(String key, Class<T> cls) {
        ShardedJedis client = null;
        T result = null;
        try {
            client = this.getJedisClient();
            byte[] obj = client.get(key.getBytes());
            if (obj == null || obj.length==0) {
                return null;
            }
            result = (T)ReisUtils.unserialize(obj);
        } finally {
            this.closeJedisClient(client);
        }
        return result;
    }
 
    public void setex(String key, Object value, int timeout) {
        ShardedJedis client = null;
        try {
            client = this.getJedisClient();
            client.setex(key.getBytes(), timeout, ReisUtils.serialize(value));
        } finally {
            this.closeJedisClient(client);
        }
    }
 
    /**
     * @param key
     * @param value
     * @return
     */
    public long setnx(String key, String value) {
        ShardedJedis client = null;
        try {
            client = this.getJedisClient();
            return client.setnx(key, value);
        } finally {
            this.closeJedisClient(client);
        }
    }
 
    public boolean setNxEx(String key, String value, long seconds) {
        Jedis jedis = null;
        ShardedJedis client = null;
        try {
            client = this.getJedisClient();
            jedis = client.getShard(key);
            long a = ((Long) jedis.eval(SET_NX_SCRIPT, 3, key, String.valueOf(seconds), value)).longValue();
            return a == 1L;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
            this.closeJedisClient(client);
        }
    }
 
    /**
     * 多长时间内尝试获取锁
     * 
     * @param key
     * @param timeOut
     * @return
     * @throws Exception
     */
    public boolean lock(String key, int timeOut) throws Exception {
        long waitEndTime = System.currentTimeMillis() + (timeOut * 1000);
        String lockKey = "JedisLock_".concat(key);
        while (!this.setNxEx(lockKey, "1", 60 * 60)) {
            if (waitEndTime < System.currentTimeMillis()) {
                return false;
            }
            try {
                // 每隔200ms秒尝试获取锁
                Thread.sleep(200);
            } catch (InterruptedException e) {
                throw e;
            }
        }
        return true;
    }
 
    public boolean tryLock(String key) {
        String lockKey = "JedisLock_".concat(key);
        return this.setNxEx(lockKey, "1", 30 * 60);
    }
 
    protected boolean tryLock(String key, long lockSeconds) {
        String lockKey = "JedisLock_".concat(key);
        return this.setNxEx(lockKey, "1", lockSeconds);
    }
 
    /**
     * 
     * @param key
     * @return
     */
    public boolean release(String key) {
        ShardedJedis client = null;
        try {
            client = this.getJedisClient();
            String lockKey ="JedisLock_".concat(key);
            return client.del(lockKey) == 1;
        } finally {
            this.closeJedisClient(client);
        }
    }
    
 
    public void lpush(String key,String value){
        ShardedJedis client = null;
        try{
            client = this.getJedisClient();
            client.lpush(key, value);
        }finally{
            this.closeJedisClient(client);
        }
    }
    
    public void rpush(String key,String str){
        ShardedJedis client = null;
        try{
            client = this.getJedisClient();
            client.rpush(key,str);
        }finally{
            this.closeJedisClient(client);
        }
    }
    
    public<T> void rpush(String key,T t){
        ShardedJedis client = null;
        try{
            client = this.getJedisClient();
            client.rpush(key.getBytes(),ReisUtils.serialize(t));
        }finally{
            this.closeJedisClient(client);
        }
    }
    
    public String rpop(String key){
        ShardedJedis client = null;
        String str = null;
        try{
            client = this.getJedisClient();
            str = client.rpop(key);
        }finally{
            this.closeJedisClient(client);
        }
        return str;
    }
    
    
    public String lpop(String key){
        ShardedJedis client = null;
        String str = null;
        try{
            client = this.getJedisClient();
            str = client.lpop(key);
        }finally{
            this.closeJedisClient(client);
        }
        return str;
    }
    
 
    /*public List<String> lrange(String key,Long start,Long end){
        ShardedJedis client = null;
        List<String> list = null;
        try{
            client = this.getJedisClient();
    
            list = client.lrange(key, start, end);
        }finally{
            this.closeJedisClient(client);
        }
        return list;
    }*/
    
    public<T> List<T> lrange(String key,Long start,Long end){
        ShardedJedis client = null;
        List<byte[]> list = null;
        List<T> list1 = new ArrayList<T>();
        try{
            client = this.getJedisClient();
    
            list = client.lrange(key.getBytes(), start, end);
            for(int i = 0;i < list.size();i++){
                @SuppressWarnings("unchecked")
                T t = (T) ReisUtils.unserialize(list.get(i));
                list1.add(t);
            }
        }finally{
            this.closeJedisClient(client);
        }
        return list1;
    }
    
    /**
     * 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。
     * 
     * @param key
     * @param start
     * @param end
     * @return
     */
    public String ltrim(String key,Long start,Long end){
        ShardedJedis client = null;
        String result = null;
        try{
            client = this.getJedisClient();
            result = client.ltrim(key.getBytes(), start, end);
        }finally{
            this.closeJedisClient(client);
        }
        return result;
    }
}