Decrease allocation rate for CacheOperation

This commit is contained in:
soldierkam 2015-12-27 22:47:21 +01:00 committed by Stephane Nicoll
parent 7dac2e3256
commit c73e52412a
6 changed files with 322 additions and 214 deletions

View File

@ -98,56 +98,59 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
} }
CacheableOperation parseCacheableAnnotation(AnnotatedElement ae, DefaultCacheConfig defaultConfig, Cacheable cacheable) { CacheableOperation parseCacheableAnnotation(AnnotatedElement ae, DefaultCacheConfig defaultConfig, Cacheable cacheable) {
CacheableOperation op = new CacheableOperation(); CacheableOperation.Builder opBuilder = new CacheableOperation.Builder();
op.setCacheNames(cacheable.cacheNames()); opBuilder.setCacheNames(cacheable.cacheNames());
op.setCondition(cacheable.condition()); opBuilder.setCondition(cacheable.condition());
op.setUnless(cacheable.unless()); opBuilder.setUnless(cacheable.unless());
op.setKey(cacheable.key()); opBuilder.setKey(cacheable.key());
op.setKeyGenerator(cacheable.keyGenerator()); opBuilder.setKeyGenerator(cacheable.keyGenerator());
op.setCacheManager(cacheable.cacheManager()); opBuilder.setCacheManager(cacheable.cacheManager());
op.setCacheResolver(cacheable.cacheResolver()); opBuilder.setCacheResolver(cacheable.cacheResolver());
op.setSync(cacheable.sync()); opBuilder.setSync(cacheable.sync());
op.setName(ae.toString()); opBuilder.setName(ae.toString());
defaultConfig.applyDefault(op); defaultConfig.applyDefault(opBuilder);
CacheableOperation op = opBuilder.build();
validateCacheOperation(ae, op); validateCacheOperation(ae, op);
return op; return op;
} }
CacheEvictOperation parseEvictAnnotation(AnnotatedElement ae, DefaultCacheConfig defaultConfig, CacheEvict cacheEvict) { CacheEvictOperation parseEvictAnnotation(AnnotatedElement ae, DefaultCacheConfig defaultConfig, CacheEvict cacheEvict) {
CacheEvictOperation op = new CacheEvictOperation(); CacheEvictOperation.Builder opBuilder = new CacheEvictOperation.Builder();
op.setCacheNames(cacheEvict.cacheNames()); opBuilder.setCacheNames(cacheEvict.cacheNames());
op.setCondition(cacheEvict.condition()); opBuilder.setCondition(cacheEvict.condition());
op.setKey(cacheEvict.key()); opBuilder.setKey(cacheEvict.key());
op.setKeyGenerator(cacheEvict.keyGenerator()); opBuilder.setKeyGenerator(cacheEvict.keyGenerator());
op.setCacheManager(cacheEvict.cacheManager()); opBuilder.setCacheManager(cacheEvict.cacheManager());
op.setCacheResolver(cacheEvict.cacheResolver()); opBuilder.setCacheResolver(cacheEvict.cacheResolver());
op.setCacheWide(cacheEvict.allEntries()); opBuilder.setCacheWide(cacheEvict.allEntries());
op.setBeforeInvocation(cacheEvict.beforeInvocation()); opBuilder.setBeforeInvocation(cacheEvict.beforeInvocation());
op.setName(ae.toString()); opBuilder.setName(ae.toString());
defaultConfig.applyDefault(op); defaultConfig.applyDefault(opBuilder);
CacheEvictOperation op = opBuilder.build();
validateCacheOperation(ae, op); validateCacheOperation(ae, op);
return op; return op;
} }
CacheOperation parsePutAnnotation(AnnotatedElement ae, DefaultCacheConfig defaultConfig, CachePut cachePut) { CacheOperation parsePutAnnotation(AnnotatedElement ae, DefaultCacheConfig defaultConfig, CachePut cachePut) {
CachePutOperation op = new CachePutOperation(); CachePutOperation.Builder opBuilder = new CachePutOperation.Builder();
op.setCacheNames(cachePut.cacheNames()); opBuilder.setCacheNames(cachePut.cacheNames());
op.setCondition(cachePut.condition()); opBuilder.setCondition(cachePut.condition());
op.setUnless(cachePut.unless()); opBuilder.setUnless(cachePut.unless());
op.setKey(cachePut.key()); opBuilder.setKey(cachePut.key());
op.setKeyGenerator(cachePut.keyGenerator()); opBuilder.setKeyGenerator(cachePut.keyGenerator());
op.setCacheManager(cachePut.cacheManager()); opBuilder.setCacheManager(cachePut.cacheManager());
op.setCacheResolver(cachePut.cacheResolver()); opBuilder.setCacheResolver(cachePut.cacheResolver());
op.setName(ae.toString()); opBuilder.setName(ae.toString());
defaultConfig.applyDefault(op); defaultConfig.applyDefault(opBuilder);
CachePutOperation op = opBuilder.build();
validateCacheOperation(ae, op); validateCacheOperation(ae, op);
return op; return op;
@ -278,7 +281,7 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
* Apply the defaults to the specified {@link CacheOperation}. * Apply the defaults to the specified {@link CacheOperation}.
* @param operation the operation to update * @param operation the operation to update
*/ */
public void applyDefault(CacheOperation operation) { public void applyDefault(CacheOperation.Builder operation) {
if (operation.getCacheNames().isEmpty() && this.cacheNames != null) { if (operation.getCacheNames().isEmpty() && this.cacheNames != null) {
operation.setCacheNames(this.cacheNames); operation.setCacheNames(this.cacheNames);
} }

View File

@ -107,7 +107,7 @@ class CacheAdviceParser extends AbstractSingleBeanDefinitionParser {
String name = prop.merge(opElement, parserContext.getReaderContext()); String name = prop.merge(opElement, parserContext.getReaderContext());
TypedStringValue nameHolder = new TypedStringValue(name); TypedStringValue nameHolder = new TypedStringValue(name);
nameHolder.setSource(parserContext.extractSource(opElement)); nameHolder.setSource(parserContext.extractSource(opElement));
CacheableOperation op = prop.merge(opElement, parserContext.getReaderContext(), new CacheableOperation()); CacheableOperation.Builder op = prop.merge(opElement, parserContext.getReaderContext(), new CacheableOperation.Builder());
op.setUnless(getAttributeValue(opElement, "unless", "")); op.setUnless(getAttributeValue(opElement, "unless", ""));
op.setSync(Boolean.valueOf(getAttributeValue(opElement, "sync", "false"))); op.setSync(Boolean.valueOf(getAttributeValue(opElement, "sync", "false")));
@ -116,7 +116,7 @@ class CacheAdviceParser extends AbstractSingleBeanDefinitionParser {
col = new ArrayList<CacheOperation>(2); col = new ArrayList<CacheOperation>(2);
cacheOpMap.put(nameHolder, col); cacheOpMap.put(nameHolder, col);
} }
col.add(op); col.add(op.build());
} }
List<Element> evictCacheMethods = DomUtils.getChildElementsByTagName(definition, CACHE_EVICT_ELEMENT); List<Element> evictCacheMethods = DomUtils.getChildElementsByTagName(definition, CACHE_EVICT_ELEMENT);
@ -125,7 +125,7 @@ class CacheAdviceParser extends AbstractSingleBeanDefinitionParser {
String name = prop.merge(opElement, parserContext.getReaderContext()); String name = prop.merge(opElement, parserContext.getReaderContext());
TypedStringValue nameHolder = new TypedStringValue(name); TypedStringValue nameHolder = new TypedStringValue(name);
nameHolder.setSource(parserContext.extractSource(opElement)); nameHolder.setSource(parserContext.extractSource(opElement));
CacheEvictOperation op = prop.merge(opElement, parserContext.getReaderContext(), new CacheEvictOperation()); CacheEvictOperation.Builder op = prop.merge(opElement, parserContext.getReaderContext(), new CacheEvictOperation.Builder());
String wide = opElement.getAttribute("all-entries"); String wide = opElement.getAttribute("all-entries");
if (StringUtils.hasText(wide)) { if (StringUtils.hasText(wide)) {
@ -142,7 +142,7 @@ class CacheAdviceParser extends AbstractSingleBeanDefinitionParser {
col = new ArrayList<CacheOperation>(2); col = new ArrayList<CacheOperation>(2);
cacheOpMap.put(nameHolder, col); cacheOpMap.put(nameHolder, col);
} }
col.add(op); col.add(op.build());
} }
List<Element> putCacheMethods = DomUtils.getChildElementsByTagName(definition, CACHE_PUT_ELEMENT); List<Element> putCacheMethods = DomUtils.getChildElementsByTagName(definition, CACHE_PUT_ELEMENT);
@ -151,7 +151,7 @@ class CacheAdviceParser extends AbstractSingleBeanDefinitionParser {
String name = prop.merge(opElement, parserContext.getReaderContext()); String name = prop.merge(opElement, parserContext.getReaderContext());
TypedStringValue nameHolder = new TypedStringValue(name); TypedStringValue nameHolder = new TypedStringValue(name);
nameHolder.setSource(parserContext.extractSource(opElement)); nameHolder.setSource(parserContext.extractSource(opElement));
CachePutOperation op = prop.merge(opElement, parserContext.getReaderContext(), new CachePutOperation()); CachePutOperation.Builder op = prop.merge(opElement, parserContext.getReaderContext(), new CachePutOperation.Builder());
op.setUnless(getAttributeValue(opElement, "unless", "")); op.setUnless(getAttributeValue(opElement, "unless", ""));
Collection<CacheOperation> col = cacheOpMap.get(nameHolder); Collection<CacheOperation> col = cacheOpMap.get(nameHolder);
@ -159,7 +159,7 @@ class CacheAdviceParser extends AbstractSingleBeanDefinitionParser {
col = new ArrayList<CacheOperation>(2); col = new ArrayList<CacheOperation>(2);
cacheOpMap.put(nameHolder, col); cacheOpMap.put(nameHolder, col);
} }
col.add(op); col.add(op.build());
} }
RootBeanDefinition attributeSourceDefinition = new RootBeanDefinition(NameMatchCacheOperationSource.class); RootBeanDefinition attributeSourceDefinition = new RootBeanDefinition(NameMatchCacheOperationSource.class);
@ -208,7 +208,7 @@ class CacheAdviceParser extends AbstractSingleBeanDefinitionParser {
} }
} }
<T extends CacheOperation> T merge(Element element, ReaderContext readerCtx, T op) { <T extends CacheOperation.Builder> T merge(Element element, ReaderContext readerCtx, T op) {
String cache = element.getAttribute("cache"); String cache = element.getAttribute("cache");
// sanity check // sanity check

View File

@ -24,34 +24,49 @@ package org.springframework.cache.interceptor;
*/ */
public class CacheEvictOperation extends CacheOperation { public class CacheEvictOperation extends CacheOperation {
private boolean cacheWide = false; private final boolean cacheWide;
private boolean beforeInvocation = false; private final boolean beforeInvocation;
public boolean isCacheWide() {
return this.cacheWide;
}
public void setCacheWide(boolean cacheWide) { public boolean isBeforeInvocation() {
this.cacheWide = cacheWide; return this.beforeInvocation;
} }
public boolean isCacheWide() { public CacheEvictOperation(CacheEvictOperation.Builder b) {
return this.cacheWide; super(b);
} this.cacheWide = b.cacheWide;
this.beforeInvocation = b.beforeInvocation;
}
public void setBeforeInvocation(boolean beforeInvocation) { public static class Builder extends CacheOperation.Builder {
this.beforeInvocation = beforeInvocation; private boolean cacheWide = false;
}
public boolean isBeforeInvocation() { private boolean beforeInvocation = false;
return this.beforeInvocation;
}
@Override public void setCacheWide(boolean cacheWide) {
protected StringBuilder getOperationDescription() { this.cacheWide = cacheWide;
StringBuilder sb = super.getOperationDescription(); }
sb.append(",");
sb.append(this.cacheWide); public void setBeforeInvocation(boolean beforeInvocation) {
sb.append(","); this.beforeInvocation = beforeInvocation;
sb.append(this.beforeInvocation); }
return sb;
} @Override
protected StringBuilder getOperationDescription() {
StringBuilder sb = super.getOperationDescription();
sb.append(",");
sb.append(this.cacheWide);
sb.append(",");
sb.append(this.beforeInvocation);
return sb;
}
public CacheEvictOperation build() {
return new CacheEvictOperation(this);
}
}
} }

View File

@ -31,137 +31,197 @@ import org.springframework.util.Assert;
*/ */
public abstract class CacheOperation implements BasicOperation { public abstract class CacheOperation implements BasicOperation {
private String name = ""; private final String name;
private Set<String> cacheNames = Collections.emptySet(); private final Set<String> cacheNames;
private String key = ""; private final String key;
private String keyGenerator = ""; private final String keyGenerator;
private String cacheManager = ""; private final String cacheManager;
private String cacheResolver = ""; private final String cacheResolver;
private String condition = ""; private final String condition;
private final String toString;
protected CacheOperation(Builder b) {
this.name = b.name;
this.cacheNames = b.cacheNames;
this.key = b.key;
this.keyGenerator = b.keyGenerator;
this.cacheManager = b.cacheManager;
this.cacheResolver = b.cacheResolver;
this.condition = b.condition;
this.toString = b.getOperationDescription().toString();
}
public String getName() {
return this.name;
}
public void setName(String name) { @Override
Assert.hasText(name); public Set<String> getCacheNames() {
this.name = name; return this.cacheNames;
} }
public String getName() {
return this.name;
}
public void setCacheName(String cacheName) {
Assert.hasText(cacheName);
this.cacheNames = Collections.singleton(cacheName);
}
public void setCacheNames(String... cacheNames) {
this.cacheNames = new LinkedHashSet<String>(cacheNames.length);
for (String cacheName : cacheNames) {
Assert.hasText(cacheName, "Cache name must be non-null if specified");
this.cacheNames.add(cacheName);
}
}
@Override
public Set<String> getCacheNames() {
return this.cacheNames;
}
public void setKey(String key) {
Assert.notNull(key);
this.key = key;
}
public String getKey() {
return this.key;
}
public void setKeyGenerator(String keyGenerator) {
Assert.notNull(keyGenerator);
this.keyGenerator = keyGenerator;
}
public String getKeyGenerator() {
return this.keyGenerator;
}
public void setCacheManager(String cacheManager) {
Assert.notNull(cacheManager);
this.cacheManager = cacheManager;
}
public String getCacheManager() {
return this.cacheManager;
}
public void setCacheResolver(String cacheResolver) {
Assert.notNull(this.cacheManager);
this.cacheResolver = cacheResolver;
}
public String getCacheResolver() {
return this.cacheResolver;
}
public void setCondition(String condition) {
Assert.notNull(condition);
this.condition = condition;
}
public String getCondition() {
return this.condition;
}
/** public String getKey() {
* This implementation compares the {@code toString()} results. return this.key;
* @see #toString() }
*/
@Override
public boolean equals(Object other) {
return (other instanceof CacheOperation && toString().equals(other.toString()));
}
/**
* This implementation returns {@code toString()}'s hash code.
* @see #toString()
*/
@Override
public int hashCode() {
return toString().hashCode();
}
/** public String getKeyGenerator() {
* Return an identifying description for this cache operation. return this.keyGenerator;
* <p>Has to be overridden in subclasses for correct {@code equals} }
* and {@code hashCode} behavior. Alternatively, {@link #equals}
* and {@link #hashCode} can be overridden themselves.
*/
@Override
public String toString() {
return getOperationDescription().toString();
}
/**
* Return an identifying description for this caching operation. public String getCacheManager() {
* <p>Available to subclasses, for inclusion in their {@code toString()} result. return this.cacheManager;
*/ }
protected StringBuilder getOperationDescription() {
StringBuilder result = new StringBuilder(getClass().getSimpleName());
result.append("[").append(this.name); public String getCacheResolver() {
result.append("] caches=").append(this.cacheNames); return this.cacheResolver;
result.append(" | key='").append(this.key); }
result.append("' | keyGenerator='").append(this.keyGenerator);
result.append("' | cacheManager='").append(this.cacheManager);
result.append("' | cacheResolver='").append(this.cacheResolver); public String getCondition() {
result.append("' | condition='").append(this.condition).append("'"); return this.condition;
return result; }
}
/**
* This implementation compares the {@code toString()} results.
*
* @see #toString()
*/
@Override
public boolean equals(Object other) {
return (other instanceof CacheOperation && toString().equals(other.toString()));
}
/**
* This implementation returns {@code toString()}'s hash code.
*
* @see #toString()
*/
@Override
public int hashCode() {
return toString().hashCode();
}
/**
* Return an identifying description for this cache operation.
* <p>Returned value is produced by calling {@link Builder#getOperationDescription()}
* during object construction. This method is used in {#hashCode} and {#equals}.
*
* @see Builder#getOperationDescription()
*/
@Override
public final String toString() {
return toString;
}
public abstract static class Builder {
private String name = "";
private Set<String> cacheNames = Collections.emptySet();
private String key = "";
private String keyGenerator = "";
private String cacheManager = "";
private String cacheResolver = "";
private String condition = "";
public void setName(String name) {
Assert.hasText(name);
this.name = name;
}
public void setCacheName(String cacheName) {
Assert.hasText(cacheName);
this.cacheNames = Collections.singleton(cacheName);
}
public void setCacheNames(String... cacheNames) {
this.cacheNames = new LinkedHashSet<String>(cacheNames.length);
for (String cacheName : cacheNames) {
Assert.hasText(cacheName, "Cache name must be non-null if specified");
this.cacheNames.add(cacheName);
}
}
public Set<String> getCacheNames() {
return cacheNames;
}
public void setKey(String key) {
Assert.notNull(key);
this.key = key;
}
public String getKey() {
return key;
}
public String getKeyGenerator() {
return keyGenerator;
}
public String getCacheManager() {
return cacheManager;
}
public String getCacheResolver() {
return cacheResolver;
}
public void setKeyGenerator(String keyGenerator) {
Assert.notNull(keyGenerator);
this.keyGenerator = keyGenerator;
}
public void setCacheManager(String cacheManager) {
Assert.notNull(cacheManager);
this.cacheManager = cacheManager;
}
public void setCacheResolver(String cacheResolver) {
Assert.notNull(cacheManager);
this.cacheResolver = cacheResolver;
}
public void setCondition(String condition) {
Assert.notNull(condition);
this.condition = condition;
}
/**
* Return an identifying description for this caching operation.
* <p>Available to subclasses, for inclusion in their {@code toString()} result.
*/
protected StringBuilder getOperationDescription() {
StringBuilder result = new StringBuilder(getClass().getSimpleName());
result.append("[").append(this.name);
result.append("] caches=").append(this.cacheNames);
result.append(" | key='").append(this.key);
result.append("' | keyGenerator='").append(this.keyGenerator);
result.append("' | cacheManager='").append(this.cacheManager);
result.append("' | cacheResolver='").append(this.cacheResolver);
result.append("' | condition='").append(this.condition).append("'");
return result;
}
public abstract CacheOperation build();
}
} }

View File

@ -25,23 +25,36 @@ package org.springframework.cache.interceptor;
*/ */
public class CachePutOperation extends CacheOperation { public class CachePutOperation extends CacheOperation {
private String unless; private final String unless;
public CachePutOperation(CachePutOperation.Builder b) {
super(b);
this.unless = b.unless;
}
public String getUnless() { public String getUnless() {
return this.unless; return this.unless;
} }
public void setUnless(String unless) { public static class Builder extends CacheOperation.Builder {
this.unless = unless;
}
@Override private String unless;
protected StringBuilder getOperationDescription() {
StringBuilder sb = super.getOperationDescription(); public void setUnless(String unless) {
sb.append(" | unless='"); this.unless = unless;
sb.append(this.unless); }
sb.append("'");
return sb; @Override
} protected StringBuilder getOperationDescription() {
StringBuilder sb = super.getOperationDescription();
sb.append(" | unless='");
sb.append(this.unless);
sb.append("'");
return sb;
}
public CachePutOperation build() {
return new CachePutOperation(this);
}
}
} }

View File

@ -25,36 +25,53 @@ package org.springframework.cache.interceptor;
*/ */
public class CacheableOperation extends CacheOperation { public class CacheableOperation extends CacheOperation {
private String unless; private final String unless;
private boolean sync; private boolean sync;
public CacheableOperation(CacheableOperation.Builder b) {
super(b);
this.unless = b.unless;
this.sync = b.sync;
}
public String getUnless() { public String getUnless() {
return this.unless; return this.unless;
} }
public void setUnless(String unless) {
this.unless = unless;
}
public boolean isSync() { public boolean isSync() {
return this.sync; return this.sync;
} }
public void setSync(boolean sync) {
this.sync = sync;
}
@Override public static class Builder extends CacheOperation.Builder {
protected StringBuilder getOperationDescription() {
StringBuilder sb = super.getOperationDescription(); private String unless;
sb.append(" | unless='");
sb.append(this.unless); private boolean sync;
sb.append("'"); public void setUnless(String unless) {
sb.append(" | sync='"); this.unless = unless;
sb.append(this.sync); }
sb.append("'");
return sb; public void setSync(boolean sync) {
} this.sync = sync;
}
@Override
protected StringBuilder getOperationDescription() {
StringBuilder sb = super.getOperationDescription();
sb.append(" | unless='");
sb.append(this.unless);
sb.append("'");
sb.append(" | sync='");
sb.append(this.sync);
sb.append("'");
return sb;
}
@Override
public CacheableOperation build() {
return new CacheableOperation(this);
}
}
} }