WarehouseCacheManager.java 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. *
  23. * @author -Nemesiss-
  24. */
  25. public class WarehouseCacheManager
  26. {
  27. protected final Map<L2PcInstance, Long> _cachedWh = new L2FastMap<>(true);
  28. protected final long _cacheTime = Config.WAREHOUSE_CACHE_TIME * 60000L;
  29. protected WarehouseCacheManager()
  30. {
  31. ThreadPoolManager.getInstance().scheduleAiAtFixedRate(new CacheScheduler(), 120000, 60000);
  32. }
  33. public void addCacheTask(L2PcInstance pc)
  34. {
  35. _cachedWh.put(pc, System.currentTimeMillis());
  36. }
  37. public void remCacheTask(L2PcInstance pc)
  38. {
  39. _cachedWh.remove(pc);
  40. }
  41. public class CacheScheduler implements Runnable
  42. {
  43. @Override
  44. public void run()
  45. {
  46. long cTime = System.currentTimeMillis();
  47. for (L2PcInstance pc : _cachedWh.keySet())
  48. {
  49. if (cTime - _cachedWh.get(pc) > _cacheTime)
  50. {
  51. pc.clearWarehouse();
  52. _cachedWh.remove(pc);
  53. }
  54. }
  55. }
  56. }
  57. public static WarehouseCacheManager getInstance()
  58. {
  59. return SingletonHolder._instance;
  60. }
  61. private static class SingletonHolder
  62. {
  63. protected static final WarehouseCacheManager _instance = new WarehouseCacheManager();
  64. }
  65. }