Ver Fonte

BETA: Some cache classes review:
* HtmCache to javolution.
* Removing hashCode usage as i said many times its wrong.
* Cleanup.

Rumen Nikiforov há 12 anos atrás
pai
commit
c70e2f169e

+ 14 - 24
L2J_Server_BETA/java/com/l2jserver/gameserver/cache/HtmCache.java

@@ -14,17 +14,16 @@
  */
 package com.l2jserver.gameserver.cache;
 
-import gnu.trove.map.hash.TIntObjectHashMap;
-
 import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
+import java.util.Map;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import com.l2jserver.Config;
-import com.l2jserver.gameserver.util.L2TIntObjectHashMap;
 import com.l2jserver.gameserver.util.Util;
+import com.l2jserver.util.L2FastMap;
 import com.l2jserver.util.file.filter.HTMLFilter;
 
 /**
@@ -36,26 +35,14 @@ public class HtmCache
 	
 	private static final HTMLFilter htmlFilter = new HTMLFilter();
 	
-	private final TIntObjectHashMap<String> _cache;
+	private final Map<String, String> _cache;
 	
 	private int _loadedFiles;
 	private long _bytesBuffLen;
 	
-	public static HtmCache getInstance()
-	{
-		return SingletonHolder._instance;
-	}
-	
 	protected HtmCache()
 	{
-		if (Config.LAZY_CACHE)
-		{
-			_cache = new L2TIntObjectHashMap<>();
-		}
-		else
-		{
-			_cache = new TIntObjectHashMap<>();
-		}
+		_cache = new L2FastMap<>(Config.LAZY_CACHE);
 		reload();
 	}
 	
@@ -121,7 +108,6 @@ public class HtmCache
 		}
 		
 		final String relpath = Util.getRelativePath(Config.DATAPACK_ROOT, file);
-		final int hashcode = relpath.hashCode();
 		String content = null;
 		try (FileInputStream fis = new FileInputStream(file);
 			BufferedInputStream bis = new BufferedInputStream(fis))
@@ -133,7 +119,7 @@ public class HtmCache
 			content = new String(raw, "UTF-8");
 			content = content.replaceAll("\r\n", "\n");
 			
-			String oldContent = _cache.get(hashcode);
+			String oldContent = _cache.get(relpath);
 			if (oldContent == null)
 			{
 				_bytesBuffLen += bytes;
@@ -143,7 +129,7 @@ public class HtmCache
 			{
 				_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + bytes;
 			}
-			_cache.put(hashcode, content);
+			_cache.put(relpath, content);
 		}
 		catch (Exception e)
 		{
@@ -180,7 +166,7 @@ public class HtmCache
 		content = getHtm(path);
 		if ((content != null) && (newPath != null))
 		{
-			_cache.put(newPath.hashCode(), content);
+			_cache.put(newPath, content);
 		}
 		
 		return content;
@@ -193,8 +179,7 @@ public class HtmCache
 			return ""; // avoid possible NPE
 		}
 		
-		final int hashCode = path.hashCode();
-		String content = _cache.get(hashCode);
+		String content = _cache.get(path);
 		if (Config.LAZY_CACHE && (content == null))
 		{
 			content = loadFile(new File(Config.DATAPACK_ROOT, path));
@@ -204,7 +189,7 @@ public class HtmCache
 	
 	public boolean contains(String path)
 	{
-		return _cache.containsKey(path.hashCode());
+		return _cache.containsKey(path);
 	}
 	
 	/**
@@ -216,6 +201,11 @@ public class HtmCache
 		return htmlFilter.accept(new File(path));
 	}
 	
+	public static HtmCache getInstance()
+	{
+		return SingletonHolder._instance;
+	}
+	
 	private static class SingletonHolder
 	{
 		protected static final HtmCache _instance = new HtmCache();

+ 9 - 10
L2J_Server_BETA/java/com/l2jserver/gameserver/cache/WarehouseCacheManager.java

@@ -14,11 +14,12 @@
  */
 package com.l2jserver.gameserver.cache;
 
-import javolution.util.FastMap;
+import java.util.Map;
 
 import com.l2jserver.Config;
 import com.l2jserver.gameserver.ThreadPoolManager;
 import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
+import com.l2jserver.util.L2FastMap;
 
 /**
  *
@@ -26,18 +27,11 @@ import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  */
 public class WarehouseCacheManager
 {
-	protected final FastMap<L2PcInstance, Long> _cachedWh;
-	protected final long _cacheTime;
-	
-	public static WarehouseCacheManager getInstance()
-	{
-		return SingletonHolder._instance;
-	}
+	protected final Map<L2PcInstance, Long> _cachedWh = new L2FastMap<>(true);
+	protected final long _cacheTime = Config.WAREHOUSE_CACHE_TIME * 60000L;
 	
 	protected WarehouseCacheManager()
 	{
-		_cacheTime = Config.WAREHOUSE_CACHE_TIME * 60000L; // 60*1000 = 60000
-		_cachedWh = new FastMap<L2PcInstance, Long>().shared();
 		ThreadPoolManager.getInstance().scheduleAiAtFixedRate(new CacheScheduler(), 120000, 60000);
 	}
 	
@@ -68,6 +62,11 @@ public class WarehouseCacheManager
 		}
 	}
 	
+	public static WarehouseCacheManager getInstance()
+	{
+		return SingletonHolder._instance;
+	}
+	
 	private static class SingletonHolder
 	{
 		protected static final WarehouseCacheManager _instance = new WarehouseCacheManager();