Java Code Examples for com.github.benmanes.caffeine.cache.Caffeine#build()

The following examples show how to use com.github.benmanes.caffeine.cache.Caffeine#build() . 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: CaffeineCache.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked"})
private Cache<K, V> buildCache(Cache<K, V> prev) {
  @SuppressWarnings({"rawtypes"})
  Caffeine builder = Caffeine.newBuilder()
      .initialCapacity(initialSize)
      .executor(executor)
      .removalListener(this)
      .recordStats();
  if (maxIdleTimeSec > 0) {
    builder.expireAfterAccess(Duration.ofSeconds(maxIdleTimeSec));
  }
  if (maxRamBytes != Long.MAX_VALUE) {
    builder.maximumWeight(maxRamBytes);
    builder.weigher((k, v) -> (int) (RamUsageEstimator.sizeOfObject(k) + RamUsageEstimator.sizeOfObject(v)));
  } else {
    builder.maximumSize(maxSize);
  }
  Cache<K, V> newCache = builder.build();
  if (prev != null) {
    newCache.putAll(prev.asMap());
  }
  return newCache;
}
 
Example 2
Source File: CaffeinePolicy.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public CaffeinePolicy(Config config, Set<Characteristic> characteristics) {
  policyStats = new PolicyStats("product.Caffeine");
  BasicSettings settings = new BasicSettings(config);
  Caffeine<Long, AccessEvent> builder = Caffeine.newBuilder()
      .removalListener((Long key, AccessEvent value, RemovalCause cause) ->
          policyStats.recordEviction())
      .initialCapacity(settings.maximumSize())
      .executor(Runnable::run);
  if (characteristics.contains(WEIGHTED)) {
    builder.maximumWeight(settings.maximumSize());
    builder.weigher((key, value) -> value.weight());
  } else {
    builder.maximumSize(settings.maximumSize());
  }
  cache = builder.build();
}
 
Example 3
Source File: AuthCache.java    From timely with Apache License 2.0 6 votes vote down vote up
private static Cache<String, TimelyPrincipal> getCache() {
    if (-1 == cacheExpirationMinutes) {
        throw new IllegalStateException("Cache session max age not configured.");
    }
    if (null == CACHE) {
        Caffeine<Object, Object> caffeine = Caffeine.newBuilder();
        caffeine.expireAfterWrite(cacheExpirationMinutes, TimeUnit.MINUTES);
        if (cacheRefreshMinutes > 0) {
            caffeine.refreshAfterWrite(cacheRefreshMinutes, TimeUnit.MINUTES);
            CACHE = caffeine.build(key -> getTimelyPrincipal(key));
        } else {
            CACHE = caffeine.build();
        }
    }
    return CACHE;
}
 
Example 4
Source File: CaffeineProvider.java    From J2Cache with Apache License 2.0 6 votes vote down vote up
/**
 * 返回对 Caffeine cache 的 封装
 * @param region region name
 * @param size   max cache object size in memory
 * @param expire cache object expire time in millisecond
 *               if this parameter set to 0 or negative numbers
 *               means never expire
 * @param listener  j2cache cache listener
 * @return CaffeineCache
 */
private CaffeineCache newCaffeineCache(String region, long size, long expire, CacheExpiredListener listener) {
    Caffeine<Object, Object> caffeine = Caffeine.newBuilder();
    caffeine = caffeine.maximumSize(size)
        .removalListener((k,v, cause) -> {
            /*
             * 程序删除的缓存不做通知处理,因为上层已经做了处理
             * 当缓存数据不是因为手工删除和超出容量限制而被删除的情况,就需要通知上层侦听器
             */
            if(cause != RemovalCause.EXPLICIT && cause != RemovalCause.REPLACED && cause != RemovalCause.SIZE)
                listener.notifyElementExpired(region, (String)k);
        });
    if (expire > 0) {
        caffeine = caffeine.expireAfterWrite(expire, TimeUnit.SECONDS);
    }
    com.github.benmanes.caffeine.cache.Cache<String, Object> loadingCache = caffeine.build();
    return new CaffeineCache(loadingCache, size, expire);
}
 
