Skip to content

Commit f75d4e1

Browse files
committed
Polish cache abstraction code
Polish cache abstraction code and refactor CacheAspectSupport.
1 parent b122ca6 commit f75d4e1

14 files changed

+305
-346
lines changed

spring-context/src/main/java/org/springframework/cache/annotation/AbstractCachingConfiguration.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,18 @@
4242
public abstract class AbstractCachingConfiguration implements ImportAware {
4343

4444
protected AnnotationAttributes enableCaching;
45+
4546
protected CacheManager cacheManager;
47+
4648
protected KeyGenerator keyGenerator;
4749

4850
@Autowired(required=false)
4951
private Collection<CacheManager> cacheManagerBeans;
52+
5053
@Autowired(required=false)
5154
private Collection<CachingConfigurer> cachingConfigurers;
5255

56+
5357
@Override
5458
public void setImportMetadata(AnnotationMetadata importMetadata) {
5559
this.enableCaching = AnnotationAttributes.fromMap(
@@ -59,6 +63,7 @@ public void setImportMetadata(AnnotationMetadata importMetadata) {
5963
importMetadata.getClassName());
6064
}
6165

66+
6267
/**
6368
* Determine which {@code CacheManager} bean to use. Prefer the result of
6469
* {@link CachingConfigurer#cacheManager()} over any by-type matching. If none, fall

spring-context/src/main/java/org/springframework/cache/config/CacheAdviceParser.java

Lines changed: 67 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -48,69 +48,17 @@
4848
*/
4949
class CacheAdviceParser extends AbstractSingleBeanDefinitionParser {
5050

51-
/**
52-
* Simple, reusable class used for overriding defaults.
53-
*
54-
* @author Costin Leau
55-
*/
56-
private static class Props {
57-
58-
private String key;
59-
private String condition;
60-
private String method;
61-
private String[] caches = null;
62-
63-
Props(Element root) {
64-
String defaultCache = root.getAttribute("cache");
65-
key = root.getAttribute("key");
66-
condition = root.getAttribute("condition");
67-
method = root.getAttribute(METHOD_ATTRIBUTE);
68-
69-
if (StringUtils.hasText(defaultCache)) {
70-
caches = StringUtils.commaDelimitedListToStringArray(defaultCache.trim());
71-
}
72-
}
73-
74-
<T extends CacheOperation> T merge(Element element, ReaderContext readerCtx, T op) {
75-
String cache = element.getAttribute("cache");
76-
77-
// sanity check
78-
String[] localCaches = caches;
79-
if (StringUtils.hasText(cache)) {
80-
localCaches = StringUtils.commaDelimitedListToStringArray(cache.trim());
81-
} else {
82-
if (caches == null) {
83-
readerCtx.error("No cache specified specified for " + element.getNodeName(), element);
84-
}
85-
}
86-
op.setCacheNames(localCaches);
87-
88-
op.setKey(getAttributeValue(element, "key", this.key));
89-
op.setCondition(getAttributeValue(element, "condition", this.condition));
90-
91-
return op;
92-
}
93-
94-
String merge(Element element, ReaderContext readerCtx) {
95-
String m = element.getAttribute(METHOD_ATTRIBUTE);
96-
97-
if (StringUtils.hasText(m)) {
98-
return m.trim();
99-
}
100-
if (StringUtils.hasText(method)) {
101-
return method;
102-
}
103-
readerCtx.error("No method specified for " + element.getNodeName(), element);
104-
return null;
105-
}
106-
}
107-
10851
private static final String CACHEABLE_ELEMENT = "cacheable";
52+
10953
private static final String CACHE_EVICT_ELEMENT = "cache-evict";
54+
11055
private static final String CACHE_PUT_ELEMENT = "cache-put";
56+
11157
private static final String METHOD_ATTRIBUTE = "method";
58+
11259
private static final String DEFS_ELEMENT = "caching";
11360

61+
11462
@Override
11563
protected Class<?> getBeanClass(Element element) {
11664
return CacheInterceptor.class;
@@ -226,4 +174,66 @@ private static String getAttributeValue(Element element, String attributeName, S
226174
return defaultValue;
227175
}
228176

177+
178+
/**
179+
* Simple, reusable class used for overriding defaults.
180+
*
181+
* @author Costin Leau
182+
*/
183+
private static class Props {
184+
185+
private String key;
186+
187+
private String condition;
188+
189+
private String method;
190+
191+
private String[] caches = null;
192+
193+
194+
Props(Element root) {
195+
String defaultCache = root.getAttribute("cache");
196+
key = root.getAttribute("key");
197+
condition = root.getAttribute("condition");
198+
method = root.getAttribute(METHOD_ATTRIBUTE);
199+
200+
if (StringUtils.hasText(defaultCache)) {
201+
caches = StringUtils.commaDelimitedListToStringArray(defaultCache.trim());
202+
}
203+
}
204+
205+
206+
<T extends CacheOperation> T merge(Element element, ReaderContext readerCtx, T op) {
207+
String cache = element.getAttribute("cache");
208+
209+
// sanity check
210+
String[] localCaches = caches;
211+
if (StringUtils.hasText(cache)) {
212+
localCaches = StringUtils.commaDelimitedListToStringArray(cache.trim());
213+
} else {
214+
if (caches == null) {
215+
readerCtx.error("No cache specified specified for " + element.getNodeName(), element);
216+
}
217+
}
218+
op.setCacheNames(localCaches);
219+
220+
op.setKey(getAttributeValue(element, "key", this.key));
221+
op.setCondition(getAttributeValue(element, "condition", this.condition));
222+
223+
return op;
224+
}
225+
226+
String merge(Element element, ReaderContext readerCtx) {
227+
String m = element.getAttribute(METHOD_ATTRIBUTE);
228+
229+
if (StringUtils.hasText(m)) {
230+
return m.trim();
231+
}
232+
if (StringUtils.hasText(method)) {
233+
return method;
234+
}
235+
readerCtx.error("No method specified for " + element.getNodeName(), element);
236+
return null;
237+
}
238+
}
229239
}

spring-context/src/main/java/org/springframework/cache/config/CacheNamespaceHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
public class CacheNamespaceHandler extends NamespaceHandlerSupport {
3636

3737
static final String CACHE_MANAGER_ATTRIBUTE = "cache-manager";
38+
3839
static final String DEFAULT_CACHE_MANAGER_BEAN_NAME = "cacheManager";
3940

4041
static String extractCacheManager(Element element) {

0 commit comments

Comments
 (0)