FastMRUCache.java 4.7 KB

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