Example 5
Source File: FileService.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static Cache<PathAndEncoding, AggregatedHttpFile> newCache(String cacheSpec) {
    final Caffeine<Object, Object> b = Caffeine.from(cacheSpec);
    b.recordStats()
     .removalListener((RemovalListener<PathAndEncoding, AggregatedHttpFile>) (key, value, cause) -> {
         if (value != null) {
             final HttpData content = value.content();
             if (content instanceof ByteBufHolder) {
                 ((ByteBufHolder) content).release();
             }
         }
     });
    return b.build();
}
 
Example 6
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 7
Source File: DefaultMessageDistributor.java    From metron with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new message distributor.
 *
 * @param periodDurationMillis The period duration in milliseconds.
 * @param profileTimeToLiveMillis The time-to-live of a profile in milliseconds.
 * @param maxNumberOfRoutes The max number of unique routes to maintain.  After this is exceeded, lesser
 *                          used routes will be evicted from the internal cache.
 * @param ticker The ticker used to drive time for the caches.  Only needs set for testing.
 */
public DefaultMessageDistributor(
        long periodDurationMillis,
        long profileTimeToLiveMillis,
        long maxNumberOfRoutes,
        Ticker ticker) {

  if(profileTimeToLiveMillis < periodDurationMillis) {
    throw new IllegalStateException(format(
            "invalid configuration: expect profile TTL (%d) to be greater than period duration (%d)",
            profileTimeToLiveMillis,
            periodDurationMillis));
  }
  this.periodDurationMillis = periodDurationMillis;

  // build the cache of active profiles
  Caffeine<Integer, ProfileBuilder> activeCacheBuilder = Caffeine
          .newBuilder()
          .maximumSize(maxNumberOfRoutes)
          .expireAfterAccess(profileTimeToLiveMillis, TimeUnit.MILLISECONDS)
          .ticker(ticker)
          .writer(new ActiveCacheWriter());
  if (LOG.isDebugEnabled()) {
    activeCacheBuilder.recordStats();
  }
  this.activeCache = activeCacheBuilder.build();

  // build the cache of expired profiles
  Caffeine<Integer, ProfileBuilder> expiredCacheBuilder = Caffeine
          .newBuilder()
          .maximumSize(maxNumberOfRoutes)
          .expireAfterWrite(profileTimeToLiveMillis, TimeUnit.MILLISECONDS)
          .ticker(ticker)
          .writer(new ExpiredCacheWriter());
  if (LOG.isDebugEnabled()) {
    expiredCacheBuilder.recordStats();
  }
  this.expiredCache = expiredCacheBuilder.build();
}
 
Example 8
Source File: CachedStormStatusServiceImpl.java    From metron with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param stormService service to decorate and delegate calls to.
 * @param maxCacheSize max number of records in the backing cache.
 * @param cacheExpirationSeconds number of seconds before the cache will expire an entry.
 */
public CachedStormStatusServiceImpl(StormStatusService stormService, long maxCacheSize,
    long cacheExpirationSeconds) {
  this.stormService = stormService;
  LOG.info("Creating Storm service cache with max size '{}', record expiration seconds '{}'",
      maxCacheSize, cacheExpirationSeconds);
  Caffeine builder = Caffeine.newBuilder().maximumSize(maxCacheSize)
      .expireAfterWrite(cacheExpirationSeconds, TimeUnit.SECONDS);
  statusCache = builder.build();
}
 
Example 9
Source File: CachingStellarProcessor.java    From metron with Apache License 2.0 5 votes vote down vote up
/**
 * Create a cache given a config.  Note that if the cache size is {@literal <}= 0, then no cache will be returned.
 * @param config
 * @return A cache.
 */
public static Cache<Key, Object> createCache(Map<String, Object> config) {

  // the cache configuration is required
  if(config == null) {
    LOG.debug("Cannot create cache; missing cache configuration");
    return null;
  }

  // max cache size is required
  Long maxSize = getParam(config, MAX_CACHE_SIZE_PARAM, null, Long.class);
  if(maxSize == null || maxSize <= 0) {
    LOG.error("Cannot create cache; missing or invalid configuration; {} = {}", MAX_CACHE_SIZE_PARAM, maxSize);
    return null;
  }

  // max time retain is required
  Integer maxTimeRetain = getParam(config, MAX_TIME_RETAIN_PARAM, null, Integer.class);
  if(maxTimeRetain == null || maxTimeRetain <= 0) {
    LOG.error("Cannot create cache; missing or invalid configuration; {} = {}", MAX_TIME_RETAIN_PARAM, maxTimeRetain);
    return null;
  }

  Caffeine<Object, Object> cache = Caffeine
          .newBuilder()
          .maximumSize(maxSize)
          .expireAfterWrite(maxTimeRetain, TimeUnit.MINUTES);

  // record stats is optional
  Boolean recordStats = getParam(config, RECORD_STATS, false, Boolean.class);
  if(recordStats) {
    cache.recordStats();
  }

  return cache.build();
}
 
