[Schmitzm-commits] r2026 - trunk/schmitzm-core/src/main/java/de/schmitzm/regex

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Sun May 27 15:09:21 CEST 2012


Author: alfonx
Date: 2012-05-27 15:09:21 +0200 (Sun, 27 May 2012)
New Revision: 2026

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/regex/RegexCache.java
Log:


Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/regex/RegexCache.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/regex/RegexCache.java	2012-05-27 12:48:55 UTC (rev 2025)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/regex/RegexCache.java	2012-05-27 13:09:21 UTC (rev 2026)
@@ -4,8 +4,9 @@
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import org.apache.solr.util.ConcurrentLFUCache;
-import org.apache.solr.util.ConcurrentLFUCache.Stats;
+import org.apache.log4j.Logger;
+import org.apache.solr.util.ConcurrentLRUCache;
+import org.apache.solr.util.ConcurrentLRUCache.Stats;
 
 /**
  * Cached compilierte Pattern und auch Ergebnisse von RegExes. Use the matchers methods to obtain cached result and add
@@ -14,10 +15,13 @@
  * @author stranger
  * @author stefan.tzeggai
  * 
- *         TODO ConcurrentLFUCache von solr einbauen um unendlichen Speicherverbrauch vorzubeugen
+ *         TODO ConcurrentLRUCache von solr einbauen um unendlichen Speicherverbrauch vorzubeugen
  */
 
 public class RegexCache {
+
+	final static Logger log = Logger.getLogger(RegexCache.class);
+
 	/**
 	 * Matcher-Cache für Regex+Value verwenden? Durch die methode mather.reset wird pro Thread ein Matcher
 	 * wiederverwendet
@@ -40,19 +44,19 @@
 	 **/
 	private static final int CACHE_VALUES_TO_RESULT_MIN_VALUE_LENGTH = 10;
 
-	private final ConcurrentLFUCache<String, ThreadLocal<Matcher>> matchersCachedThreadLocalPerRegex = new ConcurrentLFUCache<String, ThreadLocal<Matcher>>(
+	private final ConcurrentLRUCache<String, ThreadLocal<Matcher>> matchersCachedThreadLocalPerRegex = new ConcurrentLRUCache<String, ThreadLocal<Matcher>>(
 			200000, 150000);
 
 	private static RegexCache singletonInstance;
 
-	private final ConcurrentLFUCache<String, Pattern> patternCache = new ConcurrentLFUCache<String, Pattern>(50000,
+	private final ConcurrentLRUCache<String, Pattern> patternCache = new ConcurrentLRUCache<String, Pattern>(50000,
 			35000);
 
 	public static RegexCache getInstance() {
 		return singletonInstance != null ? singletonInstance : new RegexCache();
 	}
 
-	private final ConcurrentLFUCache<Pattern, ConcurrentLFUCache<String, MyMatchResult>> resultsCache = new ConcurrentLFUCache<Pattern, ConcurrentLFUCache<String, MyMatchResult>>(
+	private final ConcurrentLRUCache<Pattern, ConcurrentLRUCache<String, MyMatchResult>> resultsCache = new ConcurrentLRUCache<Pattern, ConcurrentLRUCache<String, MyMatchResult>>(
 			150000, 100000);
 
 	private RegexCache() {
@@ -147,10 +151,10 @@
 
 		final Pattern pattern = getPattern(regex);
 
-		ConcurrentLFUCache<String, MyMatchResult> m;
+		ConcurrentLRUCache<String, MyMatchResult> m;
 		m = resultsCache.get(pattern);
 		if (m == null) {
-			resultsCache.put(pattern, m = new ConcurrentLFUCache<String, MyMatchResult>(50, 20));
+			resultsCache.put(pattern, m = new ConcurrentLRUCache<String, MyMatchResult>(50, 20));
 		}
 
 		MyMatchResult mResult;
@@ -206,4 +210,33 @@
 		}
 	}
 
+	@Override
+	protected void finalize() throws Throwable {
+		try {
+			destroy();
+		} finally {
+			super.finalize();
+		}
+	}
+
+	/**
+	 * The RegexCache is no more usable after calling destroy!
+	 */
+	public void destroy() {
+		if (resultsCache != null) {
+			resultsCache.clear();
+			resultsCache.destroy();
+		}
+
+		if (patternCache != null) {
+			patternCache.clear();
+			patternCache.destroy();
+		}
+
+		if (matchersCachedThreadLocalPerRegex != null) {
+			matchersCachedThreadLocalPerRegex.clear();
+			matchersCachedThreadLocalPerRegex.destroy();
+		}
+	}
+
 }



More information about the Schmitzm-commits mailing list