Store yang store snapshot cache using soft reference. 93/6893/1
authorTomas Olvecky <tolvecky@cisco.com>
Mon, 12 May 2014 12:58:53 +0000 (14:58 +0200)
committerTomas Olvecky <tolvecky@cisco.com>
Mon, 12 May 2014 12:58:53 +0000 (14:58 +0200)
Change-Id: I9b159db83ba204b4a636f2314fd4fc2e7b6f654c
Signed-off-by: Tomas Olvecky <tolvecky@cisco.com>
opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/osgi/YangStoreServiceImpl.java

index 1102f342bd054f879c1ce06dc25071f629b506bd..5840c9dbd14d1aa3b3fe050d079e426d8ec21f20 100644 (file)
@@ -8,6 +8,7 @@
 
 package org.opendaylight.controller.netconf.confignetconfconnector.osgi;
 
+import java.lang.ref.SoftReference;
 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
 
 import javax.annotation.concurrent.GuardedBy;
@@ -15,7 +16,7 @@ import javax.annotation.concurrent.GuardedBy;
 public class YangStoreServiceImpl implements YangStoreService {
     private final SchemaContextProvider service;
     @GuardedBy("this")
-    private YangStoreSnapshotImpl cache = null;
+    private SoftReference<YangStoreSnapshotImpl> cache = new SoftReference<>(null);
 
     public YangStoreServiceImpl(SchemaContextProvider service) {
         this.service = service;
@@ -23,16 +24,18 @@ public class YangStoreServiceImpl implements YangStoreService {
 
     @Override
     public synchronized YangStoreSnapshotImpl getYangStoreSnapshot() throws YangStoreException {
-        if (cache == null) {
-            cache = new YangStoreSnapshotImpl(service.getSchemaContext());
+        YangStoreSnapshotImpl yangStoreSnapshot = cache.get();
+        if (yangStoreSnapshot == null) {
+            yangStoreSnapshot = new YangStoreSnapshotImpl(service.getSchemaContext());
+            cache = new SoftReference<>(yangStoreSnapshot);
         }
-        return cache;
+        return yangStoreSnapshot;
     }
 
     /**
      * Called when schema context changes, invalidates cache.
      */
     public synchronized void refresh() {
-        cache = null;
+        cache.clear();
     }
 }