Java Code Examples for org.springframework.cache.caffeine.CaffeineCacheManager

The following examples show how to use org.springframework.cache.caffeine.CaffeineCacheManager. 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: hono   Source File: AbstractAdapterConfig.java    License: Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Create a new cache provider based on Caffeine and Spring Cache.
 *
 * @param minCacheSize The minimum size of the cache.
 * @param maxCacheSize the maximum size of the cache.
 * @return A new cache provider or {@code null} if no cache should be used.
 */
private static CacheProvider newCaffeineCache(final int minCacheSize, final long maxCacheSize) {

    if (maxCacheSize <= 0) {
        return null;
    }

    final Caffeine<Object, Object> caffeine = Caffeine.newBuilder()
            .initialCapacity(minCacheSize)
            .maximumSize(Math.max(minCacheSize, maxCacheSize));

    final CaffeineCacheManager manager = new CaffeineCacheManager();
    manager.setAllowNullValues(false);
    manager.setCaffeine(caffeine);

    return new SpringCacheProvider(manager);
}
 
Example 2
@Test
void shouldAutoEnableCaching() {
	ApplicationContextRunner contextRunner = baseApplicationRunner();

	contextRunner.run(context -> {
		assertThat(context.getBeansOfType(CacheManager.class)).hasSize(1);
		assertThat(
				((CacheManager) context.getBean("caffeineLoadBalancerCacheManager"))
						.getCacheNames()).hasSize(1);
		assertThat(context.getBean("caffeineLoadBalancerCacheManager"))
				.isInstanceOf(CaffeineCacheManager.class);
		assertThat(
				((CacheManager) context.getBean("caffeineLoadBalancerCacheManager"))
						.getCacheNames())
								.contains("CachingServiceInstanceListSupplierCache");
	});
}
 
Example 3
@Test
void loadBalancerCacheShouldNotOverrideExistingCaffeineCacheManager() {
	ApplicationContextRunner contextRunner = baseApplicationRunner()
			.withUserConfiguration(TestConfiguration.class);

	contextRunner.run(context -> {
		assertThat(context.getBeansOfType(CacheManager.class)).hasSize(2);
		assertThat(context.getBean("cacheManager"))
				.isInstanceOf(CaffeineCacheManager.class);
		assertThat(((CacheManager) context.getBean("cacheManager")).getCacheNames())
				.isEmpty();
		assertThat(
				((CacheManager) context.getBean("caffeineLoadBalancerCacheManager"))
						.getCacheNames()).hasSize(1);
		assertThat(
				((CacheManager) context.getBean("caffeineLoadBalancerCacheManager"))
						.getCacheNames())
								.contains("CachingServiceInstanceListSupplierCache");
	});

}
 
Example 4
Source Project: hedera-mirror-node   Source File: CacheConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean(EXPIRE_AFTER_5M)
@Primary
CacheManager cacheManager5m() {
    CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager();
    caffeineCacheManager.setCacheSpecification("maximumSize=100,expireAfterWrite=5m");
    return caffeineCacheManager;
}
 
Example 5
Source Project: hawkbit   Source File: RolloutStatusCache.java    License: Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param tenantAware
 *            to get current tenant
 * @param size
 *            the maximum size of the cache
 */
public RolloutStatusCache(final TenantAware tenantAware, final long size) {
    this.tenantAware = tenantAware;

    final Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder().maximumSize(size);
    final CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager();
    caffeineCacheManager.setCaffeine(cacheBuilder);

    this.cacheManager = new TenantAwareCacheManager(caffeineCacheManager, tenantAware);
}
 
Example 6
Source Project: hawkbit   Source File: CacheAutoConfiguration.java    License: Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @return the direct cache manager to access without tenant aware
 *         check, cause in sometimes it's necessary to access the cache
 *         directly without having the current tenant, e.g. initial
 *         creation of tenant
 */
@Bean(name = "directCacheManager")
@ConditionalOnMissingBean(name = "directCacheManager")
public CacheManager directCacheManager(final CacheProperties cacheProperties) {
    final CaffeineCacheManager cacheManager = new CaffeineCacheManager();

    if (cacheProperties.getTtl() > 0) {
        final Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder()
                .expireAfterWrite(cacheProperties.getTtl(), cacheProperties.getTtlUnit());
        cacheManager.setCaffeine(cacheBuilder);
    }

    return cacheManager;
}
 
Example 7
Source Project: kaif   Source File: UtilConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public CacheManager rssHotArticlesCacheManager() {
  Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder()
      .expireAfterWrite(4, TimeUnit.HOURS)
      .maximumSize(100);
  CaffeineCacheManager cacheManager = new CaffeineCacheManager("rssHotArticles");
  cacheManager.setCaffeine(cacheBuilder);
  return cacheManager;
}
 
