WarehouseCacheManager.java 2.0 KB

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