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

The following examples show how to use org.springframework.cache.caffeine.CaffeineCache. 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: ProxyPool   Source File: CacheConfig.java    License: Apache License 2.0 7 votes vote down vote up
/**
 * 创建基于Caffeine的Cache Manager
 * @return
 */
@Bean
@Primary
public CacheManager caffeineCacheManager() {
    SimpleCacheManager cacheManager = new SimpleCacheManager();

    ArrayList<CaffeineCache> caches = new ArrayList<CaffeineCache>();
    for(Caches c : Caches.values()){
        caches.add(new CaffeineCache(c.name(),
                Caffeine.newBuilder().recordStats()
                        .expireAfterWrite(c.getTtl(), TimeUnit.SECONDS)
                        .maximumSize(c.getMaxSize())
                        .build())
        );
    }

    cacheManager.setCaches(caches);

    return cacheManager;
}
 
Example 2
Source Project: spring-boot-101   Source File: CacheConfig.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 创建基于Caffeine的Cache Manager
 * @return
 */
@Bean
@Primary
public CacheManager caffeineCacheManager() {
	SimpleCacheManager cacheManager = new SimpleCacheManager();
	
	ArrayList<CaffeineCache> caches = new ArrayList<CaffeineCache>();
	for(Caches c : Caches.values()){
		caches.add(new CaffeineCache(c.name(), 
			Caffeine.newBuilder().recordStats()
			.expireAfterWrite(c.getTtl(), TimeUnit.SECONDS)
			.maximumSize(c.getMaxSize())
			.build())
		);
	}
	
	cacheManager.setCaches(caches);

	return cacheManager;
}
 
Example 3
/**
 * 个性化配置缓存
 */
@Bean
public CacheManager cacheManager() {
    SimpleCacheManager manager = new SimpleCacheManager();
    //把各个cache注册到cacheManager中,CaffeineCache实现了org.springframework.cache.Cache接口
    ArrayList<CaffeineCache> caches = new ArrayList<>();
    for (Caches c : Caches.values()) {
        caches.add(new CaffeineCache(c.name(),
                Caffeine.newBuilder().recordStats()
                        .expireAfterWrite(c.getTtl(), TimeUnit.SECONDS)
                        .maximumSize(c.getMaxSize())
                        .build())
        );
    }
    manager.setCaches(caches);
    return manager;
}
 
Example 4
Source Project: tutorials   Source File: MultipleCacheManagerIntegrationTest.java    License: MIT License 6 votes vote down vote up
@Test
public void givenCacheResolverIsConfigured_whenCallGetOrderDetail_thenDataShouldBeInCaffieneCacheManager() {
    Integer key = 30001;
    cacheManager.getCache("orders")
        .evict(key);
    Order order = new Order();
    order.setCustomerId(1001);
    order.setItemId(10001);
    order.setOrderId(30001);
    order.setQuantity(2);
    Mockito.when(orderDetailRepository.getOrderDetail(key))
        .thenReturn(order);
    orderDetailBO.getOrderDetail(key);
    org.springframework.cache.caffeine.CaffeineCache cache = (CaffeineCache) cacheManager.getCache("orders");
    Assert.notNull(cache.get(key)
        .get(), "caffieneCache should have had the data");
}
 
Example 5
/**
  * The cache manager Bean, that defines caching parameters
  *
  * @param ticker The Ticker handle that Spring passes, unused so far
  * @return the cache manager
  */
 @Bean
 public CacheManager cacheManager(Ticker ticker) {
     SimpleCacheManager manager = new SimpleCacheManager();
     if (specs != null) {
         //@formatter:off
List<CaffeineCache> caches =
		specs.entrySet().stream()
			.map(entry -> buildCache(entry.getKey(), entry.getValue(), ticker))
			.collect(Collectors.toList());
//@formatter:on
         manager.setCaches(caches);
     }
     return manager;
 }
 
