Java Code Examples for org.springframework.cache.interceptor.CacheResolver

The following examples show how to use org.springframework.cache.interceptor.CacheResolver. These examples are extracted from open source projects. 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 Project: spring-analysis-note   Source File: CachePutOperation.java    License: 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 2
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 3
Source Project: java-technology-stack   Source File: CachePutOperation.java    License: 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 4
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 5
Source Project: spring-analysis-note   Source File: CacheResultInterceptor.java    License: 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 6
/**
 * 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 7
@Override
protected CacheResolver getDefaultCacheResolver() {
	if (getCacheResolver() == null) {
		this.cacheResolver = SingletonSupplier.of(new SimpleCacheResolver(getDefaultCacheManager()));
	}
	return getCacheResolver();
}
 
Example 8
Source Project: spring-analysis-note   Source File: AbstractJCacheKeyOperation.java    License: 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 9
Source Project: spring-analysis-note   Source File: CacheRemoveOperation.java    License: 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 10
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 11
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 12
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 13
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 14
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 15
Source Project: spring-analysis-note   Source File: AbstractJCacheOperation.java    License: 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 16
Source Project: spring-analysis-note   Source File: CacheResultOperation.java    License: 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 17
Source Project: spring-analysis-note   Source File: JCacheInterceptorTests.java    License: 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 18
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 19
Source Project: spring-analysis-note   Source File: JCacheJavaConfigTests.java    License: 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 20
Source Project: java-technology-stack   Source File: CacheResultInterceptor.java    License: 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 21
/**
 * 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 22
@Override
protected CacheResolver getDefaultCacheResolver() {
	if (getCacheResolver() == null) {
		this.cacheResolver = SingletonSupplier.of(new SimpleCacheResolver(getDefaultCacheManager()));
	}
	return getCacheResolver();
}
 
Example 23
@Override
protected CacheResolver getDefaultExceptionCacheResolver() {
	if (getExceptionCacheResolver() == null) {
		this.exceptionCacheResolver = SingletonSupplier.of(new LazyCacheResolver());
	}
	return getExceptionCacheResolver();
}
 
Example 24
Source Project: java-technology-stack   Source File: AbstractJCacheKeyOperation.java    License: 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 25
Source Project: java-technology-stack   Source File: CacheRemoveOperation.java    License: 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
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 27
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 28
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 29
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 30
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();
	}
}