Search in sources :

Example 11 with LoadingCache

use of com.google.common.cache.LoadingCache in project nakadi by zalando.

the class EventTypeCache method setupInMemoryEventTypeCache.

private LoadingCache<String, CachedValue> setupInMemoryEventTypeCache(final EventTypeRepository eventTypeRepository, final TimelineDbRepository timelineRepository) {
    final CacheLoader<String, CachedValue> loader = new CacheLoader<String, CachedValue>() {

        public CachedValue load(final String key) throws Exception {
            final EventType eventType = eventTypeRepository.findByName(key);
            final List<Timeline> timelines = timelineRepository.listTimelinesOrdered(key);
            timelineRegistrations.computeIfAbsent(key, n -> timelineSync.registerTimelineChangeListener(n, (etName) -> eventTypeCache.invalidate(etName)));
            return new CachedValue(eventType, EventValidation.forType(eventType), timelines);
        }
    };
    return CacheBuilder.newBuilder().maximumSize(CACHE_MAX_SIZE).build(loader);
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) LoadingCache(com.google.common.cache.LoadingCache) LoggerFactory(org.slf4j.LoggerFactory) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) NoSuchEventTypeException(org.zalando.nakadi.exceptions.NoSuchEventTypeException) EventValidation(org.zalando.nakadi.validation.EventValidation) ZKPaths(org.apache.curator.utils.ZKPaths) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) EventTypeValidator(org.zalando.nakadi.validation.EventTypeValidator) Nonnull(javax.annotation.Nonnull) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ZooKeeperHolder(org.zalando.nakadi.repository.zookeeper.ZooKeeperHolder) EventType(org.zalando.nakadi.domain.EventType) Logger(org.slf4j.Logger) KeeperException(org.apache.zookeeper.KeeperException) Iterator(java.util.Iterator) TimelineSync(org.zalando.nakadi.service.timeline.TimelineSync) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Collectors(java.util.stream.Collectors) CacheLoader(com.google.common.cache.CacheLoader) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) List(java.util.List) CuratorFramework(org.apache.curator.framework.CuratorFramework) Timeline(org.zalando.nakadi.domain.Timeline) EventTypeRepository(org.zalando.nakadi.repository.EventTypeRepository) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) CacheBuilder(com.google.common.cache.CacheBuilder) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Timeline(org.zalando.nakadi.domain.Timeline) EventType(org.zalando.nakadi.domain.EventType) CacheLoader(com.google.common.cache.CacheLoader)

Example 12 with LoadingCache

use of com.google.common.cache.LoadingCache in project free-framework by a601942905git.

the class RefreshCacheTest method main.

/**
 * 异步刷新缓存,当有线程访问缓存中的数据的时候,发现缓存中的数据过期了,会调用异步刷新方法来进行缓存刷新
 * 但是当有多个线程请求已经失效的缓存的时候,只有一个线程用来刷新缓存数据,其余线程都返回旧数据用于展示
 * @param args
 * @throws ExecutionException
 * @throws InterruptedException
 */
