本文整理匯總了Java中com.google.common.cache.Weigher類的典型用法代碼示例。如果您正苦於以下問題:Java Weigher類的具體用法?Java Weigher怎麽用?Java Weigher使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Weigher類屬於com.google.common.cache包,在下文中一共展示了Weigher類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: initCache
import com.google.common.cache.Weigher; //導入依賴的package包/類
private static void initCache() {
logger.info("(re-)creating tile cache");
if (doCacheLock) OrbitTiledImage2.cacheLock.writeLock().lock();
try {
long mem = Runtime.getRuntime().maxMemory();
tileCache = CacheBuilder.
newBuilder().
//recordStats().
expireAfterWrite(7, TimeUnit.MINUTES).
maximumWeight(mem/2).
weigher(new Weigher<PointAndName, Raster>() {
@Override
public int weigh(PointAndName key, Raster raster) {
return raster.getWidth()*raster.getHeight() * 3 * 4;
}
}).
build();
} finally {
if (doCacheLock) OrbitTiledImage2.cacheLock.writeLock().unlock();
}
}
示例2: CacheLIRS
import com.google.common.cache.Weigher; //導入依賴的package包/類
/**
* Create a new cache with the given memory size.
*
* @param maxMemory the maximum memory to use (1 or larger)
* @param averageMemory the average memory (1 or larger)
* @param segmentCount the number of cache segments (must be a power of 2)
* @param stackMoveDistance how many other item are to be moved to the top
* of the stack before the current item is moved
*/
@SuppressWarnings("unchecked")
CacheLIRS(Weigher<K, V> weigher, long maxMemory, int averageMemory,
int segmentCount, int stackMoveDistance, final CacheLoader<K, V> loader) {
this.weigher = weigher;
setMaxMemory(maxMemory);
setAverageMemory(averageMemory);
if (Integer.bitCount(segmentCount) != 1) {
throw new IllegalArgumentException("The segment count must be a power of 2, is " + segmentCount);
}
this.segmentCount = segmentCount;
this.segmentMask = segmentCount - 1;
this.stackMoveDistance = stackMoveDistance;
segments = new Segment[segmentCount];
invalidateAll();
this.segmentShift = Integer.numberOfTrailingZeros(segments[0].entries.length);
this.loader = loader;
}
示例3: ConfigFileController
import com.google.common.cache.Weigher; //導入依賴的package包/類
public ConfigFileController() {
localCache = CacheBuilder.newBuilder()
.expireAfterWrite(EXPIRE_AFTER_WRITE, TimeUnit.MINUTES)
.weigher(new Weigher<String, String>() {
@Override
public int weigh(String key, String value) {
return value == null ? 0 : value.length();
}
})
.maximumWeight(MAX_CACHE_SIZE)
.removalListener(new RemovalListener<String, String>() {
@Override
public void onRemoval(RemovalNotification<String, String> notification) {
String cacheKey = notification.getKey();
logger.debug("removing cache key: {}", cacheKey);
if (!cacheKey2WatchedKeys.containsKey(cacheKey)) {
return;
}
//create a new list to avoid ConcurrentModificationException
List<String> watchedKeys = new ArrayList<>(cacheKey2WatchedKeys.get(cacheKey));
for (String watchedKey : watchedKeys) {
watchedKeys2CacheKey.remove(watchedKey, cacheKey);
}
cacheKey2WatchedKeys.removeAll(cacheKey);
logger.debug("removed cache key: {}", cacheKey);
}
})
.build();
propertiesResponseHeaders = new HttpHeaders();
propertiesResponseHeaders.add("Content-Type", "text/plain;charset=UTF-8");
jsonResponseHeaders = new HttpHeaders();
jsonResponseHeaders.add("Content-Type", "application/json;charset=UTF-8");
NOT_FOUND_RESPONSE = new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
示例4: HttpStorageCaching
import com.google.common.cache.Weigher; //導入依賴的package包/類
public HttpStorageCaching() {
threadpool = Executors.newFixedThreadPool(100);
asyncHttp = Async.newInstance().use(threadpool);
cacheWeighter = new Weigher<String, byte[]>() {
@Override
public int weigh(String key, byte[] value) {
return value.length;
}
};
cache = CacheBuilder.newBuilder()
.weigher(cacheWeighter)
.maximumWeight(CACHE_WEIGHT)
.build();
}
示例5: MapCache
import com.google.common.cache.Weigher; //導入依賴的package包/類
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.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();
}
示例6: ExpirationKCVSCache
import com.google.common.cache.Weigher; //導入依賴的package包/類
public ExpirationKCVSCache(final KeyColumnValueStore store, String metricsName, final long cacheTimeMS, final long invalidationGracePeriodMS, final long maximumByteSize) {
super(store, metricsName);
Preconditions.checkArgument(cacheTimeMS > 0, "Cache expiration must be positive: %s", cacheTimeMS);
Preconditions.checkArgument(System.currentTimeMillis()+1000l*3600*24*365*100+cacheTimeMS>0,"Cache expiration time too large, overflow may occur: %s",cacheTimeMS);
this.cacheTimeMS = cacheTimeMS;
int concurrencyLevel = Runtime.getRuntime().availableProcessors();
Preconditions.checkArgument(invalidationGracePeriodMS >=0,"Invalid expiration grace peiod: %s", invalidationGracePeriodMS);
this.invalidationGracePeriodMS = invalidationGracePeriodMS;
CacheBuilder<KeySliceQuery,EntryList> cachebuilder = CacheBuilder.newBuilder()
.maximumWeight(maximumByteSize)
.concurrencyLevel(concurrencyLevel)
.initialCapacity(1000)
.expireAfterWrite(cacheTimeMS, TimeUnit.MILLISECONDS)
.weigher(new Weigher<KeySliceQuery, EntryList>() {
@Override
public int weigh(KeySliceQuery keySliceQuery, EntryList entries) {
return GUAVA_CACHE_ENTRY_SIZE + KEY_QUERY_SIZE + entries.getByteSize();
}
});
cache = cachebuilder.build();
expiredKeys = new ConcurrentHashMap<StaticBuffer, Long>(50,0.75f,concurrencyLevel);
penaltyCountdown = new CountDownLatch(PENALTY_THRESHOLD);
cleanupThread = new CleanupThread();
cleanupThread.start();
}
示例7: create
import com.google.common.cache.Weigher; //導入依賴的package包/類
@SuppressWarnings("unchecked")
<K, V> CacheBuilder<K, V> create(CacheBinding<K, V> def, boolean unwrapValueHolder) {
CacheBuilder<K, V> builder = newCacheBuilder();
builder.recordStats();
builder.maximumWeight(cfg.getLong("cache", def.name(), "memoryLimit", def.maximumWeight()));
builder = builder.removalListener(forwardingRemovalListenerFactory.create(def.name()));
Weigher<K, V> weigher = def.weigher();
if (weigher != null && unwrapValueHolder) {
final Weigher<K, V> impl = weigher;
weigher =
(Weigher<K, V>)
new Weigher<K, ValueHolder<V>>() {
@Override
public int weigh(K key, ValueHolder<V> value) {
return impl.weigh(key, value.value);
}
};
} else if (weigher == null) {
weigher = unitWeight();
}
builder.weigher(weigher);
Long age = def.expireAfterWrite(TimeUnit.SECONDS);
if (has(def.name(), "maxAge")) {
builder.expireAfterWrite(
ConfigUtil.getTimeUnit(
cfg, "cache", def.name(), "maxAge", age != null ? age : 0, TimeUnit.SECONDS),
TimeUnit.SECONDS);
} else if (age != null) {
builder.expireAfterWrite(age, TimeUnit.SECONDS);
}
return builder;
}
示例8: unitWeight
import com.google.common.cache.Weigher; //導入依賴的package包/類
private static <K, V> Weigher<K, V> unitWeight() {
return new Weigher<K, V>() {
@Override
public int weigh(K key, V value) {
return 1;
}
};
}
示例9: bindWeigher
import com.google.common.cache.Weigher; //導入依賴的package包/類
<K, V> Provider<Weigher<K, V>> bindWeigher(
CacheProvider<K, V> m, Class<? extends Weigher<K, V>> impl) {
Type weigherType =
Types.newParameterizedType(Weigher.class, m.keyType().getType(), m.valueType().getType());
@SuppressWarnings("unchecked")
Key<Weigher<K, V>> key = (Key<Weigher<K, V>>) Key.get(weigherType, Names.named(m.name));
bind(key).to(impl).in(Scopes.SINGLETON);
return getProvider(key);
}
示例10: createQueryContext
import com.google.common.cache.Weigher; //導入依賴的package包/類
private QueryContext createQueryContext(Session session) {
QueryContext newQueryContext = new QueryContext(context.getConf(), session);
// Set default space uri and its root uri
newQueryContext.setDefaultSpaceUri(TablespaceManager.getDefault().getUri());
newQueryContext.setDefaultSpaceRootUri(TablespaceManager.getDefault().getRootUri());
if (TajoConstants.IS_TEST_MODE) {
newQueryContext.putAll(CommonTestingUtil.getSessionVarsForTest());
}
// Set queryCache in session
int queryCacheSize = context.getConf().getIntVar(TajoConf.ConfVars.QUERY_SESSION_QUERY_CACHE_SIZE);
if (queryCacheSize > 0 && session.getQueryCache() == null) {
Weigher<String, Expr> weighByLength = new Weigher<String, Expr>() {
public int weigh(String key, Expr expr) {
return key.length();
}
};
LoadingCache<String, Expr> cache = CacheBuilder.newBuilder()
.maximumWeight(queryCacheSize * 1024)
.weigher(weighByLength)
.expireAfterAccess(1, TimeUnit.HOURS)
.build(new CacheLoader<String, Expr>() {
public Expr load(String sql) throws SQLSyntaxError {
return analyzer.parse(sql);
}
});
session.setQueryCache(cache);
}
return newQueryContext;
}
示例11: CacheStats
import com.google.common.cache.Weigher; //導入依賴的package包/類
@SuppressWarnings("unchecked")
public CacheStats(Cache<?, ?> cache, String name,
Weigher<?, ?> weigher, long maxWeight) {
this.cache = (Cache<Object, Object>) cache;
this.name = name;
this.weigher = (Weigher<Object, Object>) weigher;
this.maxWeight = maxWeight;
}
示例12: DataStoreBlobStore
import com.google.common.cache.Weigher; //導入依賴的package包/類
public DataStoreBlobStore(DataStore delegate, boolean encodeLengthInId, int cacheSizeInMB) {
this.delegate = delegate;
this.encodeLengthInId = encodeLengthInId;
this.cache = CacheLIRS.newBuilder()
.maximumWeight(cacheSizeInMB * FileUtils.ONE_MB)
.weigher(new Weigher<String, byte[]>() {
@Override
public int weigh(String key, byte[] value) {
return value.length;
}
})
.build();
}
示例13: CachingBlobStore
import com.google.common.cache.Weigher; //導入依賴的package包/類
public CachingBlobStore(long cacheSize) {
this.blobCacheSize = cacheSize;
cache = CacheLIRS.newBuilder().
maximumWeight(cacheSize).
averageWeight(getBlockSize() / 2).
weigher(new Weigher<String, byte[]>() {
@Override
public int weigh(String key, byte[] value) {
return value.length;
}
}).build();
}
示例14: initializeQueueMappingCache
import com.google.common.cache.Weigher; //導入依賴的package包/類
/**
* Method to initialize the queue mapping cache.
* <p>
* The queue mapping cache is what holds the queue mappings(queue name to queue id) in memory. The cache is
* initialized with a loader so that a queue name which is not present in the cache is loaded from the database
* upon a query for a queue name.
*/
private void initializeQueueMappingCache() {
// The size of the queue mappings cache in MegaBytes
final int QUEUE_CACHE_SIZE = 2;
// Expected concurrency for the cache (4 is guava default)
final int QUEUE_CACHE_CONCURRENCY_LEVEL = 4;
queueMappings = CacheBuilder.newBuilder().concurrencyLevel(QUEUE_CACHE_CONCURRENCY_LEVEL)
.maximumWeight(QUEUE_CACHE_SIZE * 1024 * 1024).weigher(new Weigher<String, Integer>() {
@Override
public int weigh(String s, Integer integer) {
return s.length();
}
}).build(new CacheLoader<String, Integer>() {
public Integer load(String queueName) throws AndesException {
try {
Integer queueID = getQueueID(queueName);
if (log.isDebugEnabled()) {
log.debug("Loaded queue: " + queueName + " to the cache from database");
}
return queueID;
} catch (SQLException e) {
throw new AndesException("Error retrieving queue id for queue: " + queueName, e);
}
}
});
}
示例15: newBuilder
import com.google.common.cache.Weigher; //導入依賴的package包/類
/**
* Create cache with easy setup.
*
* @param config setup
* @param weigher Guava weighter
* @return cache
*/
public static CacheBuilder<Object, Object> newBuilder(final HesperidesCacheParameter config,
final Weigher<? extends Object, ? extends Object> weigher) {
final CacheBuilder<Object, Object> cache = CacheBuilder.newBuilder();
if (config != null) {
final int maxSize = config.getMaxSize();
final int weight = config.getWeight();
final String expire = config.getItemExpireAfter();
if (maxSize != HesperidesCacheParameter.NOT_SET) {
cache.maximumSize(maxSize);
}
if (weight != HesperidesCacheParameter.NOT_SET) {
if (weigher == null) {
throw new IllegalArgumentException("Parameter 'weight' is not supported for this cache.");
}
cache.maximumWeight(weight);
}
if (expire != null) {
final Pattern p = Pattern.compile("^([0-9]+)(m|s|h|d)");
final Matcher m = p.matcher(expire);
if (m.find()) {
final int time = Integer.valueOf(m.group(1));
TimeUnit unit = TimeUnit.SECONDS;
switch (m.group(2)) {
case "m":
unit = TimeUnit.MINUTES;
break;
case "h":
unit = TimeUnit.HOURS;
break;
case "d":
unit = TimeUnit.DAYS;
break;
default:
// Nothing
}
cache.expireAfterWrite(time, unit);
cache.expireAfterAccess(time, unit);
} else {
throw new IllegalArgumentException("Parameter 'itemExpireAfter' is not valid. Valid usage is [0-9]+(m|h|d|s). (Where 'm' is minutes, 'h' is hours, 'd' is days, 's' seconds.");
}
}
}
return cache;
}