Java Code Examples for com.google.common.cache.CacheBuilder#newBuilder()

The following examples show how to use com.google.common.cache.CacheBuilder#newBuilder() . 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: LoadingCacheHelper.java    From spring-microservice-exam with MIT License 6 votes vote down vote up
/**
 * 初始化loadingCache
 * @param clazz clazz
 * @param refreshAfterWriteDuration 单位秒
 * @param expireAfterAccessDuration 单位秒
 * @param <K> key
 * @param <V> value
 * @param cacheSize cacheSize
 * @return LoadingCache
 */
private <K, V> LoadingCache<K, V> initLoadingCache(Class<? extends CacheLoader<K, V>> clazz,
		long refreshAfterWriteDuration, long expireAfterAccessDuration, long cacheSize) {
	try {
		log.info("Instantiating LoadingCache: {}", clazz);
		CacheLoader<K, V> cacheLoader = clazz.newInstance();
		CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
		builder.concurrencyLevel(1);
		if (expireAfterAccessDuration > 0) {
			// 在给定时间内没有被读/写访问,则回收
			builder.expireAfterAccess(expireAfterAccessDuration, TimeUnit.SECONDS);
		} else {
			// 自动刷新
			builder.refreshAfterWrite(refreshAfterWriteDuration, TimeUnit.SECONDS);
		}
		if (cacheSize > 0)
			builder.maximumSize(cacheSize);
		LoadingCache<K, V> cache = builder.build(cacheLoader);
		this.loadingCacheMap.put(clazz.getSimpleName(), cache);
		return cache;
	} catch (Exception e) {
		log.error("Error Instantiating LoadingCache: " + clazz, e);
		throw new CommonException(e, "Error Instantiating LoadingCache: " + clazz);
	}
}
 
Example 2
Source File: CachingTableProvider.java    From samza with Apache License 2.0 6 votes vote down vote up
private ReadWriteTable createDefaultCacheTable(String tableId, JavaTableConfig tableConfig) {
  long readTtlMs = Long.parseLong(tableConfig.getForTable(tableId, CachingTableDescriptor.READ_TTL_MS, "-1"));
  long writeTtlMs = Long.parseLong(tableConfig.getForTable(tableId, CachingTableDescriptor.WRITE_TTL_MS, "-1"));
  long cacheSize = Long.parseLong(tableConfig.getForTable(tableId, CachingTableDescriptor.CACHE_SIZE, "-1"));

  CacheBuilder cacheBuilder = CacheBuilder.newBuilder();
  if (readTtlMs != -1) {
    cacheBuilder.expireAfterAccess(readTtlMs, TimeUnit.MILLISECONDS);
  }
  if (writeTtlMs != -1) {
    cacheBuilder.expireAfterWrite(writeTtlMs, TimeUnit.MILLISECONDS);
  }
  if (cacheSize != -1) {
    cacheBuilder.maximumSize(cacheSize);
  }

  logger.info(String.format("Creating default cache with: readTtl=%d, writeTtl=%d, maxSize=%d",
      readTtlMs, writeTtlMs, cacheSize));

  GuavaCacheTable cacheTable = new GuavaCacheTable(tableId + "-def-cache", cacheBuilder.build());
  cacheTable.init(this.context);

  return cacheTable;
}
 
Example 3
Source File: GuavaCacheFactoryBean.java    From spring-cache-demo with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void afterPropertiesSet() {
    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
    if (maximumSize != null) {
      builder.maximumSize(maximumSize);
    }
    if (expireAfterAccessInSeconds != null) {
      builder.expireAfterAccess(expireAfterAccessInSeconds, TimeUnit.SECONDS);
    }
    if (expireAfterWriteInSeconds != null) {
      builder.expireAfterWrite(expireAfterWriteInSeconds, TimeUnit.SECONDS);
    }
    
    com.google.common.cache.Cache<Object, Object> guavaCache= builder.build();
    this.cache = new GuavaCache(this.name, guavaCache, this.allowNullValues);
}
 
