Java Code Examples for com.google.common.cache.CacheBuilder#initialCapacity()
The following examples show how to use
com.google.common.cache.CacheBuilder#initialCapacity() .
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: GuavaCache.java From Mybatis-PageHelper with MIT License | 6 votes |
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 2
Source File: VoiceConfig.java From arcusplatform with Apache License 2.0 | 5 votes |
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 3
Source File: MapCache.java From dsl-devkit with Eclipse Public License 1.0 | 5 votes |
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 4
Source File: DefaultActiveTraceRepository.java From pinpoint with Apache License 2.0 | 5 votes |
private ConcurrentMap<ActiveTraceHandle, ActiveTrace> createCache(int maxActiveTraceSize) { final CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder(); cacheBuilder.concurrencyLevel(64); cacheBuilder.initialCapacity(maxActiveTraceSize); cacheBuilder.maximumSize(maxActiveTraceSize); final Cache<ActiveTraceHandle, ActiveTrace> localCache = cacheBuilder.build(); return localCache.asMap(); }
Example 5
Source File: SimpleCache.java From pinpoint with Apache License 2.0 | 5 votes |
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 6
Source File: LRUCache.java From pinpoint with Apache License 2.0 | 5 votes |
public LRUCache(int maxCacheSize) { final CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder(); cacheBuilder.concurrencyLevel(32); cacheBuilder.initialCapacity(maxCacheSize); cacheBuilder.maximumSize(maxCacheSize); Cache<T, Object> localCache = cacheBuilder.build(); this.cache = localCache.asMap(); }
Example 7
Source File: GuavaCacheTest.java From pinpoint with Apache License 2.0 | 5 votes |
@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 8
Source File: CachingSubsystemRegistry.java From arcusplatform with Apache License 2.0 | 4 votes |
/** * */ @Inject public CachingSubsystemRegistry( SubsystemFactory factory, PlaceDAO placeDao, ModelDao modelDao, PlacePopulationCacheManager populationCacheMgr, SubsystemConfig config ) { this.factory = factory; this.placeDao = placeDao; this.modelDao = modelDao; this.populationCacheMgr = populationCacheMgr; CacheBuilder<Object,Object> bld = CacheBuilder.newBuilder(); if (config.getPlaceCacheConcurrencyLevel() > 0) { bld = bld.concurrencyLevel(config.getPlaceCacheConcurrencyLevel()); } if (config.getPlaceCacheExpireAccessMs() > 0) { bld =bld.expireAfterAccess(config.getPlaceCacheExpireAccessMs(), TimeUnit.MILLISECONDS); } if (config.getPlaceCacheInitialCapacity() > 0) { bld = bld.initialCapacity(config.getPlaceCacheInitialCapacity()); } if (config.getPlaceCacheMaxSize() > 0) { bld = bld.maximumSize(config.getPlaceCacheMaxSize()); } if (config.isPlaceCacheSoftValues()) { bld = bld.softValues(); } this.executors = bld .recordStats() .<UUID, SubsystemExecutor>removalListener((event) -> onRemoved(event.getValue())) .build(); IrisMetricSet metrics = IrisMetrics.metrics("subsystems"); metrics.monitor("cache", executors); }
Example 9
Source File: IrisSettings.java From arcusplatform with Apache License 2.0 | 4 votes |
public static CacheBuilder<Object,Object> configurableCacheBuilder(String base, CacheBuilder<Object,Object> builder) { // If there is a full cache specification then that overrides everything String spec = IrisSettings.getStringProperty(base + ".spec", ""); if (!spec.isEmpty()) { return CacheBuilder.from(spec); } CacheBuilder<Object,Object> bld = builder; int concurrency = IrisSettings.getIntegerProperty(base + ".concurrency", -1); if (concurrency > 0) { bld = bld.concurrencyLevel(concurrency); } long write = IrisSettings.getLongProperty(base + ".expire.write", -1L); if (write > 0) { bld = bld.expireAfterWrite(write, TimeUnit.MILLISECONDS); } long access = IrisSettings.getLongProperty(base + ".expire.access", -1L); if (access > 0) { bld = bld.expireAfterAccess(access, TimeUnit.MILLISECONDS); } long refresh = IrisSettings.getLongProperty(base + ".refresh.write", -1L); if (refresh > 0) { bld = bld.refreshAfterWrite(refresh, TimeUnit.MILLISECONDS); } int initsz = IrisSettings.getIntegerProperty(base + ".initial.capacity", -1); if (initsz > 0) { bld = bld.initialCapacity(initsz); } int maxsz = IrisSettings.getIntegerProperty(base + ".max.size", -1); if (maxsz > 0) { bld = bld.maximumSize(maxsz); } boolean soft = IrisSettings.getBooleanProperty(base + ".soft.values", false); if (soft) { bld = bld.softValues(); } return bld; }
Example 10
Source File: GuavaUtils.java From t-io with Apache License 2.0 | 4 votes |
/** * * @param concurrencyLevel * @param timeToLiveSeconds 设置写缓存后过期时间(单位:秒) * @param timeToIdleSeconds 设置读缓存后过期时间(单位:秒) * @param initialCapacity * @param maximumSize * @param recordStats * @param removalListener * @return */ public static <K, V> LoadingCache<K, V> createLoadingCache(Integer concurrencyLevel, Long timeToLiveSeconds, Long timeToIdleSeconds, Integer initialCapacity, Integer maximumSize, boolean recordStats, RemovalListener<K, V> removalListener) { if (removalListener == null) { removalListener = new RemovalListener<K, V>() { @Override public void onRemoval(RemovalNotification<K, V> notification) { log.info(notification.getKey() + " was removed"); } }; } CacheBuilder<K, V> cacheBuilder = CacheBuilder.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()); }