提交 | 用户 | age
|
826b66
|
1 |
package com.hx.redisson.manager; |
C |
2 |
|
|
3 |
import org.redisson.api.RedissonClient; |
|
4 |
import org.springframework.data.redis.core.RedisTemplate; |
|
5 |
|
|
6 |
import java.util.Map; |
|
7 |
import java.util.concurrent.ConcurrentHashMap; |
|
8 |
|
|
9 |
/** |
|
10 |
* redis管理 |
|
11 |
* |
|
12 |
* @author CJH |
|
13 |
* @date 2022-06-27 |
|
14 |
*/ |
|
15 |
public class RedisManager { |
|
16 |
|
|
17 |
private Map<String, RedisTemplate> redisTemplateMap = new ConcurrentHashMap<>(); |
|
18 |
|
|
19 |
public static Map<String, RedissonClient> redissonTemplateMap = new ConcurrentHashMap<>(); |
|
20 |
|
|
21 |
/** |
|
22 |
* 构造方法初始化 redisTemplateMap 的数据 |
|
23 |
* |
|
24 |
* @param redisTemplateMap |
|
25 |
*/ |
|
26 |
public RedisManager(Map<String, RedisTemplate> redisTemplateMap) { |
|
27 |
this.redisTemplateMap = redisTemplateMap; |
|
28 |
} |
|
29 |
|
|
30 |
|
|
31 |
/** |
|
32 |
* 根据数据库序号,返回对应的RedisTemplate |
|
33 |
* |
|
34 |
* @param dbIndex 序号 |
|
35 |
* @return {@link RedisTemplate} |
|
36 |
*/ |
|
37 |
public RedisTemplate getRedisTemplate(Integer dbIndex) { |
|
38 |
RedisTemplate redisTemplate = redisTemplateMap.get("redisTemplate" + dbIndex); |
|
39 |
if (redisTemplate == null) { |
|
40 |
throw new RuntimeException("Map不存在该redisTemplate"); |
|
41 |
} |
|
42 |
return redisTemplate; |
|
43 |
} |
|
44 |
|
|
45 |
|
|
46 |
} |