Example 4
Source File: GuavaCache.java    From Mybatis-PageHelper with MIT License 6 votes vote down vote up
public GuavaCache(Properties properties, String prefix) {
    CacheBuilder cacheBuilder = CacheBuilder.newBuilder();
    String maximumSize = properties.getProperty(prefix + ".maximumSize");
    if (StringUtil.isNotEmpty(maximumSize)) {
        cacheBuilder.maximumSize(Long.parseLong(maximumSize));
    } else {
        cacheBuilder.maximumSize(1000);
    }
    String expireAfterAccess = properties.getProperty(prefix + ".expireAfterAccess");
    if (StringUtil.isNotEmpty(expireAfterAccess)) {
        cacheBuilder.expireAfterAccess(Long.parseLong(expireAfterAccess), TimeUnit.MILLISECONDS);
    }
    String expireAfterWrite = properties.getProperty(prefix + ".expireAfterWrite");
    if (StringUtil.isNotEmpty(expireAfterWrite)) {
        cacheBuilder.expireAfterWrite(Long.parseLong(expireAfterWrite), TimeUnit.MILLISECONDS);
    }
    String initialCapacity = properties.getProperty(prefix + ".initialCapacity");
    if (StringUtil.isNotEmpty(initialCapacity)) {
        cacheBuilder.initialCapacity(Integer.parseInt(initialCapacity));
    }
    CACHE = cacheBuilder.build();
}
 
Example 5
Source File: ConfigService.java    From nifi-minifi with Apache License 2.0 6 votes vote down vote up
public ConfigService(List<ConfigurationProvider> configurationProviders, Authorizer authorizer, long maximumCacheSize, long cacheTtlMillis) {
    this.authorizer = authorizer;
    this.objectMapper = new ObjectMapper();
    if (configurationProviders == null || configurationProviders.size() == 0) {
        throw new IllegalArgumentException("Expected at least one configuration provider");
    }
    this.configurationProviderInfo = Suppliers.memoizeWithExpiration(() -> initContentTypeInfo(configurationProviders), cacheTtlMillis, TimeUnit.MILLISECONDS);
    CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
    if (maximumCacheSize >= 0) {
        cacheBuilder = cacheBuilder.maximumSize(maximumCacheSize);
    }
    if (cacheTtlMillis >= 0) {
        cacheBuilder = cacheBuilder.refreshAfterWrite(cacheTtlMillis, TimeUnit.MILLISECONDS);
    }
    this.configurationCache = cacheBuilder
            .build(new CacheLoader<ConfigurationProviderKey, ConfigurationProviderValue>() {
                @Override
                public ConfigurationProviderValue load(ConfigurationProviderKey key) throws Exception {
                    return initConfigurationProviderValue(key);
                }
            });
}
 
Example 6
Source File: NetflowV9Decoder.java    From datacollector with Apache License 2.0 6 votes vote down vote up
public static Cache<FlowSetTemplateCacheKey, FlowSetTemplate> buildTemplateCache(
    int maxTemplateCacheSize,
    int templateCacheTimeoutMs
) {
  CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
  if (maxTemplateCacheSize > 0) {
    cacheBuilder = cacheBuilder.maximumSize(maxTemplateCacheSize);
  }
  if (templateCacheTimeoutMs > 0) {
    cacheBuilder = cacheBuilder.expireAfterAccess(templateCacheTimeoutMs, TimeUnit.MILLISECONDS);
  }
  if (LOG.isTraceEnabled()) {
    cacheBuilder = cacheBuilder.removalListener((notification) -> LOG.trace(
        "Removing flow set template entry {} for cause: {} ",
        notification.getKey(),
        notification.getCause()
    ));
  }
  return cacheBuilder.build();
}
 
Example 7
Source File: CommonUtil.java    From ChangeSkin with MIT License 6 votes vote down vote up
public static <K, V> ConcurrentMap<K, V> buildCache(int seconds, int maxSize) {
    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();

    if (seconds > 0) {
        builder.expireAfterWrite(seconds, TimeUnit.SECONDS);
    }

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

    return builder.build(new CacheLoader<K, V>() {
        @Override
        public V load(K key) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }).asMap();
}
 
Example 8
Source File: HMSCache.java    From datacollector with Apache License 2.0 6 votes vote down vote up
/**
 * Build instance of {@link HMSCache}
 * @return {@link HMSCache}
 */
@SuppressWarnings("unchecked")
public HMSCache build() throws StageException {
  Utils.checkArgument(
      !cacheTypes.isEmpty(),
      "Invalid HMSCache Configuration, Should support at least one type of cache"
  );

  Map<HMSCacheType, Cache<String, Optional<HMSCacheSupport.HMSCacheInfo>>> cacheMap = new HashMap<>();
  CacheBuilder cacheBuilder = CacheBuilder.newBuilder();

  if (maxCacheSize > 0) {
    cacheBuilder.maximumSize(maxCacheSize);
  }

  for (HMSCacheType type : cacheTypes) {
    cacheMap.put(type, cacheBuilder.build());
  }
  return new HMSCache(cacheMap);
}
 