public static void main(String[] args) throws ExecutionException, InterruptedException {
    LoadingCache cache = CacheBuilder.newBuilder().refreshAfterWrite(5, TimeUnit.SECONDS).build(new CacheLoader<String, String>() {

        @Override
        public String load(String key) {
            String randomStr = UUID.randomUUID().toString();
            asyncReloading(this, executor);
            return randomStr;
        }
    });
    System.out.println("第一次获取缓存数据:" + cache.get("randomStr"));
    Thread.sleep(6000);
    for (int i = 0; i < 100; i++) {
        new Thread(() -> {
            try {
                System.out.println("第二次获取缓存数据:" + cache.get("randomStr"));
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
Also used : LoadingCache(com.google.common.cache.LoadingCache) ExecutionException(java.util.concurrent.ExecutionException)

Example 13 with LoadingCache

use of com.google.common.cache.LoadingCache in project snow-owl by b2ihealthcare.

the class SnomedQueryOptimizer method optimize.

@Override
public QueryExpressionDiffs optimize(BranchContext context, Options params) {
    final Collection<QueryExpression> inclusions = params.getCollection(QueryOptimizer.OptionKey.INCLUSIONS, QueryExpression.class);
    final List<ExtendedLocale> locales = params.getList(QueryOptimizer.OptionKey.LOCALES, ExtendedLocale.class);
    final EclParser eclParser = context.service(EclParser.class);
    final LoadingCache<String, ExpressionConstraint> eclCache = CacheBuilder.newBuilder().build(CacheLoader.from(eclParser::parse));
    final Multimap<String, QueryExpression> singleConceptInclusions = FluentIterable.from(inclusions).filter(ex -> isSingleConceptExpression(eclCache, ex.getQuery())).index(ex -> toSingleConceptId(eclCache, ex.getQuery()));
    // if there are no single concept inclusions to optimize, exit early
    if (singleConceptInclusions.isEmpty()) {
        return new QueryExpressionDiffs(Collections.emptyList());
    }
    // Record the ancestors (both direct and indirect) of each single concept inclusion
    final Multimap<String, QueryExpression> membersByAncestor = HashMultimap.create();
    SnomedRequests.prepareSearchConcept().filterByIds(singleConceptInclusions.keySet()).setLimit(singleConceptInclusions.keySet().size()).stream(context).flatMap(SnomedConcepts::stream).forEach(child -> {
        final Collection<QueryExpression> childExpressions = singleConceptInclusions.get(child.getId());
        final List<String> parentIds = child.getParentIdsAsString();
        final List<String> ancestorIds = child.getAncestorIdsAsString();
        parentIds.forEach(parentId -> {
            if (!IComponent.ROOT_ID.equals(parentId) && !Concepts.ROOT_CONCEPT.equals(parentId)) {
                membersByAncestor.putAll(parentId, childExpressions);
            }
        });
        ancestorIds.forEach(ancestorId -> {
            if (!IComponent.ROOT_ID.equals(ancestorId) && !Concepts.ROOT_CONCEPT.equals(ancestorId)) {
                membersByAncestor.putAll(ancestorId, childExpressions);
            }
        });
    });
    // Get number of referenced descendants (taking possible duplicates into account)
    final Map<String, Long> uniqueDescendantsByParent = ImmutableMap.copyOf(Maps.transformValues(membersByAncestor.asMap(), descendants -> descendants.stream().map(QueryExpression::getQuery).distinct().count()));
    final ImmutableList.Builder<QueryExpressionDiff> diffs = ImmutableList.builder();
    // references can be replaced with a single << expression.
    for (Entry<String, Long> uniqueDescendantsByParentEntry : uniqueDescendantsByParent.entrySet()) {
        SnomedConcept parent = SnomedRequests.prepareGetConcept(uniqueDescendantsByParentEntry.getKey()).setLocales(locales).setExpand("pt(),descendants(direct:false,limit:0)").build().execute(context);
        final String parentId = parent.getId();
        final int referencedDescendants = Ints.checkedCast(uniqueDescendantsByParent.get(parentId));
        final int totalDescendants = parent.getDescendants().getTotal();
        if (totalDescendants == referencedDescendants) {
            final List<QueryExpression> remove = List.copyOf(membersByAncestor.get(parentId).stream().filter(ex -> !ex.isPinned()).collect(Collectors.toList()));
            // The optimization is a "net win" if we can remove at least two clauses from the original
            if (remove.size() > 1) {
                final QueryExpression replacement = new QueryExpression(IDs.base64UUID(), String.format("<%s%s", parent.getId(), getTerm(parent)), false);
                final List<QueryExpression> addToInclusion = List.of(replacement);
                final List<QueryExpression> addToExclusion = List.of();
                final QueryExpressionDiff diff = new QueryExpressionDiff(addToInclusion, addToExclusion, remove);
                diffs.add(diff);
            }
        }
    }
    return new QueryExpressionDiffs(diffs.build());
}
Also used : ExpressionConstraint(com.b2international.snomed.ecl.ecl.ExpressionConstraint) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) LoadingCache(com.google.common.cache.LoadingCache) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) ExtendedLocale(com.b2international.commons.http.ExtendedLocale) SnomedRequests(com.b2international.snowowl.snomed.datastore.request.SnomedRequests) Options(com.b2international.commons.options.Options) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) Map(java.util.Map) QueryOptimizer(com.b2international.snowowl.core.request.QueryOptimizer) com.google.common.collect(com.google.common.collect) BadRequestException(com.b2international.commons.exceptions.BadRequestException) Collection(java.util.Collection) SnomedConcepts(com.b2international.snowowl.snomed.core.domain.SnomedConcepts) Collectors(java.util.stream.Collectors) Ints(com.google.common.primitives.Ints) IDs(com.b2international.snowowl.core.id.IDs) CacheLoader(com.google.common.cache.CacheLoader) EclParser(com.b2international.snowowl.snomed.core.ecl.EclParser) List(java.util.List) Entry(java.util.Map.Entry) CacheBuilder(com.google.common.cache.CacheBuilder) EclConceptReference(com.b2international.snomed.ecl.ecl.EclConceptReference) ExpressionConstraint(com.b2international.snomed.ecl.ecl.ExpressionConstraint) com.b2international.snowowl.core.domain(com.b2international.snowowl.core.domain) Collections(java.util.Collections) ExtendedLocale(com.b2international.commons.http.ExtendedLocale) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) ExpressionConstraint(com.b2international.snomed.ecl.ecl.ExpressionConstraint) EclParser(com.b2international.snowowl.snomed.core.ecl.EclParser)

Example 14 with LoadingCache

use of com.google.common.cache.LoadingCache in project gerrit by GerritCodeReview.

the class CacheModule method bindCacheLoader.

<K, V> Provider<CacheLoader<K, V>> bindCacheLoader(CacheProvider<K, V> m, Class<? extends CacheLoader<K, V>> impl) {
    Type type = Types.newParameterizedType(Cache.class, m.keyType().getType(), m.valueType().getType());
    Type loadingType = Types.newParameterizedType(LoadingCache.class, m.keyType().getType(), m.valueType().getType());
    Type loaderType = Types.newParameterizedType(CacheLoader.class, m.keyType().getType(), m.valueType().getType());
    @SuppressWarnings("unchecked") Key<LoadingCache<K, V>> key = (Key<LoadingCache<K, V>>) Key.get(type, Names.named(m.name));
    @SuppressWarnings("unchecked") Key<LoadingCache<K, V>> loadingKey = (Key<LoadingCache<K, V>>) Key.get(loadingType, Names.named(m.name));
    @SuppressWarnings("unchecked") Key<CacheLoader<K, V>> loaderKey = (Key<CacheLoader<K, V>>) Key.get(loaderType, Names.named(m.name));
    bind(loaderKey).to(impl).in(Scopes.SINGLETON);
    bind(loadingKey).to(key);
    return getProvider(loaderKey);
}
Also used : Type(java.lang.reflect.Type) LoadingCache(com.google.common.cache.LoadingCache) CacheLoader(com.google.common.cache.CacheLoader) Key(com.google.inject.Key)

Aggregations

LoadingCache (com.google.common.cache.LoadingCache)14 Map (java.util.Map)7 List (java.util.List)6 CacheLoader (com.google.common.cache.CacheLoader)5 Optional (java.util.Optional)5 CacheBuilder (com.google.common.cache.CacheBuilder)4 ExecutionException (java.util.concurrent.ExecutionException)3 Collectors (java.util.stream.Collectors)3 BuildTarget (com.facebook.buck.model.BuildTarget)2 ImmutableList (com.google.common.collect.ImmutableList)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Nonnull (javax.annotation.Nonnull)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 FailurePolicy (co.cask.cdap.api.flow.flowlet.FailurePolicy)1 FailureReason (co.cask.cdap.api.flow.flowlet.FailureReason)1 InputContext (co.cask.cdap.api.flow.flowlet.InputContext)1 MetricValue (co.cask.cdap.api.metrics.MetricValue)1 MetricValues (co.cask.cdap.api.metrics.MetricValues)1