1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
public class LayeringCacheManager extends RedisCacheManager {
/** 用来存储 cacheName - LayeringCache映射关系 */
private final ConcurrentHashMap<String, LayeringCache> originLayeringCacheMap = new ConcurrentHashMap<>(16);
public LayeringCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) {
super(cacheWriter, defaultCacheConfiguration);
}
public LayeringCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, String... initialCacheNames) {
super(cacheWriter, defaultCacheConfiguration, initialCacheNames);
}
public LayeringCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, boolean allowInFlightCacheCreation, String... initialCacheNames) {
super(cacheWriter, defaultCacheConfiguration, allowInFlightCacheCreation, initialCacheNames);
}
public LayeringCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, Map<String, RedisCacheConfiguration> initialCacheConfigurations) {
super(cacheWriter, defaultCacheConfiguration, initialCacheConfigurations);
}
public LayeringCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, Map<String, RedisCacheConfiguration> initialCacheConfigurations, boolean allowInFlightCacheCreation) {
super(cacheWriter, defaultCacheConfiguration, initialCacheConfigurations, allowInFlightCacheCreation);
}
/**
* 创建 RedisCache,重写方法的关键在这
*/
@SneakyThrows
@Override
protected RedisCache createRedisCache(String name, RedisCacheConfiguration cacheConfig) {
Class<? extends LayeringCacheManager> clazz = this.getClass();
Field cacheWriter = ReflectionUtils.findField(clazz, "cacheWriter");
cacheWriter.setAccessible(true);
LayingRedisCacheWriter cacheWriterVal = (LayingRedisCacheWriter) cacheWriter.get(this);
Field defaultCacheConfig = ReflectionUtils.findField(clazz, "defaultCacheConfig");
defaultCacheConfig.setAccessible(true);
RedisCacheConfiguration defaultCacheConfigVal = (RedisCacheConfiguration) defaultCacheConfig.get(this);
// 使用自定义的 LayeringCache 替换掉原有的 RedisCache
LayeringCache cache = new LayeringCache(name, cacheWriterVal, cacheConfig != null ? cacheConfig : defaultCacheConfigVal);
originLayeringCacheMap.put(name, cache);
return cache;
}
@Override
protected RedisCache getMissingCache(String name) {
return super.getMissingCache(name);
}
@Override
public Cache getCache(String name) {
return super.getCache(name);
}
public LayeringCache getOriginCache(String name) {
return originLayeringCacheMap.get(name);
}
public Collection<LayeringCache> getOriginCaches() {
return originLayeringCacheMap.values();
}
public static class LayeringCacheManagerBuilder {
private @Nullable
RedisCacheWriter cacheWriter;
private RedisCacheConfiguration defaultCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
private final Map<String, RedisCacheConfiguration> initialCaches = new LinkedHashMap<>();
private boolean enableTransactions;
boolean allowInFlightCacheCreation = true;
private LayeringCacheManagerBuilder() {
}
private LayeringCacheManagerBuilder(RedisCacheWriter cacheWriter) {
this.cacheWriter = cacheWriter;
}
public static LayeringCacheManagerBuilder fromConnectionFactory(RedisConnectionFactory connectionFactory) {
Assert.notNull(connectionFactory, "ConnectionFactory must not be null!");
// 这里是个关键点,将RedisCache对象替换为自定义的 LayingRedisCacheWriter
return new LayeringCacheManagerBuilder(new LayingRedisCacheWriter(connectionFactory));
}
public static LayeringCacheManagerBuilder fromCacheWriter(LayingRedisCacheWriter cacheWriter) {
Assert.notNull(cacheWriter, "CacheWriter must not be null!");
return new LayeringCacheManagerBuilder(cacheWriter);
}
public LayeringCacheManagerBuilder cacheDefaults(RedisCacheConfiguration defaultCacheConfiguration) {
Assert.notNull(defaultCacheConfiguration, "DefaultCacheConfiguration must not be null!");
this.defaultCacheConfiguration = defaultCacheConfiguration;
return this;
}
public LayeringCacheManagerBuilder cacheWriter(RedisCacheWriter cacheWriter) {
Assert.notNull(cacheWriter, "CacheWriter must not be null!");
this.cacheWriter = cacheWriter;
return this;
}
public LayeringCacheManagerBuilder transactionAware() {
this.enableTransactions = true;
return this;
}
public LayeringCacheManagerBuilder initialCacheNames(Set<String> cacheNames) {
Assert.notNull(cacheNames, "CacheNames must not be null!");
cacheNames.forEach(it -> withCacheConfiguration(it, defaultCacheConfiguration));
return this;
}
public LayeringCacheManagerBuilder withInitialCacheConfigurations(
Map<String, RedisCacheConfiguration> cacheConfigurations) {
Assert.notNull(cacheConfigurations, "CacheConfigurations must not be null!");
cacheConfigurations.forEach((cacheName, configuration) -> Assert.notNull(configuration,
String.format("RedisCacheConfiguration for cache %s must not be null!", cacheName)));
this.initialCaches.putAll(cacheConfigurations);
return this;
}
public LayeringCacheManagerBuilder withCacheConfiguration(String cacheName,
RedisCacheConfiguration cacheConfiguration) {
Assert.notNull(cacheName, "CacheName must not be null!");
Assert.notNull(cacheConfiguration, "CacheConfiguration must not be null!");
this.initialCaches.put(cacheName, cacheConfiguration);
return this;
}
public LayeringCacheManagerBuilder disableCreateOnMissingCache() {
this.allowInFlightCacheCreation = false;
return this;
}
public Set<String> getConfiguredCaches() {
return Collections.unmodifiableSet(this.initialCaches.keySet());
}
public Optional<RedisCacheConfiguration> getCacheConfigurationFor(String cacheName) {
return Optional.ofNullable(this.initialCaches.get(cacheName));
}
public LayeringCacheManager build() {
Assert.state(cacheWriter != null, "CacheWriter must not be null! You can provide one via 'RedisCacheManagerBuilder#cacheWriter(RedisCacheWriter)'.");
LayeringCacheManager cm = new LayeringCacheManager(cacheWriter, defaultCacheConfiguration, initialCaches,
allowInFlightCacheCreation);
cm.setTransactionAware(enableTransactions);
return cm;
}
}
}
|