Example 10
Source File: ConcurrencyContext.java    From metron with Apache License 2.0 5 votes vote down vote up
public synchronized void initialize( int numThreads
                                   , long maxCacheSize
                                   , long maxTimeRetain
                                   , WorkerPoolStrategies poolStrategy
                                   , Logger log
                                   , boolean logStats
                                   ) {
  if(executor == null) {
    if (log != null) {
      log.info("Creating new threadpool of size {}", numThreads);
    }
    executor = (poolStrategy == null? WorkerPoolStrategies.FIXED:poolStrategy).create(numThreads);
  }
  if(cache == null) {
    if (log != null) {
      log.info("Creating new cache with maximum size {}, and expiration after write of {} minutes", maxCacheSize, maxTimeRetain);
    }
    Caffeine builder = Caffeine.newBuilder().maximumSize(maxCacheSize)
                         .expireAfterWrite(maxTimeRetain, TimeUnit.MINUTES)
                         .executor(executor)
                       ;
    if(logStats) {
      builder = builder.recordStats();
    }
    cache = builder.build();
  }
}
 
Example 11
Source File: CaffeineCacheFactory.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
@Override
public <K, V> Cache<K, V> build(final String name, final CacheConfig<K, V> config) {
    Caffeine<Object, Object> builder = Caffeine.newBuilder();
    if (config.getExpireAfterWrite() > 0) {
        builder.expireAfterWrite(config.getExpireAfterWrite(), TimeUnit.MILLISECONDS);
    }
    builder.maximumSize(config.getCapacity() > 0 ? config.getCapacity() : Long.MAX_VALUE);
    com.github.benmanes.caffeine.cache.Cache<K, CacheObject<V>> cache = builder.build();
    return new CaffeineCache<>(cache, config);
}
 
Example 12
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 13
Source File: CaffeineCache.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
public CaffeineCache(String name, int capacity, Mapper<K, V> loader, long ttl, boolean statistics, boolean manageable) {
	this.name = name;
	this.capacity = capacity;
	this.ttl = ttl;
	this.loading = loader != null;

	Caffeine<Object, Object> builder = Caffeine.newBuilder();

	if (capacity > 0) {
		builder.maximumSize(capacity);
	}

	if (ttl > 0) {
		builder.expireAfterWrite(ttl, TimeUnit.MILLISECONDS);
	}

	if (statistics) {
		builder.recordStats();
	}

	if (manageable) {
		new ManageableCache(this);
	}

	if (loading) {
		this.loadingCache = builder.build(loader::map);
		this.cache = loadingCache;

	} else {
		this.loadingCache = null;
		this.cache = builder.build();
	}
}
 
Example 14
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 15
Source File: AbstractOAuth2TokenService.java    From cloud-security-xsuaa-integration with Apache License 2.0 5 votes vote down vote up
private Cache<CacheKey, OAuth2TokenResponse> createResponseCache(Ticker cacheTicker, boolean sameThreadCache) {
	Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder()
			.maximumSize(getCacheConfiguration().getCacheSize())
			.ticker(cacheTicker)
			.expireAfterWrite(getCacheConfiguration().getCacheDuration());
	if (sameThreadCache) {
		cacheBuilder.executor(Runnable::run);
	}
	return cacheBuilder.build();
}
 
Example 16
Source File: ActiveQueryLog.java    From datawave with Apache License 2.0 4 votes vote down vote up
private Cache<String,ActiveQuery> setupCache(long maxIdle) {
    Caffeine<Object,Object> caffeine = Caffeine.newBuilder();
    caffeine.expireAfterAccess(maxIdle, TimeUnit.MILLISECONDS);
    return caffeine.build();
}
 
