org.springframework.cache.interceptor.CacheResolver Java Examples

The following examples show how to use org.springframework.cache.interceptor.CacheResolver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AnnotationJCacheOperationSource.java    From spring-analysis-note with MIT License 6 votes vote down vote up
protected CacheResultOperation createCacheResultOperation(Method method, @Nullable CacheDefaults defaults, CacheResult ann) {
	String cacheName = determineCacheName(method, defaults, ann.cacheName());
	CacheResolverFactory cacheResolverFactory =
			determineCacheResolverFactory(defaults, ann.cacheResolverFactory());
	KeyGenerator keyGenerator = determineKeyGenerator(defaults, ann.cacheKeyGenerator());

	CacheMethodDetails<CacheResult> methodDetails = createMethodDetails(method, ann, cacheName);

	CacheResolver cacheResolver = getCacheResolver(cacheResolverFactory, methodDetails);
	CacheResolver exceptionCacheResolver = null;
	final String exceptionCacheName = ann.exceptionCacheName();
	if (StringUtils.hasText(exceptionCacheName)) {
		exceptionCacheResolver = getExceptionCacheResolver(cacheResolverFactory, methodDetails);
	}

	return new CacheResultOperation(methodDetails, cacheResolver, keyGenerator, exceptionCacheResolver);
}
 
Example #2
Source File: CachePutOperation.java    From spring-analysis-note with MIT License 6 votes vote down vote up
public CachePutOperation(
		CacheMethodDetails<CachePut> methodDetails, CacheResolver cacheResolver, KeyGenerator keyGenerator) {

	super(methodDetails, cacheResolver, keyGenerator);

	CachePut ann = methodDetails.getCacheAnnotation();
	this.exceptionTypeFilter = createExceptionTypeFilter(ann.cacheFor(), ann.noCacheFor());

	CacheParameterDetail valueParameterDetail =
			initializeValueParameterDetail(methodDetails.getMethod(), this.allParameterDetails);
	if (valueParameterDetail == null) {
		throw new IllegalArgumentException("No parameter annotated with @CacheValue was found for " +
				methodDetails.getMethod());
	}
	this.valueParameterDetail = valueParameterDetail;
}
 
Example #3
Source File: AnnotationJCacheOperationSource.java    From java-technology-stack with MIT License 6 votes vote down vote up
protected CacheResultOperation createCacheResultOperation(Method method, @Nullable CacheDefaults defaults, CacheResult ann) {
	String cacheName = determineCacheName(method, defaults, ann.cacheName());
	CacheResolverFactory cacheResolverFactory =
			determineCacheResolverFactory(defaults, ann.cacheResolverFactory());
	KeyGenerator keyGenerator = determineKeyGenerator(defaults, ann.cacheKeyGenerator());

	CacheMethodDetails<CacheResult> methodDetails = createMethodDetails(method, ann, cacheName);

	CacheResolver cacheResolver = getCacheResolver(cacheResolverFactory, methodDetails);
	CacheResolver exceptionCacheResolver = null;
	final String exceptionCacheName = ann.exceptionCacheName();
	if (StringUtils.hasText(exceptionCacheName)) {
		exceptionCacheResolver = getExceptionCacheResolver(cacheResolverFactory, methodDetails);
	}

	return new CacheResultOperation(methodDetails, cacheResolver, keyGenerator, exceptionCacheResolver);
}
 
Example #4
Source File: CachePutOperation.java    From java-technology-stack with MIT License 6 votes vote down vote up
public CachePutOperation(
		CacheMethodDetails<CachePut> methodDetails, CacheResolver cacheResolver, KeyGenerator keyGenerator) {

	super(methodDetails, cacheResolver, keyGenerator);

	CachePut ann = methodDetails.getCacheAnnotation();
	this.exceptionTypeFilter = createExceptionTypeFilter(ann.cacheFor(), ann.noCacheFor());

	CacheParameterDetail valueParameterDetail =
			initializeValueParameterDetail(methodDetails.getMethod(), this.allParameterDetails);
	if (valueParameterDetail == null) {
		throw new IllegalArgumentException("No parameter annotated with @CacheValue was found for " +
				methodDetails.getMethod());
	}
	this.valueParameterDetail = valueParameterDetail;
}
 
