FastMRUCache.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. return new FastMRUCache();
  62. }
  63. @Override
  64. public void cleanup(Object obj) {
  65. ((FastMRUCache) obj).reset();
  66. }
  67. };
  68. /**
  69. * Returns a set allocated from the stack when executing in a
  70. * {@link javolution.realtime.PoolContext PoolContext}).
  71. *
  72. * @return a new, pre-allocated or recycled set instance.
  73. */
  74. public static FastMRUCache newInstance() {
  75. return (FastMRUCache) FACTORY.object();
  76. }
  77. public FastMRUCache()
  78. {
  79. this(new FastMap<K,V>(),DEFAULT_CAPACITY,DEFAULT_FORGET_TIME);
  80. }
  81. public FastMRUCache (FastMap<K,V> map)
  82. {
  83. this(map,DEFAULT_CAPACITY,DEFAULT_FORGET_TIME);
  84. }
  85. public FastMRUCache (FastMap<K,V> map, int max)
  86. {
  87. this(map,max,DEFAULT_FORGET_TIME);
  88. }
  89. public FastMRUCache (FastMap<K,V> map, int max, int forgetTime)
  90. {
  91. _map = map;
  92. _cacheSize = max;
  93. _forgetTime = forgetTime;
  94. _map.setKeyComparator(FastComparator.DIRECT);
  95. }
  96. // Implements Reusable.
  97. @Override
  98. public synchronized void reset() {
  99. _map.reset();
  100. _cache.reset();
  101. _mruList.reset();
  102. _map.setKeyComparator(FastComparator.DIRECT);
  103. _cache.setKeyComparator(FastComparator.DIRECT);
  104. }
  105. public synchronized V get(K key)
  106. {
  107. V result;
  108. if (!_cache.containsKey(key))
  109. {
  110. if (_mruList.size() >= _cacheSize)
  111. {
  112. _cache.remove(_mruList.getLast());
  113. _mruList.removeLast();
  114. }
  115. result = _map.get(key);
  116. _cache.put(key, new CacheNode(result));
  117. _mruList.addFirst(key);
  118. }
  119. else
  120. {
  121. CacheNode current = _cache.get(key);
  122. if ((current._lastModified + _forgetTime) <= System.currentTimeMillis())
  123. {
  124. current._lastModified = System.currentTimeMillis();
  125. current._node = _map.get(key);
  126. _cache.put(key,current);
  127. }
  128. _mruList.remove(key);
  129. _mruList.addFirst(key);
  130. result = current._node;
  131. }
  132. return result;
  133. }
  134. @Override
  135. public synchronized boolean remove(Object key)
  136. {
  137. _cache.remove(key);
  138. _mruList.remove(key);
  139. return _map.remove(key) == key;
  140. }
  141. public FastMap<K,V> getContentMap()
  142. {
  143. return _map;
  144. }
  145. @Override
  146. public int size()
  147. {
  148. return _mruList.size();
  149. }
  150. public int capacity()
  151. {
  152. return _cacheSize;
  153. }
  154. public int getForgetTime()
  155. {
  156. return _forgetTime;
  157. }
  158. @Override
  159. public synchronized void clear()
  160. {
  161. _cache.clear();
  162. _mruList.clear();
  163. _map.clear();
  164. }
  165. // Implements FastCollection abstract method.
  166. @Override
  167. public final Record head() {
  168. return _mruList.head();
  169. }
  170. @Override
  171. public final Record tail() {
  172. return _mruList.tail();
  173. }
  174. @Override
  175. public final Object valueOf(Record record) {
  176. return ((FastMap.Entry) record).getKey();
  177. }
  178. @Override
  179. public final void delete(Record record) {
  180. remove(((FastMap.Entry) record).getKey());
  181. }
  182. }