From a1ac4c3b76ab0ea01c7a2f2e52721d94bff01717 Mon Sep 17 00:00:00 2001 From: Michael Peter Christen Date: Mon, 12 May 2014 15:45:25 +0200 Subject: [PATCH] automatically clear graphics cache --- htroot/NetworkPicture.java | 22 +++++++++---------- .../net/yacy/peers/graphics/NetworkGraph.java | 6 +++++ source/net/yacy/search/Switchboard.java | 5 +++++ source/net/yacy/visualization/CircleTool.java | 4 ++++ 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/htroot/NetworkPicture.java b/htroot/NetworkPicture.java index 16234c243..fec8bcc9a 100644 --- a/htroot/NetworkPicture.java +++ b/htroot/NetworkPicture.java @@ -36,14 +36,12 @@ import net.yacy.server.serverObjects; import net.yacy.server.serverSwitch; /** draw a picture of the yacy network */ -public class NetworkPicture -{ +public class NetworkPicture { private static final ConcurrentLog log = new ConcurrentLog("NetworkPicture"); private static final Semaphore sync = new Semaphore(1, true); - private static EncodedImage buffer = null; private static long lastAccessSeconds = 0; - + public static EncodedImage respond( final RequestHeader header, final serverObjects post, @@ -52,26 +50,26 @@ public class NetworkPicture final boolean authorized = sb.adminAuthenticated(header) >= 2; final long timeSeconds = System.currentTimeMillis() / 1000; - if (buffer != null && !authorized && timeSeconds - lastAccessSeconds < 2) { + if (NetworkGraph.buffer != null && !authorized && timeSeconds - lastAccessSeconds < 2) { if (log.isFine()) log.fine("cache hit (1); authorized = " + authorized + ", timeSeconds - lastAccessSeconds = " + (timeSeconds - lastAccessSeconds)); - return buffer; + return NetworkGraph.buffer; } - if ( buffer != null && sync.availablePermits() == 0 ) { - return buffer; + if ( NetworkGraph.buffer != null && sync.availablePermits() == 0 ) { + return NetworkGraph.buffer; } sync.acquireUninterruptibly(); - if (buffer != null && !authorized && timeSeconds - lastAccessSeconds < 2) { + if (NetworkGraph.buffer != null && !authorized && timeSeconds - lastAccessSeconds < 2) { if (log.isFine()) log.fine("cache hit (2); authorized = " + authorized + ", timeSeconds - lastAccessSeconds = " + (timeSeconds - lastAccessSeconds)); sync.release(); - return buffer; + return NetworkGraph.buffer; } int width = 1280; // 640x480 = VGA, 768x576 = SD/4:3, 1024x576 =SD/16:9 1280x720 = HD/16:9, 1920x1080 = FULL HD/16:9 @@ -124,7 +122,7 @@ public class NetworkPicture if ( maxCount > 10000 ) { maxCount = 10000; } - buffer = + NetworkGraph.buffer = new EncodedImage(NetworkGraph.getNetworkPicture( sb.peers, width, @@ -141,7 +139,7 @@ public class NetworkPicture lastAccessSeconds = System.currentTimeMillis() / 1000; sync.release(); - return buffer; + return NetworkGraph.buffer; } } diff --git a/source/net/yacy/peers/graphics/NetworkGraph.java b/source/net/yacy/peers/graphics/NetworkGraph.java index 705d8b9da..ce87f75dc 100644 --- a/source/net/yacy/peers/graphics/NetworkGraph.java +++ b/source/net/yacy/peers/graphics/NetworkGraph.java @@ -53,6 +53,8 @@ import net.yacy.visualization.RasterPlotter; public class NetworkGraph { private final static double DOUBLE_LONG_MAX_VALUE = Long.MAX_VALUE; + + public static EncodedImage buffer = null; private static int shortestName = 10; private static int longestName = 30; @@ -81,6 +83,10 @@ public class NetworkGraph { /** Private constructor to avoid instantiation of utility class. */ private NetworkGraph() { } + + public static void clearcache() { + buffer = null; + } public static class CircleThreadPiece { private final String pieceName; diff --git a/source/net/yacy/search/Switchboard.java b/source/net/yacy/search/Switchboard.java index 5c5071a3c..b29b83829 100644 --- a/source/net/yacy/search/Switchboard.java +++ b/source/net/yacy/search/Switchboard.java @@ -176,6 +176,7 @@ import net.yacy.peers.DHTSelection; import net.yacy.peers.Protocol; import net.yacy.peers.Seed; import net.yacy.peers.SeedDB; +import net.yacy.peers.graphics.NetworkGraph; import net.yacy.peers.graphics.WebStructureGraph; import net.yacy.peers.operation.yacyBuildProperties; import net.yacy.peers.operation.yacyRelease; @@ -200,6 +201,7 @@ import net.yacy.server.http.RobotsTxtConfig; import net.yacy.utils.CryptoLib; import net.yacy.utils.UPnP; import net.yacy.utils.crypt; +import net.yacy.visualization.CircleTool; import com.google.common.io.Files; @@ -1999,6 +2001,9 @@ public final class Switchboard extends serverSwitch { Cache.commit(); Digest.cleanup(); // don't let caches become permanent memory leaks + // clear graphics caches + CircleTool.clearcache(); + NetworkGraph.clearcache(); } public int cleanupJobSize() { diff --git a/source/net/yacy/visualization/CircleTool.java b/source/net/yacy/visualization/CircleTool.java index 46206ef9f..de8801c38 100644 --- a/source/net/yacy/visualization/CircleTool.java +++ b/source/net/yacy/visualization/CircleTool.java @@ -35,6 +35,10 @@ import java.util.Set; public class CircleTool { private static List circles = new ArrayList<>(); + + public static void clearcache() { + circles.clear(); + } private static int[] getCircleCoords(final short radius) { if (radius - 1 < circles.size()) return circles.get(radius - 1);