org.springframework.cache.annotation.CachingConfigurer Java Examples

The following examples show how to use org.springframework.cache.annotation.CachingConfigurer. 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: ElastiCacheAutoConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void enableElasticache_configuredWithExplicitClusterAndExpiration_configuresExplicitlyConfiguredCachesWithMixedExpirationTimes() {
	this.contextRunner
			.withPropertyValues("cloud.aws.elasticache.default-expiration=12",
					"cloud.aws.elasticache.clusters[0].name=sampleCacheOneLogical",
					"cloud.aws.elasticache.clusters[1].name=sampleCacheTwoLogical",
					"cloud.aws.elasticache.clusters[1].expiration=42")
			.withUserConfiguration(MockCacheConfigurationWithStackCaches.class)
			.run(context -> {
				CacheManager cacheManager = context.getBean(CachingConfigurer.class)
						.cacheManager();
				assertThat(cacheManager.getCacheNames()).hasSize(2);
				Cache firstCache = cacheManager.getCache("sampleCacheOneLogical");
				assertThat(firstCache.getName()).isNotNull();
				assertThat(getExpirationFromCache(firstCache)).isEqualTo(12);

				Cache secondCache = cacheManager.getCache("sampleCacheTwoLogical");
				assertThat(secondCache.getName()).isNotNull();
				assertThat(getExpirationFromCache(secondCache)).isEqualTo(42);
			});
}
 
Example #2
Source File: ElastiCacheCachingConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void enableElasticache_configuredWithoutExplicitClusterButDefaultExpiryTime_configuresImplicitlyConfiguredCachesWithDefaultExpiryTimeOnAllCaches()
		throws Exception {
	// @checkstyle:on
	// Arrange

	// Act
	this.context = new AnnotationConfigApplicationContext(
			ApplicationConfigurationWithNoExplicitStackConfigurationAndDefaultExpiration.class);

	// Assert
	CacheManager cacheManager = this.context.getBean(CachingConfigurer.class)
			.cacheManager();
	assertThat(cacheManager.getCacheNames().size()).isEqualTo(2);
	Cache firstCache = cacheManager.getCache("sampleCacheOneLogical");
	assertThat(firstCache.getName()).isNotNull();
	assertThat(getExpirationFromCache(firstCache)).isEqualTo(23);

	Cache secondCache = cacheManager.getCache("sampleCacheTwoLogical");
	assertThat(secondCache.getName()).isNotNull();
	assertThat(getExpirationFromCache(secondCache)).isEqualTo(23);
}
 
Example #3
Source File: ElastiCacheCachingConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void enableElasticache_configuredWithoutExplicitCluster_configuresImplicitlyConfiguredCaches()
		throws Exception {
	// Arrange

	// Act
	this.context = new AnnotationConfigApplicationContext(
			ApplicationConfigurationWithNoExplicitStackConfiguration.class);

	// Assert
	CacheManager cacheManager = this.context.getBean(CachingConfigurer.class)
			.cacheManager();
	assertThat(cacheManager.getCacheNames().size()).isEqualTo(2);
	Cache firstCache = cacheManager.getCache("sampleCacheOneLogical");
	assertThat(firstCache.getName()).isNotNull();
	assertThat(getExpirationFromCache(firstCache)).isEqualTo(0);

	Cache secondCache = cacheManager.getCache("sampleCacheTwoLogical");
	assertThat(secondCache.getName()).isNotNull();
	assertThat(getExpirationFromCache(secondCache)).isEqualTo(0);
}
 
