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

The following examples show how to use com.github.benmanes.caffeine.cache.Caffeine#recordStats() . 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 ditto with Eclipse Public License 2.0 6 votes vote down vote up
private CaffeineCache(final Caffeine<? super K, ? super V> caffeine,
        final AsyncCacheLoader<K, V> loader,
        @Nullable final String cacheName) {

    if (cacheName != null) {
        this.metricStatsCounter =
                MetricsStatsCounter.of(cacheName, this::getMaxCacheSize, this::getCurrentCacheSize);
        caffeine.recordStats(() -> metricStatsCounter);
        this.asyncLoadingCache = caffeine.buildAsync(loader);
        this.synchronousCacheView = asyncLoadingCache.synchronous();
    } else {
        this.asyncLoadingCache = caffeine.buildAsync(loader);
        this.synchronousCacheView = asyncLoadingCache.synchronous();
        this.metricStatsCounter = null;
    }
}
 
Example 2
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 3
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 4
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 5
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 6
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 7
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 8
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;
}