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