Example #4
Source File: ElastiCacheCachingConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void enableElasticache_configuredWithExplicitClusterAndExpiration_configuresExplicitlyConfiguredCachesWithMixedExpirationTimes()
		throws Exception {
	// @checkstyle:on
	// Arrange

	// Act
	this.context = new AnnotationConfigApplicationContext(
			ApplicationConfigurationWithExplicitStackConfigurationAndMixedExpiryTime.class);

	// Assert
	CacheManager cacheManager = this.context.getBean(CachingConfigurer.class)
			.cacheManager();
	assertThat(cacheManager.getCacheNames().size()).isEqualTo(2);
	Cache firstCache = cacheManager.getCache("firstCache");
	assertThat(firstCache.getName()).isNotNull();
	assertThat(getExpirationFromCache(firstCache)).isEqualTo(12);

	Cache secondCache = cacheManager.getCache("secondCache");
	assertThat(secondCache.getName()).isNotNull();
	assertThat(getExpirationFromCache(secondCache)).isEqualTo(42);
}
 
Example #5
Source File: ElastiCacheCachingConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void enableElasticache_configuredWithExplicitClusterAndExpiration_configuresExplicitlyConfiguredCachesWithCustomExpirationTimes()
		// @checkstyle:on
		throws Exception {
	// Arrange

	// Act
	this.context = new AnnotationConfigApplicationContext(
			ApplicationConfigurationWithExplicitStackConfigurationAndExpiryTime.class);

	// Assert
	CacheManager cacheManager = this.context.getBean(CachingConfigurer.class)
			.cacheManager();
	assertThat(cacheManager.getCacheNames().size()).isEqualTo(2);
	Cache firstCache = cacheManager.getCache("firstCache");
	assertThat(firstCache.getName()).isNotNull();
	assertThat(getExpirationFromCache(firstCache)).isEqualTo(23);

	Cache secondCache = cacheManager.getCache("secondCache");
	assertThat(secondCache.getName()).isNotNull();
	assertThat(getExpirationFromCache(secondCache)).isEqualTo(42);
}
 
Example #6
Source File: ElastiCacheCachingConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void enableElasticache_configuredWithExplicitCluster_configuresExplicitlyConfiguredCaches()
		throws Exception {
	// Arrange

	// Act
	this.context = new AnnotationConfigApplicationContext(
			ApplicationConfigurationWithExplicitStackConfiguration.class);

	// Assert
	CacheManager cacheManager = this.context.getBean(CachingConfigurer.class)
			.cacheManager();
	assertThat(cacheManager.getCacheNames().size()).isEqualTo(2);
	Cache firstCache = cacheManager.getCache("firstCache");
	assertThat(firstCache.getName()).isNotNull();
	assertThat(getExpirationFromCache(firstCache)).isEqualTo(0);

	Cache secondCache = cacheManager.getCache("secondCache");
	assertThat(secondCache.getName()).isNotNull();
	assertThat(getExpirationFromCache(secondCache)).isEqualTo(0);
}
 
Example #7
Source File: ElastiCacheAutoConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void enableElasticache_configuredWithoutExplicitClusterButDefaultExpiryTime_configuresImplicitlyConfiguredCachesWithDefaultExpiryTimeOnAllCaches() {
	this.contextRunner
			.withPropertyValues("cloud.aws.elasticache.default-expiration=23")
			.withUserConfiguration(MockCacheConfigurationWithStackCaches.class)
			.run(context -> {
				CacheManager cacheManager = context.getBean(CachingConfigurer.class)
						.cacheManager();
				assertThat(cacheManager.getCacheNames()).hasSize(2);
				Cache firstCache = cacheManager.getCache("sampleCacheOneLogical");
				assertThat(firstCache.getName()).isNotNull();
				assertThat(getExpirationFromCache(firstCache)).isEqualTo(23);

				Cache secondCache = cacheManager.getCache("sampleCacheTwoLogical");
				assertThat(secondCache.getName()).isNotNull();
				assertThat(getExpirationFromCache(secondCache)).isEqualTo(23);
			});
}
 