Example #5
Source File: AnnotationJCacheOperationSource.java    From spring-analysis-note with MIT License 5 votes vote down vote up
protected CacheRemoveAllOperation createCacheRemoveAllOperation(Method method, @Nullable CacheDefaults defaults, CacheRemoveAll ann) {
	String cacheName = determineCacheName(method, defaults, ann.cacheName());
	CacheResolverFactory cacheResolverFactory =
			determineCacheResolverFactory(defaults, ann.cacheResolverFactory());

	CacheMethodDetails<CacheRemoveAll> methodDetails = createMethodDetails(method, ann, cacheName);
	CacheResolver cacheResolver = getCacheResolver(cacheResolverFactory, methodDetails);
	return new CacheRemoveAllOperation(methodDetails, cacheResolver);
}
 
Example #6
Source File: AnnotationJCacheOperationSource.java    From spring-analysis-note with MIT License 5 votes vote down vote up
protected CacheResolver getExceptionCacheResolver(
		@Nullable CacheResolverFactory factory, CacheMethodDetails<CacheResult> details) {

	if (factory != null) {
		javax.cache.annotation.CacheResolver cacheResolver = factory.getExceptionCacheResolver(details);
		return new CacheResolverAdapter(cacheResolver);
	}
	else {
		return getDefaultExceptionCacheResolver();
	}
}
 
Example #7
Source File: AnnotationJCacheOperationSource.java    From spring-analysis-note with MIT License 5 votes vote down vote up
protected CacheRemoveOperation createCacheRemoveOperation(Method method, @Nullable CacheDefaults defaults, CacheRemove ann) {
	String cacheName = determineCacheName(method, defaults, ann.cacheName());
	CacheResolverFactory cacheResolverFactory =
			determineCacheResolverFactory(defaults, ann.cacheResolverFactory());
	KeyGenerator keyGenerator = determineKeyGenerator(defaults, ann.cacheKeyGenerator());

	CacheMethodDetails<CacheRemove> methodDetails = createMethodDetails(method, ann, cacheName);
	CacheResolver cacheResolver = getCacheResolver(cacheResolverFactory, methodDetails);
	return new CacheRemoveOperation(methodDetails, cacheResolver, keyGenerator);
}
 
Example #8
Source File: CacheResultOperation.java    From java-technology-stack with MIT License 5 votes vote down vote up
public CacheResultOperation(CacheMethodDetails<CacheResult> methodDetails, CacheResolver cacheResolver,
		KeyGenerator keyGenerator, @Nullable CacheResolver exceptionCacheResolver) {

	super(methodDetails, cacheResolver, keyGenerator);

	CacheResult ann = methodDetails.getCacheAnnotation();
	this.exceptionTypeFilter = createExceptionTypeFilter(ann.cachedExceptions(), ann.nonCachedExceptions());
	this.exceptionCacheResolver = exceptionCacheResolver;
	this.exceptionCacheName = (StringUtils.hasText(ann.exceptionCacheName()) ? ann.exceptionCacheName() : null);
}
 
Example #9
Source File: CacheResultInterceptor.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Nullable
private Cache resolveExceptionCache(CacheOperationInvocationContext<CacheResultOperation> context) {
	CacheResolver exceptionCacheResolver = context.getOperation().getExceptionCacheResolver();
	if (exceptionCacheResolver != null) {
		return extractFrom(context.getOperation().getExceptionCacheResolver().resolveCaches(context));
	}
	return null;
}
 
Example #10
Source File: JCacheJavaConfigTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void fullCachingConfig() throws Exception {
	AnnotationConfigApplicationContext context =
			new AnnotationConfigApplicationContext(FullCachingConfig.class);

	DefaultJCacheOperationSource cos = context.getBean(DefaultJCacheOperationSource.class);
	assertSame(context.getBean(KeyGenerator.class), cos.getKeyGenerator());
	assertSame(context.getBean("cacheResolver", CacheResolver.class),
			cos.getCacheResolver());
	assertSame(context.getBean("exceptionCacheResolver", CacheResolver.class),
			cos.getExceptionCacheResolver());
	JCacheInterceptor interceptor = context.getBean(JCacheInterceptor.class);
	assertSame(context.getBean("errorHandler", CacheErrorHandler.class), interceptor.getErrorHandler());
}
 
