Android提供的LruCache类简介

  1. private int evictionCount;  //回收的次数

  2. private int hitCount;  //命中的次数

  3. private int missCount;  //丢失的次数

  4. /**

  5. * @param maxSize for caches that do not override {@link #sizeOf}, this is

  6. *     the maximum number of entries in the cache. For all other caches,

  7. *     this is the maximum sum of the sizes of the entries in this cache.

  8. */

  9. public LruCache(int maxSize) {

  10. if (maxSize <= 0) {

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

  12. }

  13. this.maxSize = maxSize;

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

  15. }

  16. /**

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

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

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

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

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

  22. */

  23. public final V get(K key) {

  24. if (key == null) {

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

  26. }

  27. V mapValue;

  28. synchronized (this) {

  29. mapValue = map.get(key);

  30. if (mapValue != null) {

  31. hitCount++;  //命中

  32. return mapValue;

  33. }

  34. missCount++;  //丢失

  35. }

  36. /*

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

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

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

  40. * the map and release the created value.

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

  42. */

  43. V createdValue = create(key);

  44. if (createdValue == null) {

  45. return null;

  46. }

  47. synchronized (this) {

  48. createCount++;//创建++

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

  50. if (mapValue != null) {

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

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

  53. map.put(key, mapValue);

  54. } else {

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

  56. }

  57. }

  58. if (mapValue != null) {

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

  60. return mapValue;

  61. } else {

  62. trimToSize(maxSize);

  63. return createdValue;

  64. }

  65. }

  66. /**

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

  68. * the queue.

  69. *

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

  71. */

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

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

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

  75. }

  76. V previous;

  77. synchronized (this) {

  78. putCount++;

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

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

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

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

  83. }

  84. }

  85. if (previous != null) {

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

  87. }

  88. trimToSize(maxSize);

  89. return previous;

  90. }

  91. /**

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

  93. *     to evict even 0-sized elements.

  94. *  清空cache空间

  95. */

  96. private void trimToSize(int maxSize) {

  97. while (true) {

  98. K key;

  99. V value;

  100. synchronized (this) {

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

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

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

  104. }

  105. if (size <= maxSize) {

  106. break;

  107. }

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

  109. if (toEvict == null) {

  110. break;

  111. }

  112. key = toEvict.getKey();

  113. value = toEvict.getValue();

  114. map.remove(key);

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

  116. evictionCount++;

  117. }

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

  119. }

  120. }

  121. /**

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

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

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

  125. */

  126. public final V remove(K key) {

  127. if (key == null) {

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

  129. }

  130. V previous;

  131. synchronized (this) {

  132. previous = map.remove(key);

  133. if (previous != null) {

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

  135. }

  136. }

  137. if (previous != null) {

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

  139. }

  140. return previous;

  141. }

  142. /**

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

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

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

  146. * implementation does nothing.

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

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

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

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

  151. *

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

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

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

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

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

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

  158. */

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

  160. /**

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

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

  163. * default implementation returns null.

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

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

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

  167. *

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

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

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

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

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

  173. * key.

  174. */

  175. protected V create(K key) {

  176. return null;

  177. }

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

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

  180. if (result < 0) {

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

  182. }

  183. return result;

  184. }

  185. /**

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

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

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

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

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

  191. */

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

  193. return 1;

  194. }

  195. /**

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

  197. * 清空cacke

  198. */

  199. public final void evictAll() {

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

  201. }

  202. /**

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

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

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

  206. */

  207. public synchronized final int size() {

  208. return size;

  209. }

  210. /**

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

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

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

  214. */

  215. public synchronized final int maxSize() {

  216. return maxSize;

  217. }

  218. /**

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

  220. * already present in the cache.

  221. */

  222. public synchronized final int hitCount() {

  223. return hitCount;

  224. }

  225. /**

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

  227. * value to be created.

  228. */

  229. public synchronized final int missCount() {

  230. return missCount;

  231. }

  232. /**

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

  234. */

  235. public synchronized final int createCount() {

  236. return createCount;

  237. }

  238. /**

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

  240. */

  241. public synchronized final int putCount() {

  242. return putCount;

  243. }

  244. /**

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

  246. * 返回被回收的数量

  247. */

  248. public synchronized final int evictionCount() {

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值