Example 9
Source File: GuavaConcurrentMapFactory.java    From icure-backend with GNU General Public License v2.0 5 votes vote down vote up
@Override
public <K, V> ConcurrentMap<K, V> getMap(final ConcurrentMapListener<K, V> listener) {
	// Create cache builder
	CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();

	// Set expireAfterWrite
	if (expireDuration != null && expireUnit != null) {
		cacheBuilder = cacheBuilder.expireAfterWrite(expireDuration, expireUnit);
	}

	// Configure listener
	if (listener != null) {
		cacheBuilder.removalListener((RemovalListener<K, V>) notification -> {
			K key = notification.getKey();
			V value = notification.getValue();
			switch (notification.getCause()) {
				case REPLACED:
					listener.entryUpdated(key, value);
					break;
				case EXPLICIT:
					listener.entryRemoved(key, value);
					break;
				case COLLECTED:
				case EXPIRED:
				case SIZE:
					listener.entryEvicted(key, value);
					break;
			}
		});
	}

	// Build cache
	Cache<K, V> cache = cacheBuilder.build();

	return cache.asMap();
}
 
Example 10
Source File: SimpleCache.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
private ConcurrentMap<T, Result> createCache(int maxCacheSize) {
    final CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
    cacheBuilder.concurrencyLevel(64);
    cacheBuilder.initialCapacity(maxCacheSize);
    cacheBuilder.maximumSize(maxCacheSize);
    Cache<T, Result> localCache = cacheBuilder.build();
    ConcurrentMap<T, Result> cache = localCache.asMap();
    return cache;
}
 
Example 11
Source File: LRUCache.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
private LRUCache(final Function<Key, Value> loader, final RemovalListener<Key, Value> listener, final long maximumSize, final long expireDuration) {
    final CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
    if(listener != null) {
        builder.removalListener(new RemovalListener<Key, Value>() {
            @Override
            public void onRemoval(final RemovalNotification<Key, Value> notification) {
                if(log.isDebugEnabled()) {
                    log.debug(String.format("Removed %s from cache with cause %s", notification.getKey(), notification.getCause()));
                }
                listener.onRemoval(notification);
            }
        });
    }
    if(maximumSize > 0) {
        builder.maximumSize(maximumSize);
    }
    if(expireDuration > 0) {
        builder.expireAfterAccess(expireDuration, TimeUnit.MILLISECONDS);
    }
    if(loader != null) {
        delegate = builder.build(new CacheLoader<Key, Value>() {
            @Override
            public Value load(Key key) {
                return loader.apply(key);
            }
        });
    }
    else {
        delegate = builder.build();
    }
}
 
Example 12
Source File: BigQueryTarget.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public List<ConfigIssue> init() {
  List<ConfigIssue> issues = super.init();

  conf.credentials.getCredentialsProvider(getContext(), issues).ifPresent(provider -> {
    if (issues.isEmpty()) {
      try {
        Optional.ofNullable(provider.getCredentials()).ifPresent(c -> bigQuery = BigQueryDelegate.getBigquery(c, conf.credentials.projectId));
      } catch (IOException e) {
        LOG.error(Errors.BIGQUERY_05.getMessage(), e);
        issues.add(getContext().createConfigIssue(
            Groups.CREDENTIALS.name(),
            "conf.credentials.credentialsProvider",
            Errors.BIGQUERY_05
        ));
      }
    }
  });

  dataSetEval = getContext().createELEval("datasetEL");
  tableNameELEval = getContext().createELEval("tableNameEL");
  rowIdELEval = getContext().createELEval("rowIdExpression");

  CacheBuilder tableIdExistsCacheBuilder = CacheBuilder.newBuilder();
  if (conf.maxCacheSize != -1) {
    tableIdExistsCacheBuilder.maximumSize(conf.maxCacheSize);
  }

  tableIdExistsCache = tableIdExistsCacheBuilder.build(new CacheLoader<TableId, Boolean>() {
    @Override
    public Boolean load(TableId key) throws Exception {
      return bigQuery.getTable(key) != null;
    }
  });
  errorRecordHandler = new DefaultErrorRecordHandler(getContext());

  return issues;
}
 