Example #11
Source File: AnnotationCacheOperationSourceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void assertJCacheResolver(CacheResolver actual,
		Class<? extends javax.cache.annotation.CacheResolver> expectedTargetType) {

	if (expectedTargetType == null) {
		assertNull(actual);
	}
	else {
		assertEquals("Wrong cache resolver implementation", CacheResolverAdapter.class, actual.getClass());
		CacheResolverAdapter adapter = (CacheResolverAdapter) actual;
		assertEquals("Wrong target JCache implementation", expectedTargetType, adapter.getTarget().getClass());
	}
}
 
Example #12
Source File: CacheResultInterceptor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Nullable
private Cache resolveExceptionCache(CacheOperationInvocationContext<CacheResultOperation> context) {
	CacheResolver exceptionCacheResolver = context.getOperation().getExceptionCacheResolver();
	if (exceptionCacheResolver != null) {
		return extractFrom(context.getOperation().getExceptionCacheResolver().resolveCaches(context));
	}
	return null;
}
 
Example #13
Source File: JCacheInterceptorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
protected JCacheOperationSource createOperationSource(CacheManager cacheManager,
		CacheResolver cacheResolver, CacheResolver exceptionCacheResolver, KeyGenerator keyGenerator) {

	DefaultJCacheOperationSource source = new DefaultJCacheOperationSource();
	source.setCacheManager(cacheManager);
	source.setCacheResolver(cacheResolver);
	source.setExceptionCacheResolver(exceptionCacheResolver);
	source.setKeyGenerator(keyGenerator);
	source.setBeanFactory(new StaticListableBeanFactory());
	source.afterSingletonsInstantiated();
	return source;
}
 
Example #14
Source File: DefaultJCacheOperationSource.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Construct a new {@code DefaultJCacheOperationSource} with the given cache manager,
 * cache resolver and key generator suppliers, applying the corresponding default
 * if a supplier is not resolvable.
 * @since 5.1
 */
public DefaultJCacheOperationSource(
		@Nullable Supplier<CacheManager> cacheManager, @Nullable Supplier<CacheResolver> cacheResolver,
		@Nullable Supplier<CacheResolver> exceptionCacheResolver, @Nullable Supplier<KeyGenerator> keyGenerator) {

	this.cacheManager = SingletonSupplier.ofNullable(cacheManager);
	this.cacheResolver = SingletonSupplier.ofNullable(cacheResolver);
	this.exceptionCacheResolver = SingletonSupplier.ofNullable(exceptionCacheResolver);
	this.keyGenerator = new SingletonSupplier<>(keyGenerator, SimpleKeyGenerator::new);
}
 
Example #15
Source File: AnnotationJCacheOperationSource.java    From spring-analysis-note with MIT License 5 votes vote down vote up
protected CachePutOperation createCachePutOperation(Method method, @Nullable CacheDefaults defaults, CachePut ann) {
	String cacheName = determineCacheName(method, defaults, ann.cacheName());
	CacheResolverFactory cacheResolverFactory =
			determineCacheResolverFactory(defaults, ann.cacheResolverFactory());
	KeyGenerator keyGenerator = determineKeyGenerator(defaults, ann.cacheKeyGenerator());

	CacheMethodDetails<CachePut> methodDetails = createMethodDetails(method, ann, cacheName);
	CacheResolver cacheResolver = getCacheResolver(cacheResolverFactory, methodDetails);
	return new CachePutOperation(methodDetails, cacheResolver, keyGenerator);
}
 
Example #16
Source File: AnnotationJCacheOperationSource.java    From java-technology-stack with MIT License 5 votes vote down vote up
protected CacheResolver getCacheResolver(
		@Nullable CacheResolverFactory factory, CacheMethodDetails<?> details) {

	if (factory != null) {
		javax.cache.annotation.CacheResolver cacheResolver = factory.getCacheResolver(details);
		return new CacheResolverAdapter(cacheResolver);
	}
	else {
		return getDefaultCacheResolver();
	}
}
 
Example #17
Source File: AbstractJCacheKeyOperation.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Create a new instance.
 * @param methodDetails the {@link CacheMethodDetails} related to the cached method
 * @param cacheResolver the cache resolver to resolve regular caches
 * @param keyGenerator the key generator to compute cache keys
 */
