提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.tenant.core.redis; |
H |
2 |
|
4a47e4
|
3 |
import cn.hutool.core.collection.CollUtil; |
e7c126
|
4 |
import com.iailab.framework.redis.core.TimeoutRedisCacheManager; |
H |
5 |
import com.iailab.framework.tenant.core.context.TenantContextHolder; |
|
6 |
import lombok.extern.slf4j.Slf4j; |
|
7 |
import org.springframework.cache.Cache; |
|
8 |
import org.springframework.data.redis.cache.RedisCacheConfiguration; |
|
9 |
import org.springframework.data.redis.cache.RedisCacheManager; |
|
10 |
import org.springframework.data.redis.cache.RedisCacheWriter; |
4a47e4
|
11 |
|
H |
12 |
import java.util.Set; |
e7c126
|
13 |
|
H |
14 |
/** |
|
15 |
* 多租户的 {@link RedisCacheManager} 实现类 |
|
16 |
* |
|
17 |
* 操作指定 name 的 {@link Cache} 时,自动拼接租户后缀,格式为 name + ":" + tenantId + 后缀 |
|
18 |
* |
|
19 |
* @author airhead |
|
20 |
*/ |
|
21 |
@Slf4j |
|
22 |
public class TenantRedisCacheManager extends TimeoutRedisCacheManager { |
|
23 |
|
4a47e4
|
24 |
private final Set<String> ignoreCaches; |
H |
25 |
|
e7c126
|
26 |
public TenantRedisCacheManager(RedisCacheWriter cacheWriter, |
4a47e4
|
27 |
RedisCacheConfiguration defaultCacheConfiguration, |
H |
28 |
Set<String> ignoreCaches) { |
e7c126
|
29 |
super(cacheWriter, defaultCacheConfiguration); |
4a47e4
|
30 |
this.ignoreCaches = ignoreCaches; |
e7c126
|
31 |
} |
H |
32 |
|
|
33 |
@Override |
|
34 |
public Cache getCache(String name) { |
|
35 |
// 如果开启多租户,则 name 拼接租户后缀 |
|
36 |
if (!TenantContextHolder.isIgnore() |
4a47e4
|
37 |
&& TenantContextHolder.getTenantId() != null |
H |
38 |
&& !CollUtil.contains(ignoreCaches, name)) { |
e7c126
|
39 |
name = name + ":" + TenantContextHolder.getTenantId(); |
H |
40 |
} |
|
41 |
|
|
42 |
// 继续基于父方法 |
|
43 |
return super.getCache(name); |
|
44 |
} |
|
45 |
|
|
46 |
} |