Example 13
Source File: StandAloneLimiter.java    From neural with MIT License 5 votes vote down vote up
@Override
public synchronized boolean refresh(LimiterConfig limiterConfig) throws Exception {
    super.refresh(limiterConfig);

    // rate limiter
    rateLimiter = AdjustableRateLimiter.create(limiterConfig.getMaxPermitRate());

    // request limiter
    CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
    cacheBuilder.expireAfterWrite(limiterConfig.getRequestInterval().toMillis(), TimeUnit.MILLISECONDS);
    cache = cacheBuilder.build();

    // concurrent limiter
    semaphore = new AdjustableSemaphore(limiterConfig.getMaxPermitConcurrent(), true);

    try {
        if (0 < limiterConfig.getMaxPermitConcurrent()) {
            // the refresh semaphore
            semaphore.setMaxPermits(limiterConfig.getMaxPermitConcurrent());
        }
        if (0 < limiterConfig.getRatePermit()) {
            // the refresh rateLimiter
            rateLimiter.setRate(limiterConfig.getRatePermit());
        }

        return true;
    } catch (Exception e) {
        log.error("The refresh local limiter is exception", e);
    }

    return false;
}
 
Example 14
Source File: GuavaCacheFactory.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) {
    CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
    if (config.getExpireAfterWrite() > 0) {
        cacheBuilder.expireAfterWrite(config.getExpireAfterWrite(), TimeUnit.MILLISECONDS);
    }
    cacheBuilder.maximumSize(config.getCapacity() > 0 ? config.getCapacity() : Long.MAX_VALUE);
    com.google.common.cache.Cache<K, CacheObject<V>> cache = cacheBuilder.build();
    return new GuavaCache<>(cache, config);
}
 
Example 15
Source File: GuavaCacheTest.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
    CacheBuilder builder = CacheBuilder.newBuilder();
    builder.concurrencyLevel(8);
    builder.maximumSize(1);
    builder.initialCapacity(1);
    Cache<String, Object> cache = builder.build();

    cache.put("test1", "1");
    logger.debug("{}", cache.size());
    cache.put("test3", "2");
    logger.debug("{}", cache.size());

}
 
Example 16
Source File: VoiceConfig.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
public CacheBuilder cacheBuilder() {
   CacheBuilder builder = CacheBuilder.newBuilder();
   if(cacheConcurrencyLevel >= 0) { builder.concurrencyLevel(cacheConcurrencyLevel); }
   if(cacheExpireAfterAccessMs >= 0) { builder.expireAfterAccess(cacheExpireAfterAccessMs, TimeUnit.MILLISECONDS); }
   if(cacheExpireAfterWriteMs >= 0) { builder.expireAfterWrite(cacheExpireAfterWriteMs, TimeUnit.MILLISECONDS); }
   if(cacheInitialCapacity >= 0) { builder.initialCapacity(cacheInitialCapacity); }
   if(cacheMaximumSize >= 0) { builder.maximumSize(cacheMaximumSize); }
   if(cacheRefreshAfterWriteMs >= 0) { builder.refreshAfterWrite(cacheRefreshAfterWriteMs, TimeUnit.MILLISECONDS); }
   if(cacheRecordStats) { builder.recordStats(); }
   return builder;
}
 
Example 17
Source File: MapCache.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
MapCache(final String name, final CacheConfiguration config) {
  this.name = name;

  CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
  if (config.isStatisticsEnabled()) {
    cacheBuilder.recordStats();
  }
  if (config.isSoftValuesEnabled()) {
    cacheBuilder.softValues();
  }
  if (config.getInitialCapacity() >= 0) {
    cacheBuilder.initialCapacity(config.getInitialCapacity());
  }
  if (config.getMaximumSize() >= 0) {
    if (config.isArraySizeEnabled()) {
      cacheBuilder.maximumWeight(config.getMaximumSize());
      cacheBuilder.weigher(new Weigher<K, V>() {
        @Override
        public int weigh(final K key, final V value) {
          if (value instanceof byte[]) {
            return ((byte[]) value).length;
          }
          throw new IllegalStateException("Using array size is only supported for byte arrays"); //$NON-NLS-1$
        }
      });
    } else {
      cacheBuilder.maximumSize(config.getMaximumSize());
    }
  }

  backend = cacheBuilder.build();
}
 
Example 18
Source File: DispatchableRoutingResolver.java    From cloud-config with MIT License 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
    CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
    if(autoRefresh) {
        cacheBuilder.refreshAfterWrite(refreshInterval, TimeUnit.MINUTES);
    }
    cachedIterators = cacheBuilder.build(getCacheLoader());
}
 
