use of com.google.common.cache.Weigher in project jackrabbit-oak by apache.
the class CacheTest method testRefresh.
@Test
public void testRefresh() throws ExecutionException {
CacheLIRS<Integer, String> cache = new CacheLIRS.Builder<Integer, String>().maximumWeight(100).weigher(new Weigher<Integer, String>() {
@Override
public int weigh(Integer key, String value) {
return key + value.length();
}
}).build(new CacheLoader<Integer, String>() {
@Override
public String load(Integer key) throws Exception {
if (key < 0 || key >= 100) {
throw new Exception("Out of range");
}
return "n" + key;
}
@Override
public ListenableFuture<String> reload(Integer key, String oldValue) {
assertTrue(oldValue != null);
SettableFuture<String> f = SettableFuture.create();
f.set(oldValue);
return f;
}
});
assertEquals("n1", cache.get(1));
cache.refresh(1);
cache.refresh(2);
try {
cache.get(-1);
fail();
} catch (Exception e) {
// expected
}
// expected to log a warning, but not fail
cache.refresh(-1);
}
use of com.google.common.cache.Weigher in project bazel by bazelbuild.
the class DexBuilder method runPersistentWorker.
/**
* Implements a persistent worker process for use with Bazel (see {@code WorkerSpawnStrategy}).
*/
private static void runPersistentWorker() throws IOException {
ExecutorService executor = newFixedThreadPool(Runtime.getRuntime().availableProcessors());
Cache<DexingKey, byte[]> dexCache = CacheBuilder.newBuilder().maximumWeight(Math.min(Runtime.getRuntime().maxMemory() - 25 * ONE_MEG, 200 * ONE_MEG)).weigher(new Weigher<DexingKey, byte[]>() {
@Override
public int weigh(DexingKey key, byte[] value) {
return key.classfileContent().length + value.length;
}
}).build();
try {
while (true) {
WorkRequest request = WorkRequest.parseDelimitedFrom(System.in);
if (request == null) {
return;
}
// Redirect dx's output so we can return it in response
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos, /*autoFlush*/
true);
DxConsole.out = DxConsole.err = ps;
// Make sure that we exit nonzero in case uncaught errors occur during processRequest.
int exitCode = 1;
try {
processRequest(executor, dexCache, request.getArgumentsList());
// success!
exitCode = 0;
} catch (Exception e) {
// Deliberate catch-all so we can capture a stack trace.
// TODO(bazel-team): Consider canceling any outstanding futures created for this request
e.printStackTrace(ps);
} catch (Error e) {
e.printStackTrace();
// try capturing the error, may fail if out of memory
e.printStackTrace(ps);
// rethrow to kill the worker
throw e;
} finally {
// Try sending a response no matter what
String output;
try {
output = baos.toString();
} catch (Throwable t) {
// most likely out of memory, so log with minimal memory needs
t.printStackTrace();
output = "check worker log for exceptions";
}
WorkResponse.newBuilder().setOutput(output).setExitCode(exitCode).build().writeDelimitedTo(System.out);
System.out.flush();
}
}
} finally {
executor.shutdown();
}
}
use of com.google.common.cache.Weigher in project gerrit by GerritCodeReview.
the class CacheModule method bindWeigher.
<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);
}
Aggregations