Example 8
Source Project: kaif   Source File: UtilConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public CacheManager zoneInfoCacheManager() {
  Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder()
      .expireAfterWrite(10, TimeUnit.MINUTES)
      .maximumSize(2000);
  CaffeineCacheManager cacheManager = new CaffeineCacheManager("ZoneInfo");
  cacheManager.setCaffeine(cacheBuilder);
  return cacheManager;
}
 
Example 9
Source Project: kaif   Source File: UtilConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public CacheManager articleCacheManager() {
  Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder()
      .expireAfterWrite(10, TimeUnit.MINUTES)
      .maximumSize(2000);
  CaffeineCacheManager cacheManager = new CaffeineCacheManager("Article");
  cacheManager.setCaffeine(cacheBuilder);
  return cacheManager;
}
 
Example 10
Source Project: kaif   Source File: UtilConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * hot zones cache, refresh every one hour. no need to distribute if we have multiple web servers
 *
 * @see {@link io.kaif.model.article.ArticleDao#listHotZonesWithCache(int, java.time.Instant)}
 */
@Bean
public CacheManager listHotZonesCacheManager() {
  Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder()
      .expireAfterWrite(1, TimeUnit.HOURS)
      .maximumSize(1000);
  CaffeineCacheManager cacheManager = new CaffeineCacheManager("listHotZones");
  cacheManager.setCaffeine(cacheBuilder);
  return cacheManager;
}
 
Example 11
Source Project: kaif   Source File: UtilConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * administrators cache, refresh every one minutes. no need to distribute if we have multiple
 * web servers (user just not see new administrators)
 */
@Bean
public CacheManager listAdministratorsCacheManager() {
  Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder()
      .expireAfterWrite(1, TimeUnit.MINUTES)
      .maximumSize(1000);
  CaffeineCacheManager cacheManager = new CaffeineCacheManager("listAdministrators");
  cacheManager.setCaffeine(cacheBuilder);
  return cacheManager;
}
 
Example 12
Source Project: kaif   Source File: UtilConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * short life client app user cache (the cache is Optional<ClientAppUser>), so this is not
 * distribute-able
 *
 * @see {@link io.kaif.model.clientapp.ClientAppDao#findClientAppUserWithCache}
 */
@Bean
public CacheManager findClientAppUserCacheManager() {
  Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder()
      .expireAfterWrite(1, TimeUnit.MINUTES)
      .maximumSize(1000);
  CaffeineCacheManager cacheManager = new CaffeineCacheManager("findClientAppUser");
  cacheManager.setCaffeine(cacheBuilder);
  return cacheManager;
}
 
Example 13
Source Project: kaif   Source File: UtilConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public CacheManager honorRollsCacheManager() {
  Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder()
      .expireAfterWrite(1, TimeUnit.HOURS)
      .maximumSize(100);
  CaffeineCacheManager cacheManager = new CaffeineCacheManager("listHonorRoll");
  cacheManager.setCaffeine(cacheBuilder);
  return cacheManager;
}
 
Example 14
@Test
void caffeineLoadBalancerCacheShouldNotOverrideCacheTypeSetting() {
	ApplicationContextRunner contextRunner = baseApplicationRunner()
			.withUserConfiguration(TestConfiguration.class)
			.withPropertyValues("spring.cache.type=none");

	contextRunner.run(context -> {
		assertThat(context.getBeansOfType(CacheManager.class)).hasSize(2);
		assertThat(context.getBean("caffeineLoadBalancerCacheManager"))
				.isInstanceOf(CaffeineCacheManager.class);
		assertThat(context.getBeansOfType(CacheManager.class).get("cacheManager"))
				.isInstanceOf(NoOpCacheManager.class);

	});
}
 
Example 15
@Test
void shouldNotInstantiateCaffeineLoadBalancerCacheIfDisabled() {
	ApplicationContextRunner contextRunner = baseApplicationRunner()
			.withPropertyValues("spring.cloud.loadbalancer.cache.enabled=false")
			.withUserConfiguration(TestConfiguration.class);

	contextRunner.run(context -> {
		assertThat(context.getBeansOfType(CacheManager.class)).hasSize(1);
		assertThat(context.getBean("cacheManager"))
				.isInstanceOf(CaffeineCacheManager.class);
		assertThat(((CacheManager) context.getBean("cacheManager")).getCacheNames())
				.isEmpty();
	});
}
 
Example 16
Source Project: tutorials   Source File: CaffeineConfiguration.java    License: MIT License 5 votes vote down vote up
@Bean
public CacheManager cacheManager(Caffeine caffeine) {
    CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager();
    caffeineCacheManager.getCache("addresses");
    caffeineCacheManager.setCaffeine(caffeine);
    return caffeineCacheManager;
}
 
Example 17
Source Project: genie   Source File: CachingAutoConfigurationTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * The auto configuration creates the expected beans.
 */
@Test
void expectedBeansExist() {
    this.contextRunner.run(
        context -> {
            // This should be provided by the Spring Boot starter after @EnableCaching is applied
            Assertions.assertThat(context).hasSingleBean(CacheManager.class);
            Assertions.assertThat(context).hasSingleBean(CaffeineCacheManager.class);
        }
    );
}
 
Example 18
Source Project: hedera-mirror-node   Source File: CacheConfiguration.java    License: Apache License 2.0 4 votes vote down vote up
@Bean(ENTITY_CACHE)
CacheManager entityCache(GrpcProperties grpcProperties) {
    CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager();
    caffeineCacheManager.setCacheSpecification("maximumSize=" + grpcProperties.getEntityCacheSize());
    return caffeineCacheManager;
}
 
Example 19
Source Project: hedera-mirror-node   Source File: CacheConfiguration.java    License: Apache License 2.0 4 votes vote down vote up
@Bean(EXPIRE_AFTER_30M)
CacheManager cacheManager30m() {
    CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager();
    caffeineCacheManager.setCacheSpecification("maximumSize=10000,expireAfterWrite=30m");
    return caffeineCacheManager;
}
 
Example 20
Source Project: hedera-mirror-node   Source File: CacheConfiguration.java    License: Apache License 2.0 4 votes vote down vote up
@Bean(TINY_LRU_CACHE)
CacheManager tinyLruCache() {
    CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager();
    caffeineCacheManager.setCacheSpecification("maximumSize=100");
    return caffeineCacheManager;
}
 
Example 21
Source Project: Spring-5.0-Cookbook   Source File: CachingConfig.java    License: MIT License 4 votes vote down vote up
@Bean
@Qualifier("caffeineCacheManager")
public CaffeineCacheManager caffeineCacheManager() {
    return new CaffeineCacheManager();
}
 
Example 22
Source Project: Spring-5.0-Cookbook   Source File: CachingConfig.java    License: MIT License 4 votes vote down vote up
@Bean
@Qualifier("caffeineCacheManager")
public CaffeineCacheManager caffeineCacheManager() {
    return new CaffeineCacheManager();
}
 
Example 23
Source Project: Spring-5.0-Cookbook   Source File: CachingConfig.java    License: MIT License 4 votes vote down vote up
@Bean
@Qualifier("caffeineCacheManager")
public CaffeineCacheManager caffeineCacheManager() {
    return new CaffeineCacheManager();
}
 
Example 24
Source Project: Spring-5.0-Cookbook   Source File: CachingConfig.java    License: MIT License 4 votes vote down vote up
@Bean
@Qualifier("caffeineCacheManager")
public CaffeineCacheManager caffeineCacheManager() {
    return new CaffeineCacheManager();
}
 
Example 25
Source Project: Spring-5.0-Cookbook   Source File: CachingConfig.java    License: MIT License 4 votes vote down vote up
@Bean
@Qualifier("caffeineCacheManager")
public CaffeineCacheManager caffeineCacheManager() {
    return new CaffeineCacheManager();
}
 
Example 26
Source Project: Spring-5.0-Cookbook   Source File: CachingConfig.java    License: MIT License 4 votes vote down vote up
@Bean
@Qualifier("caffeineCacheManager")
public CaffeineCacheManager caffeineCacheManager() {
    return new CaffeineCacheManager();
}
 
Example 27
Source Project: Spring-5.0-Cookbook   Source File: CachingConfig.java    License: MIT License 4 votes vote down vote up
@Bean
@Qualifier("caffeineCacheManager")
public CaffeineCacheManager caffeineCacheManager() {
    return new CaffeineCacheManager();
}
 
Example 28
Source Project: Spring-5.0-Cookbook   Source File: CachingConfig.java    License: MIT License 4 votes vote down vote up
@Bean
@Qualifier("caffeineCacheManager")
public CaffeineCacheManager caffeineCacheManager() {
    return new CaffeineCacheManager();
}
 
Example 29
Source Project: Spring-5.0-Cookbook   Source File: CachingConfig.java    License: MIT License 4 votes vote down vote up
@Bean
@Qualifier("caffeineCacheManager")
public CaffeineCacheManager caffeineCacheManager() {
    return new CaffeineCacheManager();
}
 
Example 30
Source Project: zhcet-web   Source File: CacheConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
public CacheManager cacheManager() {
    return new CaffeineCacheManager();
}