Example #8
Source File: ElastiCacheAutoConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void enableElasticache_configuredWithoutExplicitCluster_configuresImplicitlyConfiguredCaches() {
	this.contextRunner
			.withUserConfiguration(MockCacheConfigurationWithStackCaches.class)
			.run(context -> {
				CacheManager cacheManager = context.getBean(CachingConfigurer.class)
						.cacheManager();
				assertThat(cacheManager.getCacheNames()).hasSize(2);
				Cache firstCache = cacheManager.getCache("sampleCacheOneLogical");
				assertThat(firstCache.getName()).isNotNull();
				assertThat(getExpirationFromCache(firstCache)).isEqualTo(0);

				Cache secondCache = cacheManager.getCache("sampleCacheTwoLogical");
				assertThat(secondCache.getName()).isNotNull();
				assertThat(getExpirationFromCache(secondCache)).isEqualTo(0);
			});
}
 
Example #9
Source File: ElastiCacheAutoConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void enableElasticache_configuredWithExplicitClusterAndExpiration_configuresExplicitlyConfiguredCachesWithCustomExpirationTimes() {
	this.contextRunner
			.withPropertyValues(
					"cloud.aws.elasticache.clusters[0].name=sampleCacheOneLogical",
					"cloud.aws.elasticache.clusters[0].expiration=23",
					"cloud.aws.elasticache.clusters[1].name=sampleCacheTwoLogical",
					"cloud.aws.elasticache.clusters[1].expiration=42")
			.withUserConfiguration(MockCacheConfigurationWithStackCaches.class)
			.run(context -> {
				CacheManager cacheManager = context.getBean(CachingConfigurer.class)
						.cacheManager();
				assertThat(cacheManager.getCacheNames()).hasSize(2);
				Cache firstCache = cacheManager.getCache("sampleCacheOneLogical");
				assertThat(firstCache.getName()).isNotNull();
				assertThat(getExpirationFromCache(firstCache)).isEqualTo(23);

				Cache secondCache = cacheManager.getCache("sampleCacheTwoLogical");
				assertThat(secondCache.getName()).isNotNull();
				assertThat(getExpirationFromCache(secondCache)).isEqualTo(42);
			});
}
 
Example #10
Source File: ElastiCacheAutoConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void enableElasticache_configuredWithExplicitCluster_configuresExplicitlyConfiguredCaches() {
	this.contextRunner
			.withPropertyValues(
					"cloud.aws.elasticache.clusters[0].name=sampleCacheOneLogical",
					"cloud.aws.elasticache.clusters[1].name=sampleCacheTwoLogical")
			.withUserConfiguration(MockCacheConfigurationWithStackCaches.class)
			.run(context -> {
				// Assert
				CacheManager cacheManager = context.getBean(CachingConfigurer.class)
						.cacheManager();
				assertThat(cacheManager.getCacheNames()).hasSize(2);
				Cache firstCache = cacheManager.getCache("sampleCacheOneLogical");
				assertThat(firstCache.getName()).isNotNull();
				assertThat(getExpirationFromCache(firstCache)).isEqualTo(0);

				Cache secondCache = cacheManager.getCache("sampleCacheTwoLogical");
				assertThat(secondCache.getName()).isNotNull();
				assertThat(getExpirationFromCache(secondCache)).isEqualTo(0);
			});
}
 
Example #11
Source File: AbstractJCacheConfiguration.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected void useCachingConfigurer(CachingConfigurer config) {
	super.useCachingConfigurer(config);
	if (config instanceof JCacheConfigurer) {
		this.exceptionCacheResolver = ((JCacheConfigurer) config)::exceptionCacheResolver;
	}
}
 
Example #12
Source File: ElastiCacheAutoConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void cacheManager_configuredNoCachesWithNoStack_configuresNoCacheManager() {
	this.contextRunner.run(context -> {
		CacheManager cacheManager = context.getBean(CachingConfigurer.class)
				.cacheManager();
		assertThat(cacheManager.getCacheNames()).isEmpty();
	});
}
 
Example #13
Source File: ElastiCacheAutoConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void cacheManager_configuredMultipleCachesWithStack_configuresCacheManager() {
	this.contextRunner
			.withUserConfiguration(MockCacheConfigurationWithStackCaches.class)
			.run(context -> {
				CacheManager cacheManager = context.getBean(CachingConfigurer.class)
						.cacheManager();
				assertThat(cacheManager.getCacheNames()
						.contains("sampleCacheOneLogical")).isTrue();
				assertThat(cacheManager.getCacheNames()
						.contains("sampleCacheTwoLogical")).isTrue();
				assertThat(cacheManager.getCacheNames()).hasSize(2);
			});
}
 
