Commit d47902bdca2eb772c974803a5bd72e917a294b39
1 parent
9e2630ee
启动时redis未连接导致无法启动
Showing
4 changed files
with
158 additions
and
17 deletions
src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java
| ... | ... | @@ -20,7 +20,6 @@ import org.springframework.stereotype.Service; |
| 20 | 20 | import org.springframework.transaction.TransactionDefinition; |
| 21 | 21 | import org.springframework.transaction.TransactionStatus; |
| 22 | 22 | import org.springframework.util.ObjectUtils; |
| 23 | -import org.springframework.util.StringUtils; | |
| 24 | 23 | |
| 25 | 24 | import com.alibaba.fastjson.JSON; |
| 26 | 25 | import com.alibaba.fastjson.JSONArray; |
| ... | ... | @@ -40,7 +39,6 @@ import com.genersoft.iot.vmp.service.bean.SSRCInfo; |
| 40 | 39 | import com.genersoft.iot.vmp.storager.dao.MediaServerMapper; |
| 41 | 40 | import com.genersoft.iot.vmp.utils.DateUtil; |
| 42 | 41 | import com.genersoft.iot.vmp.utils.redis.RedisUtil; |
| 43 | -import com.genersoft.iot.vmp.vmanager.bean.WVPResult; | |
| 44 | 42 | |
| 45 | 43 | import okhttp3.OkHttpClient; |
| 46 | 44 | import okhttp3.Request; |
| ... | ... | @@ -135,7 +133,7 @@ public class MediaServerServiceImpl implements IMediaServerService { |
| 135 | 133 | logger.info("media server [ {} ] ssrcConfig is null", mediaServerItem.getId()); |
| 136 | 134 | return null; |
| 137 | 135 | }else { |
| 138 | - String ssrc = null; | |
| 136 | + String ssrc; | |
| 139 | 137 | if (presetSsrc != null) { |
| 140 | 138 | ssrc = presetSsrc; |
| 141 | 139 | }else { |
| ... | ... | @@ -497,7 +495,7 @@ public class MediaServerServiceImpl implements IMediaServerService { |
| 497 | 495 | } |
| 498 | 496 | |
| 499 | 497 | // 获取分数最低的,及并发最低的 |
| 500 | - Set<Object> objects = RedisUtil.ZRange(key, 0, -1); | |
| 498 | + Set<Object> objects = RedisUtil.zRange(key, 0, -1); | |
| 501 | 499 | ArrayList<Object> mediaServerObjectS = new ArrayList<>(objects); |
| 502 | 500 | |
| 503 | 501 | String mediaServerId = (String)mediaServerObjectS.get(0); | ... | ... |
src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java
| ... | ... | @@ -21,6 +21,7 @@ import com.genersoft.iot.vmp.utils.redis.RedisUtil; |
| 21 | 21 | import org.slf4j.Logger; |
| 22 | 22 | import org.slf4j.LoggerFactory; |
| 23 | 23 | import org.springframework.beans.factory.annotation.Autowired; |
| 24 | +import org.springframework.context.annotation.DependsOn; | |
| 24 | 25 | import org.springframework.stereotype.Component; |
| 25 | 26 | |
| 26 | 27 | import java.util.*; | ... | ... |
src/main/java/com/genersoft/iot/vmp/utils/SpringBeanFactory.java
| ... | ... | @@ -33,11 +33,11 @@ public class SpringBeanFactory implements ApplicationContextAware { |
| 33 | 33 | /** |
| 34 | 34 | * 获取对象 这里重写了bean方法,起主要作用 |
| 35 | 35 | */ |
| 36 | - public static Object getBean(String beanId) throws BeansException { | |
| 36 | + public static <T> T getBean(String beanId) throws BeansException { | |
| 37 | 37 | if (applicationContext == null) { |
| 38 | 38 | return null; |
| 39 | 39 | } |
| 40 | - return applicationContext.getBean(beanId); | |
| 40 | + return (T) applicationContext.getBean(beanId); | |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | /** | ... | ... |
src/main/java/com/genersoft/iot/vmp/utils/redis/RedisUtil.java
| ... | ... | @@ -5,15 +5,13 @@ import java.util.concurrent.TimeUnit; |
| 5 | 5 | |
| 6 | 6 | import com.alibaba.fastjson.JSONObject; |
| 7 | 7 | import com.genersoft.iot.vmp.utils.SpringBeanFactory; |
| 8 | -import org.springframework.beans.factory.annotation.Autowired; | |
| 9 | 8 | import org.springframework.data.redis.core.*; |
| 10 | -import org.springframework.stereotype.Component; | |
| 11 | 9 | import org.springframework.util.CollectionUtils; |
| 12 | 10 | |
| 13 | 11 | /** |
| 14 | - * @description:Redis工具类 | |
| 15 | - * @author: swwheihei | |
| 16 | - * @date: 2020年5月6日 下午8:27:29 | |
| 12 | + * Redis工具类 | |
| 13 | + * @author swwheihei | |
| 14 | + * @date 2020年5月6日 下午8:27:29 | |
| 17 | 15 | */ |
| 18 | 16 | @SuppressWarnings(value = {"rawtypes", "unchecked"}) |
| 19 | 17 | public class RedisUtil { |
| ... | ... | @@ -21,9 +19,9 @@ public class RedisUtil { |
| 21 | 19 | private static RedisTemplate redisTemplate; |
| 22 | 20 | |
| 23 | 21 | static { |
| 24 | - redisTemplate = (RedisTemplate)SpringBeanFactory.getBean("redisTemplate"); | |
| 22 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 25 | 23 | } |
| 26 | - | |
| 24 | + | |
| 27 | 25 | /** |
| 28 | 26 | * 指定缓存失效时间 |
| 29 | 27 | * @param key 键 |
| ... | ... | @@ -31,6 +29,9 @@ public class RedisUtil { |
| 31 | 29 | * @return true / false |
| 32 | 30 | */ |
| 33 | 31 | public static boolean expire(String key, long time) { |
| 32 | + if (redisTemplate == null) { | |
| 33 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 34 | + } | |
| 34 | 35 | try { |
| 35 | 36 | if (time > 0) { |
| 36 | 37 | redisTemplate.expire(key, time, TimeUnit.SECONDS); |
| ... | ... | @@ -45,9 +46,11 @@ public class RedisUtil { |
| 45 | 46 | /** |
| 46 | 47 | * 根据 key 获取过期时间 |
| 47 | 48 | * @param key 键 |
| 48 | - * @return | |
| 49 | 49 | */ |
| 50 | 50 | public static long getExpire(String key) { |
| 51 | + if (redisTemplate == null) { | |
| 52 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 53 | + } | |
| 51 | 54 | return redisTemplate.getExpire(key, TimeUnit.SECONDS); |
| 52 | 55 | } |
| 53 | 56 | |
| ... | ... | @@ -57,6 +60,9 @@ public class RedisUtil { |
| 57 | 60 | * @return true / false |
| 58 | 61 | */ |
| 59 | 62 | public static boolean hasKey(String key) { |
| 63 | + if (redisTemplate == null) { | |
| 64 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 65 | + } | |
| 60 | 66 | try { |
| 61 | 67 | return redisTemplate.hasKey(key); |
| 62 | 68 | } catch (Exception e) { |
| ... | ... | @@ -71,6 +77,9 @@ public class RedisUtil { |
| 71 | 77 | * @param key 键(一个或者多个) |
| 72 | 78 | */ |
| 73 | 79 | public static boolean del(String... key) { |
| 80 | + if (redisTemplate == null) { | |
| 81 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 82 | + } | |
| 74 | 83 | try { |
| 75 | 84 | if (key != null && key.length > 0) { |
| 76 | 85 | if (key.length == 1) { |
| ... | ... | @@ -95,6 +104,9 @@ public class RedisUtil { |
| 95 | 104 | * @return 值 |
| 96 | 105 | */ |
| 97 | 106 | public static Object get(String key) { |
| 107 | + if (redisTemplate == null) { | |
| 108 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 109 | + } | |
| 98 | 110 | return key == null ? null : redisTemplate.opsForValue().get(key); |
| 99 | 111 | } |
| 100 | 112 | |
| ... | ... | @@ -105,6 +117,9 @@ public class RedisUtil { |
| 105 | 117 | * @return true / false |
| 106 | 118 | */ |
| 107 | 119 | public static boolean set(String key, Object value) { |
| 120 | + if (redisTemplate == null) { | |
| 121 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 122 | + } | |
| 108 | 123 | try { |
| 109 | 124 | redisTemplate.opsForValue().set(key, value); |
| 110 | 125 | return true; |
| ... | ... | @@ -122,6 +137,9 @@ public class RedisUtil { |
| 122 | 137 | * @return true / false |
| 123 | 138 | */ |
| 124 | 139 | public static boolean set(String key, Object value, long time) { |
| 140 | + if (redisTemplate == null) { | |
| 141 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 142 | + } | |
| 125 | 143 | try { |
| 126 | 144 | if (time > 0) { |
| 127 | 145 | redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); |
| ... | ... | @@ -142,6 +160,9 @@ public class RedisUtil { |
| 142 | 160 | * @return |
| 143 | 161 | */ |
| 144 | 162 | public static long incr(String key, long delta) { |
| 163 | + if (redisTemplate == null) { | |
| 164 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 165 | + } | |
| 145 | 166 | if (delta < 0) { |
| 146 | 167 | throw new RuntimeException("递增因子必须大于 0"); |
| 147 | 168 | } |
| ... | ... | @@ -155,6 +176,9 @@ public class RedisUtil { |
| 155 | 176 | * @return |
| 156 | 177 | */ |
| 157 | 178 | public static long decr(String key, long delta) { |
| 179 | + if (redisTemplate == null) { | |
| 180 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 181 | + } | |
| 158 | 182 | if (delta < 0) { |
| 159 | 183 | throw new RuntimeException("递减因子必须大于 0"); |
| 160 | 184 | } |
| ... | ... | @@ -170,6 +194,9 @@ public class RedisUtil { |
| 170 | 194 | * @return 值 |
| 171 | 195 | */ |
| 172 | 196 | public static Object hget(String key, String item) { |
| 197 | + if (redisTemplate == null) { | |
| 198 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 199 | + } | |
| 173 | 200 | return redisTemplate.opsForHash().get(key, item); |
| 174 | 201 | } |
| 175 | 202 | |
| ... | ... | @@ -179,6 +206,9 @@ public class RedisUtil { |
| 179 | 206 | * @return 对应的多个键值 |
| 180 | 207 | */ |
| 181 | 208 | public static Map<Object, Object> hmget(String key) { |
| 209 | + if (redisTemplate == null) { | |
| 210 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 211 | + } | |
| 182 | 212 | return redisTemplate.opsForHash().entries(key); |
| 183 | 213 | } |
| 184 | 214 | |
| ... | ... | @@ -189,6 +219,9 @@ public class RedisUtil { |
| 189 | 219 | * @return true / false |
| 190 | 220 | */ |
| 191 | 221 | public static boolean hmset(String key, Map<Object, Object> map) { |
| 222 | + if (redisTemplate == null) { | |
| 223 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 224 | + } | |
| 192 | 225 | try { |
| 193 | 226 | redisTemplate.opsForHash().putAll(key, map); |
| 194 | 227 | return true; |
| ... | ... | @@ -206,6 +239,9 @@ public class RedisUtil { |
| 206 | 239 | * @return true / false |
| 207 | 240 | */ |
| 208 | 241 | public static boolean hmset(String key, Map<Object, Object> map, long time) { |
| 242 | + if (redisTemplate == null) { | |
| 243 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 244 | + } | |
| 209 | 245 | try { |
| 210 | 246 | redisTemplate.opsForHash().putAll(key, map); |
| 211 | 247 | if (time > 0) { |
| ... | ... | @@ -226,6 +262,9 @@ public class RedisUtil { |
| 226 | 262 | * @return true / false |
| 227 | 263 | */ |
| 228 | 264 | public static boolean hset(String key, String item, Object value) { |
| 265 | + if (redisTemplate == null) { | |
| 266 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 267 | + } | |
| 229 | 268 | try { |
| 230 | 269 | redisTemplate.opsForHash().put(key, item, value); |
| 231 | 270 | return true; |
| ... | ... | @@ -244,6 +283,9 @@ public class RedisUtil { |
| 244 | 283 | * @return true / false |
| 245 | 284 | */ |
| 246 | 285 | public static boolean hset(String key, String item, Object value, long time) { |
| 286 | + if (redisTemplate == null) { | |
| 287 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 288 | + } | |
| 247 | 289 | try { |
| 248 | 290 | redisTemplate.opsForHash().put(key, item, value); |
| 249 | 291 | if (time > 0) { |
| ... | ... | @@ -262,6 +304,9 @@ public class RedisUtil { |
| 262 | 304 | * @param item 项(可以多个,no null) |
| 263 | 305 | */ |
| 264 | 306 | public static void hdel(String key, Object... item) { |
| 307 | + if (redisTemplate == null) { | |
| 308 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 309 | + } | |
| 265 | 310 | redisTemplate.opsForHash().delete(key, item); |
| 266 | 311 | } |
| 267 | 312 | |
| ... | ... | @@ -272,6 +317,9 @@ public class RedisUtil { |
| 272 | 317 | * @return true / false |
| 273 | 318 | */ |
| 274 | 319 | public static boolean hHasKey(String key, String item) { |
| 320 | + if (redisTemplate == null) { | |
| 321 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 322 | + } | |
| 275 | 323 | return redisTemplate.opsForHash().hasKey(key, item); |
| 276 | 324 | } |
| 277 | 325 | |
| ... | ... | @@ -283,6 +331,9 @@ public class RedisUtil { |
| 283 | 331 | * @return |
| 284 | 332 | */ |
| 285 | 333 | public static Double hincr(String key, String item, Double by) { |
| 334 | + if (redisTemplate == null) { | |
| 335 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 336 | + } | |
| 286 | 337 | return redisTemplate.opsForHash().increment(key, item, by); |
| 287 | 338 | } |
| 288 | 339 | |
| ... | ... | @@ -294,6 +345,9 @@ public class RedisUtil { |
| 294 | 345 | * @return |
| 295 | 346 | */ |
| 296 | 347 | public static Double hdecr(String key, String item, Double by) { |
| 348 | + if (redisTemplate == null) { | |
| 349 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 350 | + } | |
| 297 | 351 | return redisTemplate.opsForHash().increment(key, item, -by); |
| 298 | 352 | } |
| 299 | 353 | |
| ... | ... | @@ -305,6 +359,9 @@ public class RedisUtil { |
| 305 | 359 | * @return 值 |
| 306 | 360 | */ |
| 307 | 361 | public static Set<Object> sGet(String key) { |
| 362 | + if (redisTemplate == null) { | |
| 363 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 364 | + } | |
| 308 | 365 | try { |
| 309 | 366 | return redisTemplate.opsForSet().members(key); |
| 310 | 367 | } catch (Exception e) { |
| ... | ... | @@ -320,6 +377,9 @@ public class RedisUtil { |
| 320 | 377 | * @return true / false |
| 321 | 378 | */ |
| 322 | 379 | public static boolean sHasKey(String key, Object value) { |
| 380 | + if (redisTemplate == null) { | |
| 381 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 382 | + } | |
| 323 | 383 | try { |
| 324 | 384 | return redisTemplate.opsForSet().isMember(key, value); |
| 325 | 385 | } catch (Exception e) { |
| ... | ... | @@ -335,6 +395,9 @@ public class RedisUtil { |
| 335 | 395 | * @return 成功个数 |
| 336 | 396 | */ |
| 337 | 397 | public static long sSet(String key, Object... values) { |
| 398 | + if (redisTemplate == null) { | |
| 399 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 400 | + } | |
| 338 | 401 | try { |
| 339 | 402 | return redisTemplate.opsForSet().add(key, values); |
| 340 | 403 | } catch (Exception e) { |
| ... | ... | @@ -351,6 +414,9 @@ public class RedisUtil { |
| 351 | 414 | * @return 成功放入个数 |
| 352 | 415 | */ |
| 353 | 416 | public static long sSet(String key, long time, Object... values) { |
| 417 | + if (redisTemplate == null) { | |
| 418 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 419 | + } | |
| 354 | 420 | try { |
| 355 | 421 | long count = redisTemplate.opsForSet().add(key, values); |
| 356 | 422 | if (time > 0) { |
| ... | ... | @@ -369,6 +435,9 @@ public class RedisUtil { |
| 369 | 435 | * @return 长度 |
| 370 | 436 | */ |
| 371 | 437 | public static long sGetSetSize(String key) { |
| 438 | + if (redisTemplate == null) { | |
| 439 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 440 | + } | |
| 372 | 441 | try { |
| 373 | 442 | return redisTemplate.opsForSet().size(key); |
| 374 | 443 | } catch (Exception e) { |
| ... | ... | @@ -384,6 +453,9 @@ public class RedisUtil { |
| 384 | 453 | * @return 成功移除个数 |
| 385 | 454 | */ |
| 386 | 455 | public static long setRemove(String key, Object... values) { |
| 456 | + if (redisTemplate == null) { | |
| 457 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 458 | + } | |
| 387 | 459 | try { |
| 388 | 460 | return redisTemplate.opsForSet().remove(key, values); |
| 389 | 461 | } catch (Exception e) { |
| ... | ... | @@ -401,6 +473,9 @@ public class RedisUtil { |
| 401 | 473 | * @param score |
| 402 | 474 | */ |
| 403 | 475 | public static void zAdd(Object key, Object value, double score) { |
| 476 | + if (redisTemplate == null) { | |
| 477 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 478 | + } | |
| 404 | 479 | redisTemplate.opsForZSet().add(key, value, score); |
| 405 | 480 | } |
| 406 | 481 | |
| ... | ... | @@ -411,6 +486,9 @@ public class RedisUtil { |
| 411 | 486 | * @param value |
| 412 | 487 | */ |
| 413 | 488 | public static void zRemove(Object key, Object value) { |
| 489 | + if (redisTemplate == null) { | |
| 490 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 491 | + } | |
| 414 | 492 | redisTemplate.opsForZSet().remove(key, value); |
| 415 | 493 | } |
| 416 | 494 | |
| ... | ... | @@ -422,6 +500,9 @@ public class RedisUtil { |
| 422 | 500 | * @param delta -1 表示减 1 表示加1 |
| 423 | 501 | */ |
| 424 | 502 | public static Double zIncrScore(Object key, Object value, double delta) { |
| 503 | + if (redisTemplate == null) { | |
| 504 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 505 | + } | |
| 425 | 506 | return redisTemplate.opsForZSet().incrementScore(key, value, delta); |
| 426 | 507 | } |
| 427 | 508 | |
| ... | ... | @@ -433,6 +514,9 @@ public class RedisUtil { |
| 433 | 514 | * @return |
| 434 | 515 | */ |
| 435 | 516 | public static Double zScore(Object key, Object value) { |
| 517 | + if (redisTemplate == null) { | |
| 518 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 519 | + } | |
| 436 | 520 | return redisTemplate.opsForZSet().score(key, value); |
| 437 | 521 | } |
| 438 | 522 | |
| ... | ... | @@ -444,6 +528,9 @@ public class RedisUtil { |
| 444 | 528 | * @return |
| 445 | 529 | */ |
| 446 | 530 | public static Long zRank(Object key, Object value) { |
| 531 | + if (redisTemplate == null) { | |
| 532 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 533 | + } | |
| 447 | 534 | return redisTemplate.opsForZSet().rank(key, value); |
| 448 | 535 | } |
| 449 | 536 | |
| ... | ... | @@ -454,6 +541,9 @@ public class RedisUtil { |
| 454 | 541 | * @return |
| 455 | 542 | */ |
| 456 | 543 | public static Long zSize(Object key) { |
| 544 | + if (redisTemplate == null) { | |
| 545 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 546 | + } | |
| 457 | 547 | return redisTemplate.opsForZSet().zCard(key); |
| 458 | 548 | } |
| 459 | 549 | |
| ... | ... | @@ -467,7 +557,10 @@ public class RedisUtil { |
| 467 | 557 | * @param end |
| 468 | 558 | * @return |
| 469 | 559 | */ |
| 470 | - public static Set<Object> ZRange(Object key, int start, int end) { | |
| 560 | + public static Set<Object> zRange(Object key, int start, int end) { | |
| 561 | + if (redisTemplate == null) { | |
| 562 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 563 | + } | |
| 471 | 564 | return redisTemplate.opsForZSet().range(key, start, end); |
| 472 | 565 | } |
| 473 | 566 | /** |
| ... | ... | @@ -479,6 +572,9 @@ public class RedisUtil { |
| 479 | 572 | * @return |
| 480 | 573 | */ |
| 481 | 574 | public static Set<ZSetOperations.TypedTuple<String>> zRangeWithScore(Object key, int start, int end) { |
| 575 | + if (redisTemplate == null) { | |
| 576 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 577 | + } | |
| 482 | 578 | return redisTemplate.opsForZSet().rangeWithScores(key, start, end); |
| 483 | 579 | } |
| 484 | 580 | /** |
| ... | ... | @@ -492,6 +588,9 @@ public class RedisUtil { |
| 492 | 588 | * @return |
| 493 | 589 | */ |
| 494 | 590 | public static Set<String> zRevRange(Object key, int start, int end) { |
| 591 | + if (redisTemplate == null) { | |
| 592 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 593 | + } | |
| 495 | 594 | return redisTemplate.opsForZSet().reverseRange(key, start, end); |
| 496 | 595 | } |
| 497 | 596 | /** |
| ... | ... | @@ -503,6 +602,9 @@ public class RedisUtil { |
| 503 | 602 | * @return |
| 504 | 603 | */ |
| 505 | 604 | public static Set<String> zSortRange(Object key, int min, int max) { |
| 605 | + if (redisTemplate == null) { | |
| 606 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 607 | + } | |
| 506 | 608 | return redisTemplate.opsForZSet().rangeByScore(key, min, max); |
| 507 | 609 | } |
| 508 | 610 | |
| ... | ... | @@ -517,6 +619,9 @@ public class RedisUtil { |
| 517 | 619 | * @return |
| 518 | 620 | */ |
| 519 | 621 | public static List<Object> lGet(String key, long start, long end) { |
| 622 | + if (redisTemplate == null) { | |
| 623 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 624 | + } | |
| 520 | 625 | try { |
| 521 | 626 | return redisTemplate.opsForList().range(key, start, end); |
| 522 | 627 | } catch (Exception e) { |
| ... | ... | @@ -531,6 +636,9 @@ public class RedisUtil { |
| 531 | 636 | * @return 长度 |
| 532 | 637 | */ |
| 533 | 638 | public static long lGetListSize(String key) { |
| 639 | + if (redisTemplate == null) { | |
| 640 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 641 | + } | |
| 534 | 642 | try { |
| 535 | 643 | return redisTemplate.opsForList().size(key); |
| 536 | 644 | } catch (Exception e) { |
| ... | ... | @@ -548,6 +656,9 @@ public class RedisUtil { |
| 548 | 656 | * @return 值 |
| 549 | 657 | */ |
| 550 | 658 | public static Object lGetIndex(String key, long index) { |
| 659 | + if (redisTemplate == null) { | |
| 660 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 661 | + } | |
| 551 | 662 | try { |
| 552 | 663 | return redisTemplate.opsForList().index(key, index); |
| 553 | 664 | } catch (Exception e) { |
| ... | ... | @@ -563,6 +674,9 @@ public class RedisUtil { |
| 563 | 674 | * @return true / false |
| 564 | 675 | */ |
| 565 | 676 | public static boolean lSet(String key, Object value) { |
| 677 | + if (redisTemplate == null) { | |
| 678 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 679 | + } | |
| 566 | 680 | try { |
| 567 | 681 | redisTemplate.opsForList().rightPush(key, value); |
| 568 | 682 | return true; |
| ... | ... | @@ -580,6 +694,9 @@ public class RedisUtil { |
| 580 | 694 | * @return true / false |
| 581 | 695 | */ |
| 582 | 696 | public static boolean lSet(String key, Object value, long time) { |
| 697 | + if (redisTemplate == null) { | |
| 698 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 699 | + } | |
| 583 | 700 | try { |
| 584 | 701 | redisTemplate.opsForList().rightPush(key, value); |
| 585 | 702 | if (time > 0) { |
| ... | ... | @@ -599,6 +716,9 @@ public class RedisUtil { |
| 599 | 716 | * @return true / false |
| 600 | 717 | */ |
| 601 | 718 | public static boolean lSetList(String key, List<Object> values) { |
| 719 | + if (redisTemplate == null) { | |
| 720 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 721 | + } | |
| 602 | 722 | try { |
| 603 | 723 | redisTemplate.opsForList().rightPushAll(key, values); |
| 604 | 724 | return true; |
| ... | ... | @@ -616,6 +736,9 @@ public class RedisUtil { |
| 616 | 736 | * @return true / false |
| 617 | 737 | */ |
| 618 | 738 | public static boolean lSetList(String key, List<Object> values, long time) { |
| 739 | + if (redisTemplate == null) { | |
| 740 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 741 | + } | |
| 619 | 742 | try { |
| 620 | 743 | redisTemplate.opsForList().rightPushAll(key, values); |
| 621 | 744 | if (time > 0) { |
| ... | ... | @@ -636,6 +759,9 @@ public class RedisUtil { |
| 636 | 759 | * @return true / false |
| 637 | 760 | */ |
| 638 | 761 | public static boolean lUpdateIndex(String key, long index, Object value) { |
| 762 | + if (redisTemplate == null) { | |
| 763 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 764 | + } | |
| 639 | 765 | try { |
| 640 | 766 | redisTemplate.opsForList().set(key, index, value); |
| 641 | 767 | return true; |
| ... | ... | @@ -655,6 +781,9 @@ public class RedisUtil { |
| 655 | 781 | * @return |
| 656 | 782 | */ |
| 657 | 783 | public static long lRemove(String key, long count, Object value) { |
| 784 | + if (redisTemplate == null) { | |
| 785 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 786 | + } | |
| 658 | 787 | try { |
| 659 | 788 | return redisTemplate.opsForList().remove(key, count, value); |
| 660 | 789 | } catch (Exception e) { |
| ... | ... | @@ -669,6 +798,9 @@ public class RedisUtil { |
| 669 | 798 | * @return |
| 670 | 799 | */ |
| 671 | 800 | public static Object lLeftPop(String key) { |
| 801 | + if (redisTemplate == null) { | |
| 802 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 803 | + } | |
| 672 | 804 | return redisTemplate.opsForList().leftPop(key); |
| 673 | 805 | } |
| 674 | 806 | |
| ... | ... | @@ -678,6 +810,9 @@ public class RedisUtil { |
| 678 | 810 | * @return |
| 679 | 811 | */ |
| 680 | 812 | public static Object lrightPop(String key) { |
| 813 | + if (redisTemplate == null) { | |
| 814 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 815 | + } | |
| 681 | 816 | return redisTemplate.opsForList().rightPop(key); |
| 682 | 817 | } |
| 683 | 818 | |
| ... | ... | @@ -687,6 +822,9 @@ public class RedisUtil { |
| 687 | 822 | * @return true / false |
| 688 | 823 | */ |
| 689 | 824 | public static List<Object> keys(String key) { |
| 825 | + if (redisTemplate == null) { | |
| 826 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 827 | + } | |
| 690 | 828 | try { |
| 691 | 829 | Set<String> set = redisTemplate.keys(key); |
| 692 | 830 | return new ArrayList<>(set); |
| ... | ... | @@ -727,6 +865,9 @@ public class RedisUtil { |
| 727 | 865 | * @return |
| 728 | 866 | */ |
| 729 | 867 | public static List<Object> scan(String query) { |
| 868 | + if (redisTemplate == null) { | |
| 869 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 870 | + } | |
| 730 | 871 | Set<String> resultKeys = (Set<String>) redisTemplate.execute((RedisCallback<Set<String>>) connection -> { |
| 731 | 872 | ScanOptions scanOptions = ScanOptions.scanOptions().match("*" + query + "*").count(1000).build(); |
| 732 | 873 | Cursor<byte[]> scan = connection.scan(scanOptions); |
| ... | ... | @@ -743,9 +884,10 @@ public class RedisUtil { |
| 743 | 884 | |
| 744 | 885 | // ============================== 消息发送与订阅 ============================== |
| 745 | 886 | public static void convertAndSend(String channel, JSONObject msg) { |
| 746 | -// redisTemplate.convertAndSend(channel, msg); | |
| 887 | + if (redisTemplate == null) { | |
| 888 | + redisTemplate = SpringBeanFactory.getBean("redisTemplate"); | |
| 889 | + } | |
| 747 | 890 | redisTemplate.convertAndSend(channel, msg); |
| 748 | - | |
| 749 | 891 | } |
| 750 | 892 | |
| 751 | 893 | } | ... | ... |