提交 | 用户 | 时间
|
722af2
|
1 |
package com.dl.framework.handler; |
X |
2 |
|
|
3 |
import com.dl.common.utils.StringUtils; |
|
4 |
import org.redisson.api.NameMapper; |
|
5 |
|
|
6 |
/** |
|
7 |
* redis缓存key前缀处理 |
|
8 |
* |
|
9 |
* @author ye |
|
10 |
* @date 2022/7/14 17:44 |
|
11 |
* @since 4.3.0 |
|
12 |
*/ |
|
13 |
public class KeyPrefixHandler implements NameMapper { |
|
14 |
|
|
15 |
private final String keyPrefix; |
|
16 |
|
|
17 |
public KeyPrefixHandler(String keyPrefix) { |
|
18 |
//前缀为空 则返回空前缀 |
|
19 |
this.keyPrefix = StringUtils.isBlank(keyPrefix) ? "" : keyPrefix + ":"; |
|
20 |
} |
|
21 |
|
|
22 |
/** |
|
23 |
* 增加前缀 |
|
24 |
*/ |
|
25 |
@Override |
|
26 |
public String map(String name) { |
|
27 |
if (StringUtils.isBlank(name)) { |
|
28 |
return null; |
|
29 |
} |
|
30 |
if (StringUtils.isNotBlank(keyPrefix) && !name.startsWith(keyPrefix)) { |
|
31 |
return keyPrefix + name; |
|
32 |
} |
|
33 |
return name; |
|
34 |
} |
|
35 |
|
|
36 |
/** |
|
37 |
* 去除前缀 |
|
38 |
*/ |
|
39 |
@Override |
|
40 |
public String unmap(String name) { |
|
41 |
if (StringUtils.isBlank(name)) { |
|
42 |
return null; |
|
43 |
} |
|
44 |
if (StringUtils.isNotBlank(keyPrefix) && name.startsWith(keyPrefix)) { |
|
45 |
return name.substring(keyPrefix.length()); |
|
46 |
} |
|
47 |
return name; |
|
48 |
} |
|
49 |
|
|
50 |
} |