Example 17
Source File: CaffeineUtils.java    From t-io with Apache License 2.0 4 votes vote down vote up
/**
 * @param cacheName
 * @param timeToLiveSeconds 设置写缓存后过期时间(单位:秒)
 * @param timeToIdleSeconds 设置读缓存后过期时间(单位:秒)
 * @param initialCapacity
 * @param maximumSize
 * @param recordStats
 * @param removalListener
 * @return
 */
public static <K, V> LoadingCache<K, V> createLoadingCache(String cacheName, Long timeToLiveSeconds, Long timeToIdleSeconds, Integer initialCapacity, Integer maximumSize,
        boolean recordStats, RemovalListener<K, V> removalListener) {

	if (removalListener == null) {
		removalListener = new DefaultRemovalListener<K, V>(cacheName);
	}

	Caffeine<K, V> cacheBuilder = Caffeine.newBuilder().removalListener(removalListener);

	//设置并发级别为8,并发级别是指可以同时写缓存的线程数
	//		cacheBuilder.concurrencyLevel(concurrencyLevel);
	if (timeToLiveSeconds != null && timeToLiveSeconds > 0) {
		//设置写缓存后8秒钟过期
		cacheBuilder.expireAfterWrite(timeToLiveSeconds, TimeUnit.SECONDS);
	}
	if (timeToIdleSeconds != null && timeToIdleSeconds > 0) {
		//设置访问缓存后8秒钟过期
		cacheBuilder.expireAfterAccess(timeToIdleSeconds, TimeUnit.SECONDS);
	}

	//设置缓存容器的初始容量为10
	cacheBuilder.initialCapacity(initialCapacity);
	//设置缓存最大容量为100,超过100之后就会按照LRU最近最少使用算法来移除缓存项
	cacheBuilder.maximumSize(maximumSize);

	if (recordStats) {
		//设置要统计缓存的命中率
		cacheBuilder.recordStats();
	}
	//build方法中可以指定CacheLoader,在缓存不存在时通过CacheLoader的实现自动加载缓存
	LoadingCache<K, V> loadingCache = cacheBuilder.build(new CacheLoader<K, V>() {
		@Override
		public V load(K key) throws Exception {
			return null;
		}
	});

	return loadingCache;

	//		for (int i = 0; i < 20; i++)
	//		{
	//			//从缓存中得到数据,由于我们没有设置过缓存,所以需要通过CacheLoader加载缓存数据
	//			Long student = studentCache.get("p");
	//			System.out.println(student);
	//			//休眠1秒
	//			TimeUnit.SECONDS.sleep(1);
	//		}

	//		System.out.println("cache stats:");
	//最后打印缓存的命中率等 情况
	//		System.out.println(studentCache.stats().toString());
}
 
Example 18
Source File: KnowledgeBaseServiceImpl.java    From inception with Apache License 2.0 4 votes vote down vote up
@Autowired
public KnowledgeBaseServiceImpl(RepositoryProperties aRepoProperties,
        KnowledgeBaseProperties aKBProperties)
{
    Caffeine<QueryKey, List<KBHandle>> cacheBuilder = Caffeine.newBuilder()
            .maximumWeight(aKBProperties.getCacheSize())
            .expireAfterAccess(aKBProperties.getCacheExpireDelay())
            .refreshAfterWrite(aKBProperties.getCacheRefreshDelay())
            .weigher((QueryKey key, List<KBHandle> value) -> value.size());
    
    if (log.isTraceEnabled()) {
        cacheBuilder.recordStats();
    }
    
    queryCache = cacheBuilder.build(this::runQuery);
    
    kbRepositoriesRoot = new File(aRepoProperties.getPath(), "kb");
    
    // Originally, the KBs were stored next to the repository folder - but they should be
    // *under* the repository folder
    File legacyLocation = new File(System.getProperty(SettingsUtil.getPropApplicationHome(),
            System.getProperty("user.home") + "/"
                    + SettingsUtil.getApplicationUserHomeSubdir()),
            "kb");

    if (legacyLocation.exists() && legacyLocation.isDirectory()) {
        try {
            log.info("Found legacy KB folder at [" + legacyLocation
                    + "]. Trying to move it to the new location at [" + kbRepositoriesRoot
                    + "]");
            Files.createDirectories(kbRepositoriesRoot.getParentFile().toPath());
            Files.move(legacyLocation.toPath(), kbRepositoriesRoot.toPath(), REPLACE_EXISTING);
            log.info("Move successful.");
        }
        catch (IOException e) {
            throw new RuntimeException("Detected legacy KB folder at [" + legacyLocation
                    + "] but cannot move it to the new location at [" + kbRepositoriesRoot
                    + "]. Please perform the move manually and ensure that the application "
                    + "has all necessary permissions to the new location - then try starting "
                    + "the application again.");
        }
    }
    
    repoManager = RepositoryProvider.getRepositoryManager(kbRepositoriesRoot);
    log.info("Knowledge base repository path: {}", kbRepositoriesRoot);
}
 
