use of com.google.common.cache.Cache in project gerrit by GerritCodeReview.
the class CachesCollection method parse.
@Override
public CacheResource parse(ConfigResource parent, IdString id) throws AuthException, ResourceNotFoundException, PermissionBackendException {
permissionBackend.user(self).check(GlobalPermission.VIEW_CACHES);
String cacheName = id.get();
String pluginName = "gerrit";
int i = cacheName.lastIndexOf('-');
if (i != -1) {
pluginName = cacheName.substring(0, i);
cacheName = cacheName.length() > i + 1 ? cacheName.substring(i + 1) : "";
}
Provider<Cache<?, ?>> cacheProvider = cacheMap.byPlugin(pluginName).get(cacheName);
if (cacheProvider == null) {
throw new ResourceNotFoundException(id);
}
return new CacheResource(pluginName, cacheName, cacheProvider);
}
use of com.google.common.cache.Cache in project gerrit by GerritCodeReview.
the class NoteDbModule method configure.
@Override
public void configure() {
factory(ChangeUpdate.Factory.class);
factory(ChangeDraftUpdate.Factory.class);
factory(DeleteCommentRewriter.Factory.class);
factory(DraftCommentNotes.Factory.class);
factory(RobotCommentUpdate.Factory.class);
factory(RobotCommentNotes.Factory.class);
factory(NoteDbUpdateManager.Factory.class);
if (!useTestBindings) {
install(ChangeNotesCache.module());
if (cfg.getBoolean("noteDb", null, "testRebuilderWrapper", false)) {
// Yes, another variety of test bindings with a different way of
// configuring it.
bind(ChangeRebuilder.class).to(TestChangeRebuilderWrapper.class);
} else {
bind(ChangeRebuilder.class).to(ChangeRebuilderImpl.class);
}
} else {
bind(ChangeRebuilder.class).toInstance(new ChangeRebuilder(null) {
@Override
public Result rebuild(ReviewDb db, Change.Id changeId) {
return null;
}
@Override
public Result rebuildEvenIfReadOnly(ReviewDb db, Id changeId) {
return null;
}
@Override
public Result rebuild(NoteDbUpdateManager manager, ChangeBundle bundle) {
return null;
}
@Override
public NoteDbUpdateManager stage(ReviewDb db, Change.Id changeId) {
return null;
}
@Override
public Result execute(ReviewDb db, Change.Id changeId, NoteDbUpdateManager manager) {
return null;
}
@Override
public void buildUpdates(NoteDbUpdateManager manager, ChangeBundle bundle) {
// Do nothing.
}
@Override
public void rebuildReviewDb(ReviewDb db, Project.NameKey project, Id changeId) {
// Do nothing.
}
});
bind(new TypeLiteral<Cache<ChangeNotesCache.Key, ChangeNotesState>>() {
}).annotatedWith(Names.named(ChangeNotesCache.CACHE_NAME)).toInstance(CacheBuilder.newBuilder().<ChangeNotesCache.Key, ChangeNotesState>build());
}
}
use of com.google.common.cache.Cache in project bookkeeper by apache.
the class TestFileInfoBackingCache method testForDeadlocks.
/**
* Of course this can't prove they don't exist, but
* try to shake them out none the less.
*/
@Test
public void testForDeadlocks() throws Exception {
int numRunners = 20;
int maxLedgerId = 10;
AtomicBoolean done = new AtomicBoolean(false);
FileInfoBackingCache cache = new FileInfoBackingCache((ledgerId, createIfNotFound) -> {
File f = new File(baseDir, String.valueOf(ledgerId));
f.deleteOnExit();
return f;
});
Iterable<Future<Set<CachedFileInfo>>> futures = IntStream.range(0, numRunners).mapToObj((i) -> {
Callable<Set<CachedFileInfo>> c = () -> {
Random r = new Random();
List<CachedFileInfo> fileInfos = new ArrayList<>();
Set<CachedFileInfo> allFileInfos = new HashSet<>();
while (!done.get()) {
if (r.nextBoolean() && fileInfos.size() < 5) {
// take a reference
CachedFileInfo fi = cache.loadFileInfo(r.nextInt(maxLedgerId), masterKey);
Assert.assertFalse(fi.isClosed());
allFileInfos.add(fi);
fileInfos.add(fi);
} else {
// release a reference
Collections.shuffle(fileInfos);
if (!fileInfos.isEmpty()) {
fileInfos.remove(0).release();
}
}
}
for (CachedFileInfo fi : fileInfos) {
Assert.assertFalse(fi.isClosed());
fi.release();
}
return allFileInfos;
};
return executor.submit(c);
}).collect(Collectors.toList());
Thread.sleep(TimeUnit.SECONDS.toMillis(10));
done.set(true);
// ensure all threads are finished operating on cache, before checking any
for (Future<Set<CachedFileInfo>> f : futures) {
f.get();
}
for (Future<Set<CachedFileInfo>> f : futures) {
for (CachedFileInfo fi : f.get()) {
Assert.assertTrue(fi.isClosed());
Assert.assertEquals(FileInfoBackingCache.DEAD_REF, fi.getRefCount());
}
}
// They should be loaded fresh (i.e. this load should be only reference)
for (int i = 0; i < maxLedgerId; i++) {
Assert.assertEquals(1, cache.loadFileInfo(i, masterKey).getRefCount());
}
}
use of com.google.common.cache.Cache in project bookkeeper by apache.
the class TestFileInfoBackingCache method testRaceGuavaEvictAndReleaseBeforeRetain.
@Test
public void testRaceGuavaEvictAndReleaseBeforeRetain() throws Exception {
AtomicBoolean done = new AtomicBoolean(false);
FileInfoBackingCache cache = new FileInfoBackingCache((ledgerId, createIfNotFound) -> {
File f = new File(baseDir, String.valueOf(ledgerId));
f.deleteOnExit();
return f;
});
Cache<Long, CachedFileInfo> guavaCache = CacheBuilder.newBuilder().maximumSize(1).removalListener(this::guavaEvictionListener).build();
Iterable<Future<Set<CachedFileInfo>>> futures = LongStream.range(0L, 2L).mapToObj((i) -> {
Callable<Set<CachedFileInfo>> c = () -> {
Set<CachedFileInfo> allFileInfos = new HashSet<>();
while (!done.get()) {
CachedFileInfo fi = null;
do {
fi = guavaCache.get(i, () -> cache.loadFileInfo(i, masterKey));
allFileInfos.add(fi);
Thread.sleep(100);
} while (!fi.tryRetain());
Assert.assertFalse(fi.isClosed());
fi.release();
}
return allFileInfos;
};
return executor.submit(c);
}).collect(Collectors.toList());
Thread.sleep(TimeUnit.SECONDS.toMillis(10));
done.set(true);
// ensure all threads are finished operating on cache, before checking any
for (Future<Set<CachedFileInfo>> f : futures) {
f.get();
}
guavaCache.invalidateAll();
for (Future<Set<CachedFileInfo>> f : futures) {
for (CachedFileInfo fi : f.get()) {
Assert.assertTrue(fi.isClosed());
Assert.assertEquals(FileInfoBackingCache.DEAD_REF, fi.getRefCount());
}
}
}
use of com.google.common.cache.Cache in project micrometer by micrometer-metrics.
the class CacheSample method main.
public static void main(String[] args) {
MeterRegistry registry = SampleConfig.myMonitoringSystem();
GuavaCacheMetrics.monitor(registry, guavaCache, "book.guava");
// read all of Frankenstein
HttpClient.create("www.gutenberg.org").get("/cache/epub/84/pg84.txt").flatMapMany(res -> res.addHandler(wordDecoder()).receive().asString()).delayElements(// one word per 10 ms
Duration.ofMillis(10)).filter(word -> !word.isEmpty()).doOnNext(word -> {
if (guavaCache.getIfPresent(word) == null)
guavaCache.put(word, 1);
}).blockLast();
}
Aggregations