FastMRUCache.java 5.3 KB

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