ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android提供的LruCache类简介,html5移动开发即学即用

2022-02-02 14:34:06  阅读:161  来源: 互联网

标签:map value maxSize html5 key Android null 即学 previous


  1. /** Size of this cache in units. Not necessarily the number of elements. */

  2. private int size; //已经存储的大小

  3. private int maxSize; //规定的最大存储空间

  4. private int putCount;  //put的次数

  5. private int createCount;  //create的次数

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

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

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

  9. /**

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

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

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

  13. */

  14. public LruCache(int maxSize) {

  15. if (maxSize <= 0) {

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

  17. }

  18. this.maxSize = maxSize;

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

  20. }

  21. /**

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

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

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

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

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

  27. */

  28. public final V get(K key) {

  29. if (key == null) {

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

  31. }

  32. V mapValue;

  33. synchronized (this) {

  34. mapValue = map.get(key);

  35. if (mapValue != null) {

  36. hitCount++;  //命中

  37. return mapValue;

  38. }

  39. missCount++;  //丢失

  40. }

  41. /*

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

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

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

  45. * the map and release the created value.

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

  47. */

  48. V createdValue = create(key);

  49. if (createdValue == null) {

  50. return null;

  51. }

  52. synchronized (this) {

  53. createCount++;//创建++

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

  55. if (mapValue != null) {

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

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

  58. map.put(key, mapValue);

  59. } else {

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

  61. }

  62. }

  63. if (mapValue != null) {

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

  65. return mapValue;

  66. } else {

  67. trimToSize(maxSize);

  68. return createdValue;

  69. }

  70. }

  71. /**

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

  73. * the queue.

  74. *

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

  76. */

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

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

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

  80. }

  81. V previous;

  82. synchronized (this) {

  83. putCount++;

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

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

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

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

  88. }

  89. }

  90. if (previous != null) {

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

  92. }

  93. trimToSize(maxSize);

  94. return previous;

  95. }

  96. /**

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

  98. *     to evict even 0-sized elements.

  99. *  清空cache空间

  100. */

  101. private void trimToSize(int maxSize) {

  102. while (true) {

  103. K key;

  104. V value;

  105. synchronized (this) {

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

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

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

  109. }

  110. if (size <= maxSize) {

  111. break;

  112. }

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

  114. if (toEvict == null) {

  115. break;

  116. }

  117. key = toEvict.getKey();

  118. value = toEvict.getValue();

  119. map.remove(key);

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

  121. evictionCount++;

  122. }

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

  124. }

  125. }

  126. /**

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

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

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

  130. */

  131. public final V remove(K key) {

  132. if (key == null) {

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

  134. }

  135. V previous;

  136. synchronized (this) {

  137. previous = map.remove(key);

  138. if (previous != null) {

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

  140. }

  141. }

  142. if (previous != null) {

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

  144. }

  145. return previous;

  146. }

  147. /**

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

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

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

  151. * implementation does nothing.

最后

如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。

最后针对Android程序员,我这边给大家整理了一些资料,包括不限于高级UI、性能优化、移动架构师、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter等全方面的Android进阶实践技术;希望能帮助到大家,也节省大家在网上搜索资料的时间来学习,也可以分享动态给身边好友一起学习!

需要资料的朋友可以点击我的GitHub免费领取

包括不限于高级UI、性能优化、移动架构师、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter等全方面的Android进阶实践技术;希望能帮助到大家,也节省大家在网上搜索资料的时间来学习,也可以分享动态给身边好友一起学习!

需要资料的朋友可以点击我的GitHub免费领取

标签:map,value,maxSize,html5,key,Android,null,即学,previous
来源: https://blog.csdn.net/wa32saa/article/details/122769380

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有