Example 19
Source File: CaffeineCacheFromContext.java    From caffeine with Apache License 2.0 4 votes vote down vote up
public static <K, V> Cache<K, V> newCaffeineCache(CacheContext context) {
  Caffeine<Object, Object> builder = Caffeine.newBuilder();
  context.caffeine = builder;

  if (context.initialCapacity != InitialCapacity.DEFAULT) {
    builder.initialCapacity(context.initialCapacity.size());
  }
  if (context.isRecordingStats()) {
    builder.recordStats();
  }
  if (context.maximumSize != Maximum.DISABLED) {
    if (context.weigher == CacheWeigher.DEFAULT) {
      builder.maximumSize(context.maximumSize.max());
    } else {
      builder.weigher(context.weigher);
      builder.maximumWeight(context.maximumWeight());
    }
  }
  if (context.expiryType() != CacheExpiry.DISABLED) {
    builder.expireAfter(context.expiry);
  }
  if (context.afterAccess != Expire.DISABLED) {
    builder.expireAfterAccess(context.afterAccess.timeNanos(), TimeUnit.NANOSECONDS);
  }
  if (context.afterWrite != Expire.DISABLED) {
    builder.expireAfterWrite(context.afterWrite.timeNanos(), TimeUnit.NANOSECONDS);
  }
  if (context.refresh != Expire.DISABLED) {
    builder.refreshAfterWrite(context.refresh.timeNanos(), TimeUnit.NANOSECONDS);
  }
  if (context.expires() || context.refreshes()) {
    SerializableTicker ticker = context.ticker()::read;
    builder.ticker(ticker);
  }
  if (context.keyStrength == ReferenceType.WEAK) {
    builder.weakKeys();
  } else if (context.keyStrength == ReferenceType.SOFT) {
    throw new IllegalStateException();
  }
  if (context.isWeakValues()) {
    builder.weakValues();
  } else if (context.isSoftValues()) {
    builder.softValues();
  }
  if (context.cacheExecutor != CacheExecutor.DEFAULT) {
    builder.executor(context.executor);
  }
  if (context.cacheScheduler != CacheScheduler.DEFAULT) {
    builder.scheduler(context.scheduler);
  }
  if (context.removalListenerType != Listener.DEFAULT) {
    builder.removalListener(context.removalListener);
  }
  if (context.isStrongKeys() && !context.isAsync()) {
    builder.writer(context.cacheWriter());
  }
  if (context.isAsync()) {
    if (context.loader == null) {
      context.asyncCache = builder.buildAsync();
    } else {
      context.asyncCache = builder.buildAsync(
          context.isAsyncLoading ? context.loader.async() : context.loader);
    }
    context.cache = context.asyncCache.synchronous();
  } else if (context.loader == null) {
    context.cache = builder.build();
  } else {
    context.cache = builder.build(context.loader);
  }

  @SuppressWarnings("unchecked")
  Cache<K, V> castedCache = (Cache<K, V>) context.cache;
  RandomSeedEnforcer.resetThreadLocalRandom();
  return castedCache;
}
 
Example 20
Source File: CaffeinatedGuava.java    From caffeine with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a Caffeine cache wrapped in a Guava {@link Cache} facade.
 *
 * @param builder the configured cache builder
 * @return a cache exposed under the Guava APIs
 */
@NonNull
public static <K, V, K1 extends K, V1 extends V> Cache<K1, V1> build(
    @NonNull Caffeine<K, V> builder) {
  return new CaffeinatedGuavaCache<>(builder.build());
}