From: Robert Varga Date: Tue, 10 Nov 2015 10:02:37 +0000 (+0100) Subject: Make InMemorySchemaSourceCache expire items X-Git-Tag: release/beryllium~150 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=e420e37d6545f45c94c1cffbd5e261cad1cb3ead;p=yangtools.git Make InMemorySchemaSourceCache expire items We use the class only for ASTs. Caching them is good for periods of parsing churn, such as startup, but the items should expire after not being used for some time. Change-Id: I38d95b62fd823906b851466c39429aaab7a72b56 Signed-off-by: Robert Varga --- diff --git a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/repo/util/InMemorySchemaSourceCache.java b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/repo/util/InMemorySchemaSourceCache.java index 20e203d0c1..52a5a957eb 100644 --- a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/repo/util/InMemorySchemaSourceCache.java +++ b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/repo/util/InMemorySchemaSourceCache.java @@ -17,6 +17,7 @@ import com.google.common.util.concurrent.Futures; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.concurrent.TimeUnit; import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException; import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException; import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation; @@ -40,6 +41,12 @@ public class InMemorySchemaSourceCache ext return new InMemorySchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues()); } + public static InMemorySchemaSourceCache createSoftCache( + final SchemaSourceRegistry consumer, final Class representation, final long lifetime, final TimeUnit units) { + return new InMemorySchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues() + .expireAfterAccess(lifetime, units)); + } + @Override public CheckedFuture getSource(final SourceIdentifier sourceIdentifier) { final T present = cache.getIfPresent(sourceIdentifier); diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/repo/YangTextSchemaContextResolver.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/repo/YangTextSchemaContextResolver.java index 7c0c9763ce..9b5fb1ee91 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/repo/YangTextSchemaContextResolver.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/repo/YangTextSchemaContextResolver.java @@ -22,6 +22,7 @@ import java.net.URL; import java.util.Collection; import java.util.Set; import java.util.concurrent.ConcurrentLinkedDeque; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import javax.annotation.Nonnull; import org.opendaylight.yangtools.yang.model.api.SchemaContext; @@ -48,6 +49,7 @@ import org.slf4j.LoggerFactory; public final class YangTextSchemaContextResolver implements AutoCloseable, SchemaSourceProvider { private static final Logger LOG = LoggerFactory.getLogger(YangTextSchemaContextResolver.class); + private static final long SOURCE_LIFETIME_SECONDS = 60; private final Collection requiredSources = new ConcurrentLinkedDeque<>(); private final Multimap texts = ArrayListMultimap.create(); @@ -67,7 +69,8 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem final TextToASTTransformer t = TextToASTTransformer.create(repository, registry); transReg = registry.registerSchemaSourceListener(t); - cache = InMemorySchemaSourceCache.createSoftCache(registry, ASTSchemaSource.class); + cache = InMemorySchemaSourceCache.createSoftCache(registry, ASTSchemaSource.class, SOURCE_LIFETIME_SECONDS, + TimeUnit.SECONDS); } public static YangTextSchemaContextResolver create(final String name) {