WarehouseCacheManager.java 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 javolution.util.FastMap;
  17. import com.l2jserver.Config;
  18. import com.l2jserver.gameserver.ThreadPoolManager;
  19. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  20. /**
  21. *
  22. * @author -Nemesiss-
  23. */
  24. public class WarehouseCacheManager
  25. {
  26. protected final FastMap<L2PcInstance, Long> _cachedWh;
  27. protected final long _cacheTime;
  28. public static WarehouseCacheManager getInstance()
  29. {
  30. return SingletonHolder._instance;
  31. }
  32. private WarehouseCacheManager()
  33. {
  34. _cacheTime = Config.WAREHOUSE_CACHE_TIME * 60000L; // 60*1000 = 60000
  35. _cachedWh = new FastMap<L2PcInstance, Long>().shared();
  36. ThreadPoolManager.getInstance().scheduleAiAtFixedRate(new CacheScheduler(), 120000, 60000);
  37. }
  38. public void addCacheTask(L2PcInstance pc)
  39. {
  40. _cachedWh.put(pc, System.currentTimeMillis());
  41. }
  42. public void remCacheTask(L2PcInstance pc)
  43. {
  44. _cachedWh.remove(pc);
  45. }
  46. public class CacheScheduler implements Runnable
  47. {
  48. @Override
  49. public void run()
  50. {
  51. long cTime = System.currentTimeMillis();
  52. for (L2PcInstance pc : _cachedWh.keySet())
  53. {
  54. if (cTime - _cachedWh.get(pc) > _cacheTime)
  55. {
  56. pc.clearWarehouse();
  57. _cachedWh.remove(pc);
  58. }
  59. }
  60. }
  61. }
  62. @SuppressWarnings("synthetic-access")
  63. private static class SingletonHolder
  64. {
  65. protected static final WarehouseCacheManager _instance = new WarehouseCacheManager();
  66. }
  67. }