Android提供的LruCache类简介

  1. */

  2. public LruCache(int maxSize) {

  3. if (maxSize <= 0) {

  4. throw new IllegalArgumentException(“maxSize <= 0”);

  5. }

  6. this.maxSize = maxSize;

  7. this.map = new LinkedHashMap<K, V>(0, 0.75f, true);

  8. }

  9. /**

  10. * Returns the value for {@code key} if it exists in the cache or can be

  11. * created by {@code #create}. If a value was returned, it is moved to the

  12. * head of the queue. This returns null if a value is not cached and cannot

  13. * be created. 通过key返回相应的item,或者创建返回相应的item。相应的item会移动到队列的头部,

  14. * 如果item的value没有被cache或者不能被创建,则返回null。

  15. */

  16. public final V get(K key) {

  17. if (key == null) {

  18. throw new NullPointerException(“key == null”);

  19. }

  20. V mapValue;

  21. synchronized (this) {

  22. mapValue = map.get(key);

  23. if (mapValue != null) {

  24. hitCount++;  //命中

  25. return mapValue;

  26. }

  27. missCount++;  //丢失

  28. }

  29. /*

  30. * Attempt to create a value. This may take a long time, and the map

  31. * may be different when create() returns. If a conflicting value was

  32. * added to the map while create() was working, we leave that value in

  33. * the map and release the created value.

  34. * 如果丢失了就试图创建一个item

  35. */

  36. V createdValue = create(key);

  37. if (createdValue == null) {

  38. return null;

  39. }

  40. synchronized (this) {

  41. createCount++;//创建++

  42. mapValue = map.put(key, createdValue);

  43. if (mapValue != null) {

  44. // There was a conflict so undo that last put

  45. //如果前面存在oldValue,那么撤销put()

  46. map.put(key, mapValue);

  47. } else {

  48. size += safeSizeOf(key, createdValue);

  49. }

  50. }

  51. if (mapValue != null) {

  52. entryRemoved(false, key, createdValue, mapValue);

  53. return mapValue;

  54. } else {

  55. trimToSize(maxSize);

  56. return createdValue;

  57. }

  58. }

  59. /**

  60. * Caches {@code value} for {@code key}. The value is moved to the head of

  61. * the queue.

  62. *

  63. * @return the previous value mapped by {@code key}.

  64. */

  65. public final V put(K key, V value) {

  66. if (key == null || value == null) {

  67. throw new NullPointerException(“key == null || value == null”);

  68. }

  69. V previous;

  70. synchronized (this) {

  71. putCount++;

  72. size += safeSizeOf(key, value);

  73. previous = map.put(key, value);

  74. if (previous != null) {  //返回的先前的value值

  75. size -= safeSizeOf(key, previous);

  76. }

  77. }

  78. if (previous != null) {

  79. entryRemoved(false, key, previous, value);

  80. }

  81. trimToSize(maxSize);

  82. return previous;

  83. }

  84. /**

  85. * @param maxSize the maximum size of the cache before returning. May be -1

  86. *     to evict even 0-sized elements.

  87. *  清空cache空间

  88. */

  89. private void trimToSize(int maxSize) {

  90. while (true) {

  91. K key;

  92. V value;

  93. synchronized (this) {

  94. if (size < 0 || (map.isEmpty() && size != 0)) {

  95. throw new IllegalStateException(getClass().getName()

  96. + “.sizeOf() is reporting inconsistent results!”);

  97. }

  98. if (size <= maxSize) {

  99. break;

  100. }

  101. Map.Entry<K, V> toEvict = map.eldest();

  102. if (toEvict == null) {

  103. break;

  104. }

  105. key = toEvict.getKey();

  106. value = toEvict.getValue();

  107. map.remove(key);

  108. size -= safeSizeOf(key, value);

  109. evictionCount++;

  110. }

  111. entryRemoved(true, key, value, null);

  112. }

  113. }

  114. /**

  115. * Removes the entry for {@code key} if it exists.

  116. * 删除key相应的cache项,返回相应的value

  117. * @return the previous value mapped by {@code key}.

  118. */

  119. public final V remove(K key) {

  120. if (key == null) {

  121. throw new NullPointerException(“key == null”);

  122. }

  123. V previous;

  124. synchronized (this) {

  125. previous = map.remove(key);

  126. if (previous != null) {

  127. size -= safeSizeOf(key, previous);

  128. }

  129. }

  130. if (previous != null) {

  131. entryRemoved(false, key, previous, null);

  132. }

  133. return previous;

  134. }

  135. /**

  136. * Called for entries that have been evicted or removed. This method is

  137. * invoked when a value is evicted to make space, removed by a call to

  138. * {@link #remove}, or replaced by a call to {@link #put}. The default

  139. * implementation does nothing.

  140. * 当item被回收或者删掉时调用。改方法当value被回收释放存储空间时被remove调用,

  141. * 或者替换item值时put调用,默认实现什么都没做。

  142. The method is called without synchronization: other threads may

  143. * access the cache while this method is executing.

  144. *

  145. * @param evicted true if the entry is being removed to make space, false

  146. *     if the removal was caused by a {@link #put} or {@link #remove}.

  147. * true—为释放空间被删除;false—put或remove导致

  148. * @param newValue the new value for {@code key}, if it exists. If non-null,

  149. *     this removal was caused by a {@link #put}. Otherwise it was caused by

  150. *     an eviction or a {@link #remove}.

  151. */

  152. protected void entryRemoved(boolean evicted, K key, V oldValue, V newValue) {}

  153. /**

  154. * Called after a cache miss to compute a value for the corresponding key.

  155. * Returns the computed value or null if no value can be computed. The

  156. * default implementation returns null.

  157. * 当某Item丢失时会调用到,返回计算的相应的value或者null

  158. The method is called without synchronization: other threads may

  159. * access the cache while this method is executing.

  160. *

  161. If a value for {@code key} exists in the cache when this method

  162. * returns, the created value will be released with {@link #entryRemoved}

  163. * and discarded. This can occur when multiple threads request the same key

  164. * at the same time (causing multiple values to be created), or when one

  165. * thread calls {@link #put} while another is creating a value for the same

  166. * key.

  167. */

  168. protected V create(K key) {

  169. return null;

  170. }

  171. private int safeSizeOf(K key, V value) {

  172. int result = sizeOf(key, value);

  173. if (result < 0) {

  174. throw new IllegalStateException("Negative size: " + key + “=” + value);

  175. }

  176. return result;

  177. }

  178. /**

  179. * Returns the size of the entry for {@code key} and {@code value} in

  180. * user-defined units.  The default implementation returns 1 so that size

  181. * is the number of entries and max size is the maximum number of entries.

  182. * 返回用户定义的item的大小,默认返回1代表item的数量,最大size就是最大item值

  183. An entry’s size must not change while it is in the cache.

  184. */

  185. protected int sizeOf(K key, V value) {

  186. return 1;

  187. }

  188. /**

  189. * Clear the cache, calling {@link #entryRemoved} on each removed entry.

  190. * 清空cacke

  191. */

  192. public final void evictAll() {

  193. trimToSize(-1); // -1 will evict 0-sized elements

  194. }

  195. /**

  196. * For caches that do not override {@link #sizeOf}, this returns the number

  197. * of entries in the cache. For all other caches, this returns the sum of

  198. * the sizes of the entries in this cache.

  199. */

  200. public synchronized final int size() {

  201. return size;

  202. }

  203. /**

  204. * For caches that do not override {@link #sizeOf}, this returns the maximum

  205. * number of entries in the cache. For all other caches, this returns the

  206. * maximum sum of the sizes of the entries in this cache.

  207. */

  208. public synchronized final int maxSize() {

  209. return maxSize;

  210. }

  211. /**

  212. * Returns the number of times {@link #get} returned a value that was

  213. * already present in the cache.

  214. */

  215. public synchronized final int hitCount() {

  216. return hitCount;

  217. }

  218. /**

  219. * Returns the number of times {@link #get} returned null or required a new

  220. * value to be created.

  221. */

  222. public synchronized final int missCount() {

  223. return missCount;

  224. }

  225. /**

  226. * Returns the number of times {@link #create(Object)} returned a value.

  227. */

  228. public synchronized final int createCount() {

  229. return createCount;

  230. }

  231. /**

  232. * Returns the number of times {@link #put} was called.

  233. */

  234. public synchronized final int putCount() {

  235. return putCount;

  236. }

  237. /**

  238. * Returns the number of values that have been evicted.

  239. * 返回被回收的数量

  240. */

  241. public synchronized final int evictionCount() {

  242. return evictionCount;

  243. }

  244. /**

  245. * Returns a copy of the current contents of the cache, ordered from least

  246. * recently accessed to most recently accessed. 返回当前cache的副本,从最近最少访问到最多访问

  247. */

  248. public synchronized final Map<K, V> snapshot() {

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值