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