2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.model.repo.spi;
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.FinalizablePhantomReference;
12 import com.google.common.base.FinalizableReferenceQueue;
13 import com.google.common.cache.Cache;
14 import com.google.common.cache.CacheBuilder;
15 import com.google.common.util.concurrent.FluentFuture;
16 import java.time.Duration;
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.concurrent.TimeUnit;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
23 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
24 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
25 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
28 * A simple {@link AbstractSchemaSourceCache} based on {@link Cache Guava Cache}.
30 * @param <T> {@link SchemaSourceRepresentation} type stored in this cache
33 public final class GuavaSchemaSourceCache<T extends SchemaSourceRepresentation> extends AbstractSchemaSourceCache<T>
34 implements AutoCloseable {
35 // FIXME: 7.0.0: use a java.util.Cleaner?
36 private final List<FinalizablePhantomReference<T>> regs = Collections.synchronizedList(new ArrayList<>());
37 private final FinalizableReferenceQueue queue = new FinalizableReferenceQueue();
38 private final Cache<SourceIdentifier, T> cache;
40 private GuavaSchemaSourceCache(final SchemaSourceRegistry consumer, final Class<T> representation,
41 final CacheBuilder<Object, Object> cacheBuilder) {
42 super(consumer, representation, PotentialSchemaSource.Costs.IMMEDIATE);
43 cache = cacheBuilder.build();
46 public static <R extends SchemaSourceRepresentation> @NonNull GuavaSchemaSourceCache<R> createSoftCache(
47 final SchemaSourceRegistry consumer, final Class<R> representation) {
48 return new GuavaSchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues());
51 public static <R extends SchemaSourceRepresentation> @NonNull GuavaSchemaSourceCache<R> createSoftCache(
52 final SchemaSourceRegistry consumer, final Class<R> representation, final long lifetime,
53 final TimeUnit units) {
54 return new GuavaSchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues()
55 .expireAfterAccess(lifetime, units));
58 public static <R extends SchemaSourceRepresentation> @NonNull GuavaSchemaSourceCache<R> createSoftCache(
59 final SchemaSourceRegistry consumer, final Class<R> representation, final Duration duration) {
60 return new GuavaSchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues()
61 .expireAfterAccess(duration));
65 public FluentFuture<? extends T> getSource(final SourceIdentifier sourceIdentifier) {
66 final T present = cache.getIfPresent(sourceIdentifier);
67 return present != null ? FluentFutures.immediateFluentFuture(present)
68 : FluentFutures.immediateFailedFluentFuture(new MissingSchemaSourceException("Source not found",
73 protected void offer(final T source) {
74 final T present = cache.getIfPresent(source.getIdentifier());
75 if (present == null) {
76 cache.put(source.getIdentifier(), source);
78 final SchemaSourceRegistration<T> reg = register(source.getIdentifier());
79 final FinalizablePhantomReference<T> ref = new FinalizablePhantomReference<>(source, queue) {
81 public void finalizeReferent() {
93 while (!regs.isEmpty()) {
94 final FinalizablePhantomReference<?> ref = regs.get(0);
95 ref.finalizeReferent();
98 cache.invalidateAll();