protected AbstractJCacheKeyOperation(CacheMethodDetails<A> methodDetails,
		CacheResolver cacheResolver, KeyGenerator keyGenerator) {

	super(methodDetails, cacheResolver);
	this.keyGenerator = keyGenerator;
	this.keyParameterDetails = initializeKeyParameterDetails(this.allParameterDetails);
}
 
Example #18
Source File: AnnotationJCacheOperationSource.java    From spring-analysis-note with MIT License 5 votes vote down vote up
protected CacheResolver getCacheResolver(
		@Nullable CacheResolverFactory factory, CacheMethodDetails<?> details) {

	if (factory != null) {
		javax.cache.annotation.CacheResolver cacheResolver = factory.getCacheResolver(details);
		return new CacheResolverAdapter(cacheResolver);
	}
	else {
		return getDefaultCacheResolver();
	}
}
 
Example #19
Source File: DefaultJCacheOperationSource.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected CacheResolver getDefaultCacheResolver() {
	if (getCacheResolver() == null) {
		this.cacheResolver = SingletonSupplier.of(new SimpleCacheResolver(getDefaultCacheManager()));
	}
	return getCacheResolver();
}
 
Example #20
Source File: AnnotationJCacheOperationSource.java    From java-technology-stack with MIT License 5 votes vote down vote up
protected CacheRemoveAllOperation createCacheRemoveAllOperation(Method method, @Nullable CacheDefaults defaults, CacheRemoveAll ann) {
	String cacheName = determineCacheName(method, defaults, ann.cacheName());
	CacheResolverFactory cacheResolverFactory =
			determineCacheResolverFactory(defaults, ann.cacheResolverFactory());

	CacheMethodDetails<CacheRemoveAll> methodDetails = createMethodDetails(method, ann, cacheName);
	CacheResolver cacheResolver = getCacheResolver(cacheResolverFactory, methodDetails);
	return new CacheRemoveAllOperation(methodDetails, cacheResolver);
}
 
Example #21
Source File: AnnotationJCacheOperationSource.java    From java-technology-stack with MIT License 5 votes vote down vote up
protected CacheRemoveOperation createCacheRemoveOperation(Method method, @Nullable CacheDefaults defaults, CacheRemove ann) {
	String cacheName = determineCacheName(method, defaults, ann.cacheName());
	CacheResolverFactory cacheResolverFactory =
			determineCacheResolverFactory(defaults, ann.cacheResolverFactory());
	KeyGenerator keyGenerator = determineKeyGenerator(defaults, ann.cacheKeyGenerator());

	CacheMethodDetails<CacheRemove> methodDetails = createMethodDetails(method, ann, cacheName);
	CacheResolver cacheResolver = getCacheResolver(cacheResolverFactory, methodDetails);
	return new CacheRemoveOperation(methodDetails, cacheResolver, keyGenerator);
}
 
Example #22
Source File: AbstractJCacheOperation.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Construct a new {@code AbstractJCacheOperation}.
 * @param methodDetails the {@link CacheMethodDetails} related to the cached method
 * @param cacheResolver the cache resolver to resolve regular caches
 */
protected AbstractJCacheOperation(CacheMethodDetails<A> methodDetails, CacheResolver cacheResolver) {
	Assert.notNull(methodDetails, "CacheMethodDetails must not be null");
	Assert.notNull(cacheResolver, "CacheResolver must not be null");
	this.methodDetails = methodDetails;
	this.cacheResolver = cacheResolver;
	this.allParameterDetails = initializeAllParameterDetails(methodDetails.getMethod());
}
 
Example #23
Source File: AnnotationJCacheOperationSource.java    From java-technology-stack with MIT License 5 votes vote down vote up
protected CachePutOperation createCachePutOperation(Method method, @Nullable CacheDefaults defaults, CachePut ann) {
	String cacheName = determineCacheName(method, defaults, ann.cacheName());
	CacheResolverFactory cacheResolverFactory =
			determineCacheResolverFactory(defaults, ann.cacheResolverFactory());
	KeyGenerator keyGenerator = determineKeyGenerator(defaults, ann.cacheKeyGenerator());

	CacheMethodDetails<CachePut> methodDetails = createMethodDetails(method, ann, cacheName);
	CacheResolver cacheResolver = getCacheResolver(cacheResolverFactory, methodDetails);
	return new CachePutOperation(methodDetails, cacheResolver, keyGenerator);
}
 