Example #14
Source File: ElastiCacheAutoConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void customCachingConfigurerProvided_doesNotCreateDefaultOne() {
	this.contextRunner
			.withUserConfiguration(MockCacheConfigurationWithStackCaches.class,
					CustomCacheConfigurerConfiguration.class)
			.run(context -> {
				assertThat(context.containsBean("cachingConfigurer")).isFalse();
				assertThat(context.getBean("customCachingConfigurer",
						CachingConfigurer.class)).isNotNull();
			});
}
 
Example #15
Source File: ElastiCacheCachingConfiguration.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Bean
public CachingConfigurer cachingConfigurer(AmazonElastiCache amazonElastiCache,
		ResourceIdResolver resourceIdResolver, List<CacheFactory> cacheFactories) {
	if (this.annotationAttributes != null
			&& this.annotationAttributes.getAnnotationArray("value").length > 0) {
		return new ElastiCacheCacheConfigurer(amazonElastiCache, resourceIdResolver,
				getCacheNamesFromCacheClusterConfigs(
						this.annotationAttributes.getAnnotationArray("value")),
				cacheFactories);
	}
	else {
		return new ElastiCacheCacheConfigurer(amazonElastiCache, resourceIdResolver,
				getConfiguredCachesInStack(), cacheFactories);
	}
}
 
Example #16
Source File: ElastiCacheAutoConfiguration.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(CachingConfigurer.class)
public CachingConfigurer cachingConfigurer(AmazonElastiCache amazonElastiCache,
		ResourceIdResolver resourceIdResolver, List<CacheFactory> cacheFactories) {
	if (!this.properties.getClusters().isEmpty()) {
		return new ElastiCacheCacheConfigurer(amazonElastiCache, resourceIdResolver,
				this.properties.getCacheNames(), cacheFactories);
	}
	else {
		return new ElastiCacheCacheConfigurer(amazonElastiCache, resourceIdResolver,
				getConfiguredCachesInStack(), cacheFactories);
	}
}
 
Example #17
Source File: AbstractJCacheConfiguration.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected void useCachingConfigurer(CachingConfigurer config) {
	super.useCachingConfigurer(config);
	if (config instanceof JCacheConfigurer) {
		this.exceptionCacheResolver = ((JCacheConfigurer) config).exceptionCacheResolver();
	}
}
 
Example #18
Source File: AbstractJCacheConfiguration.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void useCachingConfigurer(CachingConfigurer config) {
	super.useCachingConfigurer(config);
	if (config instanceof JCacheConfigurer) {
		this.exceptionCacheResolver = ((JCacheConfigurer) config).exceptionCacheResolver();
	}
}
 
Example #19
Source File: AbstractJCacheConfiguration.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected void useCachingConfigurer(CachingConfigurer config) {
	super.useCachingConfigurer(config);
	if (config instanceof JCacheConfigurer) {
		this.exceptionCacheResolver = ((JCacheConfigurer) config)::exceptionCacheResolver;
	}
}
 
Example #20
Source File: ElastiCacheAutoConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 4 votes vote down vote up
@Test
public void elastiCacheIsDisabled() {
	this.contextRunner.withPropertyValues("cloud.aws.elasticache.enabled:false").run(
			context -> assertThat(context).doesNotHaveBean(CachingConfigurer.class));
}
 
Example #21
Source File: ElastiCacheAutoConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 4 votes vote down vote up
@Bean
CachingConfigurer customCachingConfigurer() {
	return mock(CachingConfigurer.class);
}
 
Example #22
Source File: SpringCacheConfiguration.java    From onetwo with Apache License 2.0 4 votes vote down vote up
@Bean
public CachingConfigurer simpleCachingConfigurer(){
	return new SimpleCachingConfigurer();
}