From 745a9f083b29e5604b94294331c0367f3676a4f5 Mon Sep 17 00:00:00 2001
From: zzz <zzz@i2pmail.org>
Date: Sat, 6 Feb 2021 11:35:39 -0500
Subject: [PATCH] NetDB: Only rewrite blind cache at shutdown if changed

---
 .../net/i2p/router/networkdb/kademlia/BlindCache.java | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/router/java/src/net/i2p/router/networkdb/kademlia/BlindCache.java b/router/java/src/net/i2p/router/networkdb/kademlia/BlindCache.java
index 0952efeca7..2a34bead6a 100644
--- a/router/java/src/net/i2p/router/networkdb/kademlia/BlindCache.java
+++ b/router/java/src/net/i2p/router/networkdb/kademlia/BlindCache.java
@@ -41,6 +41,7 @@ class BlindCache {
     private final ConcurrentHashMap<SigningPublicKey, BlindData> _reverseCache;
     // dest hash
     private final ConcurrentHashMap<Hash, BlindData> _hashCache;
+    private boolean _changed;
 
     private static final String PERSIST_FILE = "router.blindcache.dat";
 
@@ -58,7 +59,8 @@ class BlindCache {
      *  May be restarted by calling startup() again.
      */
     public synchronized void shutdown() {
-        store();
+        if (_changed)
+            store();
         _cache.clear();
         _reverseCache.clear();
         _hashCache.clear();
@@ -173,8 +175,11 @@ class BlindCache {
      */
     public void addToCache(BlindData bd) {
         storeInCache(bd);
-        if (bd.getSecret() != null || bd.getAuthPrivKey() != null)
+        if (bd.getSecret() != null || bd.getAuthPrivKey() != null) {
             store();
+        } else {
+            synchronized(this) { _changed = true; }
+        }
     }
 
     /**
@@ -313,6 +318,7 @@ class BlindCache {
                         log.warn("Error reading cache entry: " + line, dfe);
                 }
             }
+            _changed = false;
         } catch (IOException ioe) {
             if (log.shouldLog(Log.WARN) && file.exists())
                 log.warn("Error reading the blinding cache file", ioe);
@@ -339,6 +345,7 @@ class BlindCache {
             }
             if (out.checkError())
                 throw new IOException("Failed write to " + file);
+            _changed = false;
         } catch (IOException ioe) {
             if (log.shouldLog(Log.WARN))
                 log.warn("Error writing the blinding cache File", ioe);
-- 
GitLab