actual freya branch
This commit is contained in:
@@ -0,0 +1,321 @@
|
||||
/*
|
||||
* This program is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later
|
||||
* version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jserver.gameserver.cache;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javolution.util.FastMap;
|
||||
|
||||
import com.l2jserver.Config;
|
||||
import com.l2jserver.L2DatabaseFactory;
|
||||
import com.l2jserver.gameserver.datatables.ClanTable;
|
||||
import com.l2jserver.gameserver.idfactory.IdFactory;
|
||||
import com.l2jserver.gameserver.model.L2Clan;
|
||||
|
||||
/**
|
||||
* @author Layane
|
||||
*/
|
||||
public class CrestCache
|
||||
{
|
||||
private static Logger _log = Logger.getLogger(CrestCache.class.getName());
|
||||
|
||||
private final FastMRUCache<Integer, byte[]> _cachePledge = new FastMRUCache<>();
|
||||
|
||||
private final FastMRUCache<Integer, byte[]> _cachePledgeLarge = new FastMRUCache<>();
|
||||
|
||||
private final FastMRUCache<Integer, byte[]> _cacheAlly = new FastMRUCache<>();
|
||||
|
||||
private int _loadedFiles;
|
||||
|
||||
private long _bytesBuffLen;
|
||||
|
||||
public static CrestCache getInstance()
|
||||
{
|
||||
return SingletonHolder._instance;
|
||||
}
|
||||
|
||||
private CrestCache()
|
||||
{
|
||||
convertOldPedgeFiles();
|
||||
reload();
|
||||
}
|
||||
|
||||
public void reload()
|
||||
{
|
||||
FileFilter filter = new BmpFilter();
|
||||
|
||||
File dir = new File(Config.DATAPACK_ROOT, "data/crests/");
|
||||
|
||||
File[] files = dir.listFiles(filter);
|
||||
byte[] content;
|
||||
synchronized (this)
|
||||
{
|
||||
_loadedFiles = 0;
|
||||
_bytesBuffLen = 0;
|
||||
|
||||
_cachePledge.clear();
|
||||
_cachePledgeLarge.clear();
|
||||
_cacheAlly.clear();
|
||||
}
|
||||
|
||||
FastMap<Integer, byte[]> _mapPledge = _cachePledge.getContentMap();
|
||||
FastMap<Integer, byte[]> _mapPledgeLarge = _cachePledgeLarge.getContentMap();
|
||||
FastMap<Integer, byte[]> _mapAlly = _cacheAlly.getContentMap();
|
||||
|
||||
for (File file : files)
|
||||
{
|
||||
|
||||
synchronized (this)
|
||||
{
|
||||
try (RandomAccessFile f = new RandomAccessFile(file, "r"))
|
||||
{
|
||||
content = new byte[(int) f.length()];
|
||||
f.readFully(content);
|
||||
|
||||
if (file.getName().startsWith("Crest_Large_"))
|
||||
{
|
||||
_mapPledgeLarge.put(Integer.valueOf(file.getName().substring(12, file.getName().length() - 4)), content);
|
||||
}
|
||||
else if (file.getName().startsWith("Crest_"))
|
||||
{
|
||||
_mapPledge.put(Integer.valueOf(file.getName().substring(6, file.getName().length() - 4)), content);
|
||||
}
|
||||
else if (file.getName().startsWith("AllyCrest_"))
|
||||
{
|
||||
_mapAlly.put(Integer.valueOf(file.getName().substring(10, file.getName().length() - 4)), content);
|
||||
}
|
||||
_loadedFiles++;
|
||||
_bytesBuffLen += content.length;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_log.log(Level.WARNING, "Problem with crest bmp file " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_log.info("Cache[Crest]: " + String.format("%.3f", getMemoryUsage()) + "MB on " + getLoadedFiles() + " files loaded. (Forget Time: " + (_cachePledge.getForgetTime() / 1000) + "s , Capacity: " + _cachePledge.capacity() + ")");
|
||||
}
|
||||
|
||||
public void convertOldPedgeFiles()
|
||||
{
|
||||
File dir = new File(Config.DATAPACK_ROOT, "data/crests/");
|
||||
|
||||
File[] files = dir.listFiles(new OldPledgeFilter());
|
||||
|
||||
for (File file : files)
|
||||
{
|
||||
int clanId = Integer.parseInt(file.getName().substring(7, file.getName().length() - 4));
|
||||
|
||||
_log.info("Found old crest file \"" + file.getName() + "\" for clanId " + clanId);
|
||||
|
||||
int newId = IdFactory.getInstance().getNextId();
|
||||
|
||||
L2Clan clan = ClanTable.getInstance().getClan(clanId);
|
||||
|
||||
if (clan != null)
|
||||
{
|
||||
removeOldPledgeCrest(clan.getCrestId());
|
||||
|
||||
file.renameTo(new File(Config.DATAPACK_ROOT, "data/crests/Crest_" + newId + ".bmp"));
|
||||
_log.info("Renamed Clan crest to new format: Crest_" + newId + ".bmp");
|
||||
|
||||
Connection con = null;
|
||||
|
||||
try
|
||||
{
|
||||
con = L2DatabaseFactory.getInstance().getConnection();
|
||||
PreparedStatement statement = con.prepareStatement("UPDATE clan_data SET crest_id = ? WHERE clan_id = ?");
|
||||
statement.setInt(1, newId);
|
||||
statement.setInt(2, clan.getClanId());
|
||||
statement.executeUpdate();
|
||||
statement.close();
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
_log.log(Level.WARNING, "Could not update the crest id:" + e.getMessage(), e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
L2DatabaseFactory.close(con);
|
||||
}
|
||||
|
||||
clan.setCrestId(newId);
|
||||
}
|
||||
else
|
||||
{
|
||||
_log.info("Clan Id: " + clanId + " does not exist in table.. deleting.");
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public float getMemoryUsage()
|
||||
{
|
||||
return ((float) _bytesBuffLen / 1048576);
|
||||
}
|
||||
|
||||
public int getLoadedFiles()
|
||||
{
|
||||
return _loadedFiles;
|
||||
}
|
||||
|
||||
public byte[] getPledgeCrest(int id)
|
||||
{
|
||||
return _cachePledge.get(id);
|
||||
}
|
||||
|
||||
public byte[] getPledgeCrestLarge(int id)
|
||||
{
|
||||
return _cachePledgeLarge.get(id);
|
||||
}
|
||||
|
||||
public byte[] getAllyCrest(int id)
|
||||
{
|
||||
return _cacheAlly.get(id);
|
||||
}
|
||||
|
||||
public void removePledgeCrest(int id)
|
||||
{
|
||||
File crestFile = new File(Config.DATAPACK_ROOT, "data/crests/Crest_" + id + ".bmp");
|
||||
_cachePledge.remove(id);
|
||||
try
|
||||
{
|
||||
crestFile.delete();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public void removePledgeCrestLarge(int id)
|
||||
{
|
||||
File crestFile = new File(Config.DATAPACK_ROOT, "data/crests/Crest_Large_" + id + ".bmp");
|
||||
_cachePledgeLarge.remove(id);
|
||||
try
|
||||
{
|
||||
crestFile.delete();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public void removeOldPledgeCrest(int id)
|
||||
{
|
||||
File crestFile = new File(Config.DATAPACK_ROOT, "data/crests/Pledge_" + id + ".bmp");
|
||||
try
|
||||
{
|
||||
crestFile.delete();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public void removeAllyCrest(int id)
|
||||
{
|
||||
File crestFile = new File(Config.DATAPACK_ROOT, "data/crests/AllyCrest_" + id + ".bmp");
|
||||
_cacheAlly.remove(id);
|
||||
try
|
||||
{
|
||||
crestFile.delete();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public boolean savePledgeCrest(int newId, byte[] data)
|
||||
{
|
||||
File crestFile = new File(Config.DATAPACK_ROOT, "data/crests/Crest_" + newId + ".bmp");
|
||||
try (FileOutputStream out = new FileOutputStream(crestFile))
|
||||
{
|
||||
out.write(data);
|
||||
_cachePledge.getContentMap().put(newId, data);
|
||||
return true;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
_log.log(Level.INFO, "Error saving pledge crest" + crestFile + ":", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean savePledgeCrestLarge(int newId, byte[] data)
|
||||
{
|
||||
File crestFile = new File(Config.DATAPACK_ROOT, "data/crests/Crest_Large_" + newId + ".bmp");
|
||||
try (FileOutputStream out = new FileOutputStream(crestFile))
|
||||
{
|
||||
out.write(data);
|
||||
_cachePledgeLarge.getContentMap().put(newId, data);
|
||||
return true;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
_log.log(Level.INFO, "Error saving Large pledge crest" + crestFile + ":", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean saveAllyCrest(int newId, byte[] data)
|
||||
{
|
||||
File crestFile = new File(Config.DATAPACK_ROOT, "data/crests/AllyCrest_" + newId + ".bmp");
|
||||
try (FileOutputStream out = new FileOutputStream(crestFile))
|
||||
{
|
||||
out.write(data);
|
||||
_cacheAlly.getContentMap().put(newId, data);
|
||||
return true;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
_log.log(Level.INFO, "Error saving ally crest" + crestFile + ":", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected static class BmpFilter implements FileFilter
|
||||
{
|
||||
@Override
|
||||
public boolean accept(File file)
|
||||
{
|
||||
return (file.getName().endsWith(".bmp"));
|
||||
}
|
||||
}
|
||||
|
||||
protected static class OldPledgeFilter implements FileFilter
|
||||
{
|
||||
@Override
|
||||
public boolean accept(File file)
|
||||
{
|
||||
return (file.getName().startsWith("Pledge_"));
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("synthetic-access")
|
||||
private static class SingletonHolder
|
||||
{
|
||||
protected static final CrestCache _instance = new CrestCache();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
/*
|
||||
* This program is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later
|
||||
* version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jserver.gameserver.cache;
|
||||
|
||||
import javolution.context.ObjectFactory;
|
||||
import javolution.lang.Reusable;
|
||||
import javolution.util.FastCollection;
|
||||
import javolution.util.FastComparator;
|
||||
import javolution.util.FastList;
|
||||
import javolution.util.FastMap;
|
||||
|
||||
/**
|
||||
* @author Layane
|
||||
*/
|
||||
@SuppressWarnings(
|
||||
{
|
||||
"rawtypes"
|
||||
})
|
||||
public class FastMRUCache<K, V> extends FastCollection implements Reusable
|
||||
{
|
||||
/**
|
||||
* Comment for <code>serialVersionUID</code>
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final int DEFAULT_CAPACITY = 50;
|
||||
private static final int DEFAULT_FORGET_TIME = 300000; // 5 Minutes
|
||||
|
||||
private final FastMap<K, CacheNode> _cache = new FastMap<K, CacheNode>().setKeyComparator(FastComparator.DIRECT);
|
||||
private FastMap<K, V> _map;
|
||||
private final FastList<K> _mruList = new FastList<>();
|
||||
private int _cacheSize;
|
||||
private int _forgetTime;
|
||||
|
||||
class CacheNode
|
||||
{
|
||||
long _lastModified;
|
||||
V _node;
|
||||
|
||||
public CacheNode(V object)
|
||||
{
|
||||
_lastModified = System.currentTimeMillis();
|
||||
_node = object;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object)
|
||||
{
|
||||
return _node == object;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds the set factory.
|
||||
*/
|
||||
private static final ObjectFactory FACTORY = new ObjectFactory()
|
||||
{
|
||||
|
||||
@Override
|
||||
public Object create()
|
||||
{
|
||||
return new FastMRUCache();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanup(Object obj)
|
||||
{
|
||||
((FastMRUCache) obj).reset();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns a set allocated from the stack when executing in a {@link javolution.realtime.PoolContext PoolContext}).
|
||||
* @return a new, pre-allocated or recycled set instance.
|
||||
*/
|
||||
public static FastMRUCache newInstance()
|
||||
{
|
||||
return (FastMRUCache) FACTORY.object();
|
||||
}
|
||||
|
||||
public FastMRUCache()
|
||||
{
|
||||
this(new FastMap<K, V>(), DEFAULT_CAPACITY, DEFAULT_FORGET_TIME);
|
||||
}
|
||||
|
||||
public FastMRUCache(FastMap<K, V> map)
|
||||
{
|
||||
this(map, DEFAULT_CAPACITY, DEFAULT_FORGET_TIME);
|
||||
}
|
||||
|
||||
public FastMRUCache(FastMap<K, V> map, int max)
|
||||
{
|
||||
this(map, max, DEFAULT_FORGET_TIME);
|
||||
}
|
||||
|
||||
public FastMRUCache(FastMap<K, V> map, int max, int forgetTime)
|
||||
{
|
||||
_map = map;
|
||||
_cacheSize = max;
|
||||
_forgetTime = forgetTime;
|
||||
_map.setKeyComparator(FastComparator.DIRECT);
|
||||
}
|
||||
|
||||
// Implements Reusable.
|
||||
@Override
|
||||
public synchronized void reset()
|
||||
{
|
||||
_map.reset();
|
||||
_cache.reset();
|
||||
_mruList.reset();
|
||||
_map.setKeyComparator(FastComparator.DIRECT);
|
||||
_cache.setKeyComparator(FastComparator.DIRECT);
|
||||
}
|
||||
|
||||
public synchronized V get(K key)
|
||||
{
|
||||
V result;
|
||||
|
||||
if (!_cache.containsKey(key))
|
||||
{
|
||||
if (_mruList.size() >= _cacheSize)
|
||||
{
|
||||
|
||||
_cache.remove(_mruList.getLast());
|
||||
_mruList.removeLast();
|
||||
}
|
||||
|
||||
result = _map.get(key);
|
||||
|
||||
_cache.put(key, new CacheNode(result));
|
||||
_mruList.addFirst(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
CacheNode current = _cache.get(key);
|
||||
|
||||
if ((current._lastModified + _forgetTime) <= System.currentTimeMillis())
|
||||
{
|
||||
current._lastModified = System.currentTimeMillis();
|
||||
current._node = _map.get(key);
|
||||
_cache.put(key, current);
|
||||
}
|
||||
|
||||
_mruList.remove(key);
|
||||
_mruList.addFirst(key);
|
||||
|
||||
result = current._node;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized boolean remove(Object key)
|
||||
{
|
||||
_cache.remove(key);
|
||||
_mruList.remove(key);
|
||||
return _map.remove(key) == key;
|
||||
}
|
||||
|
||||
public FastMap<K, V> getContentMap()
|
||||
{
|
||||
return _map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size()
|
||||
{
|
||||
return _mruList.size();
|
||||
}
|
||||
|
||||
public int capacity()
|
||||
{
|
||||
return _cacheSize;
|
||||
}
|
||||
|
||||
public int getForgetTime()
|
||||
{
|
||||
return _forgetTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void clear()
|
||||
{
|
||||
_cache.clear();
|
||||
_mruList.clear();
|
||||
_map.clear();
|
||||
}
|
||||
|
||||
// Implements FastCollection abstract method.
|
||||
@Override
|
||||
public final Record head()
|
||||
{
|
||||
return _mruList.head();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Record tail()
|
||||
{
|
||||
return _mruList.tail();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Object valueOf(Record record)
|
||||
{
|
||||
return ((FastMap.Entry) record).getKey();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void delete(Record record)
|
||||
{
|
||||
remove(((FastMap.Entry) record).getKey());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,255 @@
|
||||
/*
|
||||
* This program is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later
|
||||
* version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jserver.gameserver.cache;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.io.FileInputStream;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javolution.util.FastMap;
|
||||
|
||||
import com.l2jserver.Config;
|
||||
import com.l2jserver.gameserver.util.Util;
|
||||
|
||||
/**
|
||||
* @author Layane
|
||||
*/
|
||||
public class HtmCache
|
||||
{
|
||||
private static Logger _log = Logger.getLogger(HtmCache.class.getName());
|
||||
|
||||
private final Map<Integer, String> _cache;
|
||||
|
||||
private int _loadedFiles;
|
||||
private long _bytesBuffLen;
|
||||
|
||||
public static HtmCache getInstance()
|
||||
{
|
||||
return SingletonHolder._instance;
|
||||
}
|
||||
|
||||
private HtmCache()
|
||||
{
|
||||
if (Config.LAZY_CACHE)
|
||||
{
|
||||
_cache = new FastMap<Integer, String>().shared();
|
||||
}
|
||||
else
|
||||
{
|
||||
_cache = new FastMap<>();
|
||||
}
|
||||
reload();
|
||||
}
|
||||
|
||||
public void reload()
|
||||
{
|
||||
reload(Config.DATAPACK_ROOT);
|
||||
}
|
||||
|
||||
public void reload(File f)
|
||||
{
|
||||
if (!Config.LAZY_CACHE)
|
||||
{
|
||||
_log.info("Html cache start...");
|
||||
parseDir(f);
|
||||
_log.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + getLoadedFiles() + " files loaded");
|
||||
}
|
||||
else
|
||||
{
|
||||
_cache.clear();
|
||||
_loadedFiles = 0;
|
||||
_bytesBuffLen = 0;
|
||||
_log.info("Cache[HTML]: Running lazy cache");
|
||||
}
|
||||
}
|
||||
|
||||
public void reloadPath(File f)
|
||||
{
|
||||
parseDir(f);
|
||||
_log.info("Cache[HTML]: Reloaded specified path.");
|
||||
}
|
||||
|
||||
public double getMemoryUsage()
|
||||
{
|
||||
return ((float) _bytesBuffLen / 1048576);
|
||||
}
|
||||
|
||||
public int getLoadedFiles()
|
||||
{
|
||||
return _loadedFiles;
|
||||
}
|
||||
|
||||
protected static class HtmFilter implements FileFilter
|
||||
{
|
||||
@Override
|
||||
public boolean accept(File file)
|
||||
{
|
||||
if (!file.isDirectory())
|
||||
{
|
||||
return (file.getName().endsWith(".htm") || file.getName().endsWith(".html"));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private void parseDir(File dir)
|
||||
{
|
||||
FileFilter filter = new HtmFilter();
|
||||
File[] files = dir.listFiles(filter);
|
||||
|
||||
for (File file : files)
|
||||
{
|
||||
if (!file.isDirectory())
|
||||
{
|
||||
loadFile(file);
|
||||
}
|
||||
else
|
||||
{
|
||||
parseDir(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String loadFile(File file)
|
||||
{
|
||||
final String relpath = Util.getRelativePath(Config.DATAPACK_ROOT, file);
|
||||
final int hashcode = relpath.hashCode();
|
||||
|
||||
final HtmFilter filter = new HtmFilter();
|
||||
|
||||
if (file.exists() && filter.accept(file) && !file.isDirectory())
|
||||
{
|
||||
String content;
|
||||
try (FileInputStream fis = new FileInputStream(file);
|
||||
BufferedInputStream bis = new BufferedInputStream(fis))
|
||||
{
|
||||
int bytes = bis.available();
|
||||
byte[] raw = new byte[bytes];
|
||||
|
||||
bis.read(raw);
|
||||
content = new String(raw, "UTF-8");
|
||||
content = content.replaceAll("\r\n", "\n");
|
||||
|
||||
String oldContent = _cache.get(hashcode);
|
||||
|
||||
if (oldContent == null)
|
||||
{
|
||||
_bytesBuffLen += bytes;
|
||||
_loadedFiles++;
|
||||
}
|
||||
else
|
||||
{
|
||||
_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + bytes;
|
||||
}
|
||||
|
||||
_cache.put(hashcode, content);
|
||||
|
||||
return content;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_log.log(Level.WARNING, "Problem with htm file " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getHtmForce(String prefix, String path)
|
||||
{
|
||||
String content = getHtm(prefix, path);
|
||||
|
||||
if (content == null)
|
||||
{
|
||||
content = "<html><body>My text is missing:<br>" + path + "</body></html>";
|
||||
_log.warning("Cache[HTML]: Missing HTML page: " + path);
|
||||
}
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
public String getHtm(String prefix, String path)
|
||||
{
|
||||
String newPath = null;
|
||||
String content;
|
||||
if ((prefix != null) && !prefix.isEmpty())
|
||||
{
|
||||
newPath = prefix + path;
|
||||
content = getHtm(newPath);
|
||||
if (content != null)
|
||||
{
|
||||
return content;
|
||||
}
|
||||
}
|
||||
|
||||
content = getHtm(path);
|
||||
if ((content != null) && (newPath != null))
|
||||
{
|
||||
_cache.put(newPath.hashCode(), content);
|
||||
}
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
private String getHtm(String path)
|
||||
{
|
||||
if ((path == null) || path.isEmpty())
|
||||
{
|
||||
return ""; // avoid possible NPE
|
||||
}
|
||||
|
||||
final int hashCode = path.hashCode();
|
||||
String content = _cache.get(hashCode);
|
||||
|
||||
if (Config.LAZY_CACHE && (content == null))
|
||||
{
|
||||
content = loadFile(new File(Config.DATAPACK_ROOT, path));
|
||||
}
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
public boolean contains(String path)
|
||||
{
|
||||
return _cache.containsKey(path.hashCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an HTM exists and can be loaded
|
||||
* @param path The path to the HTM
|
||||
*/
|
||||
public boolean isLoadable(String path)
|
||||
{
|
||||
File file = new File(path);
|
||||
HtmFilter filter = new HtmFilter();
|
||||
|
||||
if (file.exists() && filter.accept(file) && !file.isDirectory())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@SuppressWarnings("synthetic-access")
|
||||
private static class SingletonHolder
|
||||
{
|
||||
protected static final HtmCache _instance = new HtmCache();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* This program is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later
|
||||
* version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jserver.gameserver.cache;
|
||||
|
||||
import javolution.util.FastMap;
|
||||
|
||||
import com.l2jserver.Config;
|
||||
import com.l2jserver.gameserver.ThreadPoolManager;
|
||||
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
|
||||
|
||||
/**
|
||||
* @author -Nemesiss-
|
||||
*/
|
||||
public class WarehouseCacheManager
|
||||
{
|
||||
protected final FastMap<L2PcInstance, Long> _cachedWh;
|
||||
protected final long _cacheTime;
|
||||
|
||||
public static WarehouseCacheManager getInstance()
|
||||
{
|
||||
return SingletonHolder._instance;
|
||||
}
|
||||
|
||||
private WarehouseCacheManager()
|
||||
{
|
||||
_cacheTime = Config.WAREHOUSE_CACHE_TIME * 60000L; // 60*1000 = 60000
|
||||
_cachedWh = new FastMap<L2PcInstance, Long>().shared();
|
||||
ThreadPoolManager.getInstance().scheduleAiAtFixedRate(new CacheScheduler(), 120000, 60000);
|
||||
}
|
||||
|
||||
public void addCacheTask(L2PcInstance pc)
|
||||
{
|
||||
_cachedWh.put(pc, System.currentTimeMillis());
|
||||
}
|
||||
|
||||
public void remCacheTask(L2PcInstance pc)
|
||||
{
|
||||
_cachedWh.remove(pc);
|
||||
}
|
||||
|
||||
public class CacheScheduler implements Runnable
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
long cTime = System.currentTimeMillis();
|
||||
for (L2PcInstance pc : _cachedWh.keySet())
|
||||
{
|
||||
if ((cTime - _cachedWh.get(pc)) > _cacheTime)
|
||||
{
|
||||
pc.clearWarehouse();
|
||||
_cachedWh.remove(pc);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("synthetic-access")
|
||||
private static class SingletonHolder
|
||||
{
|
||||
protected static final WarehouseCacheManager _instance = new WarehouseCacheManager();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user