Example #24
Source File: CacheResultOperation.java    From spring-analysis-note with MIT License 5 votes vote down vote up
public CacheResultOperation(CacheMethodDetails<CacheResult> methodDetails, CacheResolver cacheResolver,
		KeyGenerator keyGenerator, @Nullable CacheResolver exceptionCacheResolver) {

	super(methodDetails, cacheResolver, keyGenerator);

	CacheResult ann = methodDetails.getCacheAnnotation();
	this.exceptionTypeFilter = createExceptionTypeFilter(ann.cachedExceptions(), ann.nonCachedExceptions());
	this.exceptionCacheResolver = exceptionCacheResolver;
	this.exceptionCacheName = (StringUtils.hasText(ann.exceptionCacheName()) ? ann.exceptionCacheName() : null);
}
 
Example #25
Source File: CacheRemoveOperation.java    From java-technology-stack with MIT License 5 votes vote down vote up
public CacheRemoveOperation(
		CacheMethodDetails<CacheRemove> methodDetails, CacheResolver cacheResolver, KeyGenerator keyGenerator) {

	super(methodDetails, cacheResolver, keyGenerator);
	CacheRemove ann = methodDetails.getCacheAnnotation();
	this.exceptionTypeFilter = createExceptionTypeFilter(ann.evictFor(), ann.noEvictFor());
}
 
Example #26
Source File: JCacheInterceptorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
protected JCacheOperationSource createOperationSource(CacheManager cacheManager,
		CacheResolver cacheResolver, CacheResolver exceptionCacheResolver, KeyGenerator keyGenerator) {

	DefaultJCacheOperationSource source = new DefaultJCacheOperationSource();
	source.setCacheManager(cacheManager);
	source.setCacheResolver(cacheResolver);
	source.setExceptionCacheResolver(exceptionCacheResolver);
	source.setKeyGenerator(keyGenerator);
	source.setBeanFactory(new StaticListableBeanFactory());
	source.afterSingletonsInstantiated();
	return source;
}
 
Example #27
Source File: AnnotationCacheOperationSourceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void assertJCacheResolver(CacheResolver actual,
		Class<? extends javax.cache.annotation.CacheResolver> expectedTargetType) {

	if (expectedTargetType == null) {
		assertNull(actual);
	}
	else {
		assertEquals("Wrong cache resolver implementation", CacheResolverAdapter.class, actual.getClass());
		CacheResolverAdapter adapter = (CacheResolverAdapter) actual;
		assertEquals("Wrong target JCache implementation", expectedTargetType, adapter.getTarget().getClass());
	}
}
 
Example #28
Source File: JCacheJavaConfigTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void fullCachingConfig() throws Exception {
	AnnotationConfigApplicationContext context =
			new AnnotationConfigApplicationContext(FullCachingConfig.class);

	DefaultJCacheOperationSource cos = context.getBean(DefaultJCacheOperationSource.class);
	assertSame(context.getBean(KeyGenerator.class), cos.getKeyGenerator());
	assertSame(context.getBean("cacheResolver", CacheResolver.class),
			cos.getCacheResolver());
	assertSame(context.getBean("exceptionCacheResolver", CacheResolver.class),
			cos.getExceptionCacheResolver());
	JCacheInterceptor interceptor = context.getBean(JCacheInterceptor.class);
	assertSame(context.getBean("errorHandler", CacheErrorHandler.class), interceptor.getErrorHandler());
}
 
Example #29
Source File: AbstractJCacheKeyOperation.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Create a new instance.
 * @param methodDetails the {@link CacheMethodDetails} related to the cached method
 * @param cacheResolver the cache resolver to resolve regular caches
 * @param keyGenerator the key generator to compute cache keys
 */
protected AbstractJCacheKeyOperation(CacheMethodDetails<A> methodDetails,
		CacheResolver cacheResolver, KeyGenerator keyGenerator) {

	super(methodDetails, cacheResolver);
	this.keyGenerator = keyGenerator;
	this.keyParameterDetails = initializeKeyParameterDetails(this.allParameterDetails);
}
 
Example #30
Source File: DefaultJCacheOperationSource.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected CacheResolver getDefaultExceptionCacheResolver() {
	if (getExceptionCacheResolver() == null) {
		this.exceptionCacheResolver = SingletonSupplier.of(new LazyCacheResolver());
	}
	return getExceptionCacheResolver();
}