FastMRUCache.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (C) 2004-2013 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 javolution.context.ObjectFactory;
  21. import javolution.lang.Reusable;
  22. import javolution.util.FastCollection;
  23. import javolution.util.FastComparator;
  24. import javolution.util.FastList;
  25. import javolution.util.FastMap;
  26. /**
  27. * @author Layane
  28. * @param <K>
  29. * @param <V>
  30. */
  31. @SuppressWarnings("rawtypes")
  32. public class FastMRUCache<K, V> extends FastCollection implements Reusable
  33. {
  34. private static final long serialVersionUID = 1L;
  35. private static final int DEFAULT_CAPACITY = 50;
  36. private static final int DEFAULT_FORGET_TIME = 300000; // 5 Minutes
  37. private final FastMap<K, CacheNode> _cache = new FastMap<K, CacheNode>().setKeyComparator(FastComparator.DIRECT);
  38. private FastMap<K, V> _map;
  39. private final FastList<K> _mruList = new FastList<>();
  40. private int _cacheSize;
  41. private int _forgetTime;
  42. class CacheNode
  43. {
  44. long _lastModified;
  45. V _node;
  46. public CacheNode(V object)
  47. {
  48. _lastModified = System.currentTimeMillis();
  49. _node = object;
  50. }
  51. @Override
  52. public boolean equals(Object object)
  53. {
  54. return _node == object;
  55. }
  56. }
  57. /**
  58. * Holds the set factory.
  59. */
  60. private static final ObjectFactory FACTORY = new ObjectFactory()
  61. {
  62. @Override
  63. public Object create()
  64. {
  65. return new FastMRUCache();
  66. }
  67. @Override
  68. public void cleanup(Object obj)
  69. {
  70. ((FastMRUCache) obj).reset();
  71. }
  72. };
  73. /**
  74. * Returns a set allocated from the stack when executing in a
  75. * @return a new, pre-allocated or recycled set instance.
  76. */
  77. public static FastMRUCache newInstance()
  78. {
  79. return (FastMRUCache) FACTORY.object();
  80. }
  81. public FastMRUCache()
  82. {
  83. this(new FastMap<K, V>(), DEFAULT_CAPACITY, DEFAULT_FORGET_TIME);
  84. }
  85. public FastMRUCache(FastMap<K, V> map)
  86. {
  87. this(map, DEFAULT_CAPACITY, DEFAULT_FORGET_TIME);
  88. }
  89. public FastMRUCache(FastMap<K, V> map, int max)
  90. {
  91. this(map, max, DEFAULT_FORGET_TIME);
  92. }
  93. public FastMRUCache(FastMap<K, V> map, int max, int forgetTime)
  94. {
  95. _map = map;
  96. _cacheSize = max;
  97. _forgetTime = forgetTime;
  98. _map.setKeyComparator(FastComparator.DIRECT);
  99. }
  100. // Implements Reusable.
  101. @Override
  102. public synchronized void reset()
  103. {
  104. _map.reset();
  105. _cache.reset();
  106. _mruList.reset();
  107. _map.setKeyComparator(FastComparator.DIRECT);
  108. _cache.setKeyComparator(FastComparator.DIRECT);
  109. }
  110. public synchronized V get(K key)
  111. {
  112. V result;
  113. if (!_cache.containsKey(key))
  114. {
  115. if (_mruList.size() >= _cacheSize)
  116. {
  117. _cache.remove(_mruList.getLast());
  118. _mruList.removeLast();
  119. }
  120. result = _map.get(key);
  121. _cache.put(key, new CacheNode(result));
  122. _mruList.addFirst(key);
  123. }
  124. else
  125. {
  126. CacheNode current = _cache.get(key);
  127. if ((current._lastModified + _forgetTime) <= System.currentTimeMillis())
  128. {
  129. current._lastModified = System.currentTimeMillis();
  130. current._node = _map.get(key);
  131. _cache.put(key, current);
  132. }
  133. _mruList.remove(key);
  134. _mruList.addFirst(key);
  135. result = current._node;
  136. }
  137. return result;
  138. }
  139. @Override
  140. public synchronized boolean remove(Object key)
  141. {
  142. _cache.remove(key);
  143. _mruList.remove(key);
  144. return _map.remove(key) == key;
  145. }
  146. public FastMap<K, V> getContentMap()
  147. {
  148. return _map;
  149. }
  150. @Override
  151. public int size()
  152. {
  153. return _mruList.size();
  154. }
  155. public int capacity()
  156. {
  157. return _cacheSize;
  158. }
  159. public int getForgetTime()
  160. {
  161. return _forgetTime;
  162. }
  163. @Override
  164. public synchronized void clear()
  165. {
  166. _cache.clear();
  167. _mruList.clear();
  168. _map.clear();
  169. }
  170. // Implements FastCollection abstract method.
  171. @Override
  172. public final Record head()
  173. {
  174. return _mruList.head();
  175. }
  176. @Override
  177. public final Record tail()
  178. {
  179. return _mruList.tail();
  180. }
  181. @Override
  182. public final Object valueOf(Record record)
  183. {
  184. return ((FastMap.Entry) record).getKey();
  185. }
  186. @Override
  187. public final void delete(Record record)
  188. {
  189. remove(((FastMap.Entry) record).getKey());
  190. }
  191. }