Make InMemorySchemaSourceCache expire items 81/29481/1
authorRobert Varga <rovarga@cisco.com>
Tue, 10 Nov 2015 10:02:37 +0000 (11:02 +0100)
committerRobert Varga <rovarga@cisco.com>
Tue, 10 Nov 2015 10:07:52 +0000 (11:07 +0100)
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 <rovarga@cisco.com>
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/repo/util/InMemorySchemaSourceCache.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/repo/YangTextSchemaContextResolver.java

index 20e203d0c10473a901c40bcb373b207e22434262..52a5a957ebf6d0fd55605a01778bb2d89fee15af 100644 (file)
@@ -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<T extends SchemaSourceRepresentation> ext
         return new InMemorySchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues());
     }
 
+    public static <R extends SchemaSourceRepresentation> InMemorySchemaSourceCache<R> createSoftCache(
+            final SchemaSourceRegistry consumer, final Class<R> representation, final long lifetime, final TimeUnit units) {
+        return new InMemorySchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues()
+                .expireAfterAccess(lifetime, units));
+    }
+
     @Override
     public CheckedFuture<? extends T, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
         final T present = cache.getIfPresent(sourceIdentifier);
index 7c0c9763ce99e903baf643536551bd9604e6f789..9b5fb1ee916cbd0f9576a58c4c3b81a283084c6b 100644 (file)
@@ -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<YangTextSchemaSource> {
     private static final Logger LOG = LoggerFactory.getLogger(YangTextSchemaContextResolver.class);
+    private static final long SOURCE_LIFETIME_SECONDS = 60;
 
     private final Collection<SourceIdentifier> requiredSources = new ConcurrentLinkedDeque<>();
     private final Multimap<SourceIdentifier, YangTextSchemaSource> 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) {