Example 19
Source File: CachingHiveMetastore.java    From presto with Apache License 2.0 5 votes vote down vote up
private static CacheBuilder<Object, Object> newCacheBuilder(OptionalLong expiresAfterWriteMillis, OptionalLong refreshMillis, long maximumSize)
{
    CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
    if (expiresAfterWriteMillis.isPresent()) {
        cacheBuilder = cacheBuilder.expireAfterWrite(expiresAfterWriteMillis.getAsLong(), MILLISECONDS);
    }
    if (refreshMillis.isPresent() && (expiresAfterWriteMillis.isEmpty() || expiresAfterWriteMillis.getAsLong() > refreshMillis.getAsLong())) {
        cacheBuilder = cacheBuilder.refreshAfterWrite(refreshMillis.getAsLong(), MILLISECONDS);
    }
    cacheBuilder = cacheBuilder.maximumSize(maximumSize);
    return cacheBuilder;
}
 
Example 20
Source File: ScriptService.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Inject
public ScriptService(Settings settings, Environment env, Set<ScriptEngineService> scriptEngines,
                     ResourceWatcherService resourceWatcherService, ScriptContextRegistry scriptContextRegistry) throws IOException {
    super(settings);
    this.parseFieldMatcher = new ParseFieldMatcher(settings);
    if (Strings.hasLength(settings.get(DISABLE_DYNAMIC_SCRIPTING_SETTING))) {
        throw new IllegalArgumentException(DISABLE_DYNAMIC_SCRIPTING_SETTING + " is not a supported setting, replace with fine-grained script settings. \n" +
                "Dynamic scripts can be enabled for all languages and all operations by replacing `script.disable_dynamic: false` with `script.inline: on` and `script.indexed: on` in elasticsearch.yml");
    }

    this.scriptEngines = scriptEngines;
    this.scriptContextRegistry = scriptContextRegistry;
    int cacheMaxSize = settings.getAsInt(SCRIPT_CACHE_SIZE_SETTING, SCRIPT_CACHE_SIZE_DEFAULT);
    TimeValue cacheExpire = settings.getAsTime(SCRIPT_CACHE_EXPIRE_SETTING, null);
    logger.debug("using script cache with max_size [{}], expire [{}]", cacheMaxSize, cacheExpire);

    this.defaultLang = settings.get(DEFAULT_SCRIPTING_LANGUAGE_SETTING, DEFAULT_LANG);

    CacheBuilder cacheBuilder = CacheBuilder.newBuilder();
    if (cacheMaxSize >= 0) {
        cacheBuilder.maximumSize(cacheMaxSize);
    }
    if (cacheExpire != null) {
        cacheBuilder.expireAfterAccess(cacheExpire.nanos(), TimeUnit.NANOSECONDS);
    }
    this.cache = cacheBuilder.removalListener(new ScriptCacheRemovalListener()).build();

    ImmutableMap.Builder<String, ScriptEngineService> enginesByLangBuilder = ImmutableMap.builder();
    ImmutableMap.Builder<String, ScriptEngineService> enginesByExtBuilder = ImmutableMap.builder();
    for (ScriptEngineService scriptEngine : scriptEngines) {
        for (String type : scriptEngine.types()) {
            enginesByLangBuilder.put(type, scriptEngine);
        }
        for (String ext : scriptEngine.extensions()) {
            enginesByExtBuilder.put(ext, scriptEngine);
        }
    }
    this.scriptEnginesByLang = enginesByLangBuilder.build();
    this.scriptEnginesByExt = enginesByExtBuilder.build();

    this.scriptModes = new ScriptModes(this.scriptEnginesByLang, scriptContextRegistry, settings);

    // add file watcher for static scripts
    scriptsDirectory = env.scriptsFile();
    if (logger.isTraceEnabled()) {
        logger.trace("Using scripts directory [{}] ", scriptsDirectory);
    }
    FileWatcher fileWatcher = new FileWatcher(scriptsDirectory);
    fileWatcher.addListener(new ScriptChangesListener());

    if (settings.getAsBoolean(SCRIPT_AUTO_RELOAD_ENABLED_SETTING, true)) {
        // automatic reload is enabled - register scripts
        resourceWatcherService.add(fileWatcher);
    } else {
        // automatic reload is disable just load scripts once
        fileWatcher.init();
    }
}