org.springframework.cache.caffeine.CaffeineCache Java Examples

The following examples show how to use org.springframework.cache.caffeine.CaffeineCache. 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: CacheConfig.java    From ProxyPool with 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 File: CacheConfig.java    From spring-boot-101 with 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
Source File: CacheConfig.java    From spring-boot-redis-guava-caffeine-cache with Apache License 2.0 6 votes vote down vote up
/**
 * 个性化配置缓存
 */
@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 File: MultipleCacheManagerIntegrationTest.java    From tutorials with 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
Source File: CacheConfiguration.java    From GreenSummer with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
  * 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
Source File: CacheConfiguration.java    From GreenSummer with GNU Lesser General Public License v2.1 5 votes vote down vote up
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 File: AuthConfig.java    From flow-platform-x with 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 File: AuthConfig.java    From flow-platform-x with 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
Source File: SecurityConfiguration.java    From cloud-security-xsuaa-integration with Apache License 2.0 5 votes vote down vote up
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 File: CaffeineCacheConfiguration.java    From Groza with 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 File: CaffeineCacheConfiguration.java    From Groza with 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 File: AuthConfig.java    From flow-platform-x with 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 File: AuthConfig.java    From flow-platform-x with 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 File: AbstractCacheDefinition.java    From cloudbreak with 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 File: AclConfig.java    From molgenis with 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());
}