Example 6
private CaffeineCache buildCache(String name, CacheSpec cacheSpec, Ticker ticker) {
      log.info("Cache {} specified timeout of {} min, max of {}", name, cacheSpec.getTimeout(), cacheSpec.getMax());
      //@formatter:off
final Caffeine<Object, Object> caffeineBuilder
		= Caffeine.newBuilder()
			.expireAfterWrite(cacheSpec.getTimeout(), TimeUnit.MINUTES)
			.maximumSize(cacheSpec.getMax())
			.ticker(ticker);
//@formatter:on
      return new CaffeineCache(name, caffeineBuilder.build());
  }
 
Example 7
Source Project: flow-platform-x   Source File: AuthConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public Cache onlineUsersCache() {
    return new CaffeineCache(CACHE_ONLINE,
            Caffeine.newBuilder()
                    .maximumSize(MaxCacheSize)
                    .expireAfterWrite(authProperties.getExpireSeconds(), TimeUnit.SECONDS)
                    .build());
}
 
Example 8
Source Project: flow-platform-x   Source File: AuthConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public Cache refreshTokenCache() {
    return new CaffeineCache(CACHE_REFRESH_TOKEN,
            Caffeine.newBuilder()
                    .maximumSize(MaxCacheSize)
                    .expireAfterWrite(authProperties.getRefreshExpiredSeconds(), TimeUnit.SECONDS)
                    .build());
}
 
Example 9
BearerTokenResolver getTokenBrokerResolver() {
	Cache cache = new CaffeineCache("token",
			Caffeine.newBuilder()
					.expireAfterWrite(15, TimeUnit.MINUTES)
					.maximumSize(100).build(), false);

	return new TokenBrokerResolver(xsuaaServiceConfiguration, cache, AuthenticationMethod.BASIC);
}
 
Example 10
Source Project: Groza   Source File: CaffeineCacheConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public CacheManager cacheManager() {
    SimpleCacheManager manager = new SimpleCacheManager();
    if (specs != null) {
        List<CaffeineCache> caches =
                specs.entrySet().stream()
                .map(entry -> buildCache(entry.getKey(),
                        entry.getValue()))
                .collect(Collectors.toList());
        manager.setCaches(caches);
    }
    return manager;
}
 
Example 11
Source Project: Groza   Source File: CaffeineCacheConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
private CaffeineCache buildCache(String name, CacheSpecs cacheSpecs) {
    final Caffeine<Object, Object> caffeineBuilder
            = Caffeine.newBuilder()
            .expireAfterWrite(cacheSpecs.getTimeToLiveInMinutes(), TimeUnit.MINUTES)
            .maximumSize(cacheSpecs.getMaxSize())
            .ticker(ticker());
    return new CaffeineCache(name, caffeineBuilder.build());
}
 
Example 12
Source Project: flow-platform-x   Source File: AuthConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public Cache onlineUsersCache() {
    return new CaffeineCache(CACHE_ONLINE,
            Caffeine.newBuilder()
                    .maximumSize(MaxCacheSize)
                    .expireAfterWrite(authProperties.getExpireSeconds(), TimeUnit.SECONDS)
                    .build());
}
 
Example 13
Source Project: flow-platform-x   Source File: AuthConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public Cache refreshTokenCache() {
    return new CaffeineCache(CACHE_REFRESH_TOKEN,
            Caffeine.newBuilder()
                    .maximumSize(MaxCacheSize)
                    .expireAfterWrite(authProperties.getRefreshExpiredSeconds(), TimeUnit.SECONDS)
                    .build());
}
 
Example 14
Source Project: cloudbreak   Source File: AbstractCacheDefinition.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public final Cache cacheConfiguration() {
    Caffeine<Object, Object> builder = Caffeine.newBuilder()
            .recordStats()
            .maximumSize(getMaxEntries())
            .expireAfterWrite(getTimeToLiveSeconds(), TimeUnit.SECONDS);
    return new CaffeineCache(getName(), builder.build());
}
 
Example 15
Source Project: molgenis   Source File: AclConfig.java    License: GNU Lesser General Public License v3.0 4 votes vote down vote up
@Bean
public AclCache aclCache() {
  Cache cache = new CaffeineCache("aclCache", Caffeine.newBuilder().maximumSize(10000).build());
  return new SpringCacheBasedAclCache(
      cache, permissionGrantingStrategy(), aclAuthorizationStrategy());
}