WarehouseCacheManager.java 2.1 KB

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