FastMRUCache.java 4.7 KB

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