WarehouseCacheManager.java 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.cache;
  16. import java.util.Map;
  17. import com.l2jserver.Config;
  18. import com.l2jserver.gameserver.ThreadPoolManager;
  19. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  20. import com.l2jserver.util.L2FastMap;
  21. /**
  22. * @author -Nemesiss-
  23. */
  24. public class WarehouseCacheManager
  25. {
  26. protected final Map<L2PcInstance, Long> _cachedWh = new L2FastMap<>(true);
  27. protected final long _cacheTime = Config.WAREHOUSE_CACHE_TIME * 60000L;
  28. protected WarehouseCacheManager()
  29. {
  30. ThreadPoolManager.getInstance().scheduleAiAtFixedRate(new CacheScheduler(), 120000, 60000);
  31. }
  32. public void addCacheTask(L2PcInstance pc)
  33. {
  34. _cachedWh.put(pc, System.currentTimeMillis());
  35. }
  36. public void remCacheTask(L2PcInstance pc)
  37. {
  38. _cachedWh.remove(pc);
  39. }
  40. public class CacheScheduler implements Runnable
  41. {
  42. @Override
  43. public void run()
  44. {
  45. long cTime = System.currentTimeMillis();
  46. for (L2PcInstance pc : _cachedWh.keySet())
  47. {
  48. if ((cTime - _cachedWh.get(pc)) > _cacheTime)
  49. {
  50. pc.clearWarehouse();
  51. _cachedWh.remove(pc);
  52. }
  53. }
  54. }
  55. }
  56. public static WarehouseCacheManager getInstance()
  57. {
  58. return SingletonHolder._instance;
  59. }
  60. private static class SingletonHolder
  61. {
  62. protected static final WarehouseCacheManager _instance = new WarehouseCacheManager();
  63. }
  64. }