FastMRUCache.java 4.7 KB

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