当前位置: 首页 > news >正文

做冰饮视频网站要怎么做网络推广

做冰饮视频网站,要怎么做网络推广,西安企业建站公司,长沙公司建设网站文章目录 Android LruCache源码分析概述LruCache和LinkedHashMap关系源码分析属性写入数据读取数据删除缓存 Android LruCache源码分析 概述 LruCache(Least Recently Used Cache,最近最少使用缓存)是 Android 中的一种缓存机制。 根据数据…

文章目录

  • Android LruCache源码分析
    • 概述
    • LruCache和LinkedHashMap关系
    • 源码分析
      • 属性
      • 写入数据
      • 读取数据
      • 删除缓存

Android LruCache源码分析

概述

LruCache(Least Recently Used Cache,最近最少使用缓存)是 Android 中的一种缓存机制。

根据数据的使用频率淘汰减少使用的数据,当需要缓存新数据时,如果缓存已满,LruCache 会淘汰最近最少使用的数据,腾出空间给新数据。

img

LruCache和LinkedHashMap关系

LruCache 内部使用的是 LinkedHashMap(链式哈希表),这是因为 LinkedHashMap 的构造函数里有个布尔参数 accessOrder,当它为 true 时,LinkedHashMap 会以访问顺序的方式排列元素,如下:

Map<Integer, Integer> map = new LinkedHashMap<>(5, 0.75F, true);
map.put(1, 1);
map.put(2, 2);
map.put(3, 3);
map.put(4, 4);
map.put(5, 5);
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {System.out.println(entry.getValue());
}/** 1* 2* 3* 4* 5*/
// 访问2个元素
map.get(3); 
map.get(4);
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {System.out.println(entry.getValue());
}/** 1* 2* 5* 3* 4*/

最近访问的2个元素被移动到尾部,LruCache 也是从尾部访问数据,在表头删除数据。

源码分析

属性

public class LruCache<K, V> {private final LinkedHashMap<K, V> map;   // 当前缓存大小private int size;// 最大缓存容量private int maxSize;// 写入计数private int putCount;// 创建计数private int createCount;// 淘汰计数private int evictionCount;// 缓存命中计数private int hitCount;// 缓存未命计数private int missCount;
}

写入数据

public final V put(K key, V value) {// 如果值为null,则抛出异常if (key == null || value == null) {throw new NullPointerException("key == null || value == null");}V previous;// 加锁,线程安全synchronized (this) {// 写入计数putCount++;// 通过sizeOf()计算当前项的大小,并累加已有缓存大小size += safeSizeOf(key, value);// 写入操作previous = map.put(key, value);// 如果previous为null表示为新增数据,如果previous不为null表示为修改数据if (previous != null) {// 修改数据需要将size恢复到以前的大小size -= safeSizeOf(key, previous);}}// 回调entryRemoved()方法if (previous != null) {entryRemoved(false, key, previous, value);}// 调整缓存大小trimToSize(maxSize);return previous;
}// 调整缓存大小
public void trimToSize(int maxSize) {// 死循环while (true) {K key;V value;synchronized (this) {// 缓存未满,直接返回if (size <= maxSize || map.isEmpty()) {break;}// 缓存已满情况// 从表头遍历,获取元素Map.Entry<K, V> toEvict = map.entrySet().iterator().next();key = toEvict.getKey();value = toEvict.getValue();// 删除元素map.remove(key);// 减少删除元素的缓存size -= safeSizeOf(key, value);// 删除计数evictionCount++;}// 回调entryRemoved()方法entryRemoved(true, key, value, null);}
}
  • 插入元素,并增加已缓存的大小。
  • 调用 trimToSize() 方法,调整缓存大小。

读取数据

public final V get(@NonNull K key) {if (key == null) {throw new NullPointerException("key == null");}V mapValue;synchronized (this) {// 获取元素,LinkedHashMap会将这个元素移动到表尾mapValue = map.get(key);if (mapValue != null) {hitCount++;return mapValue;}missCount++;}// 没有元素时,会回调create()方法V createdValue = create(key);if (createdValue == null) {return null;}// 下面和put()流程相同synchronized (this) {createCount++;mapValue = map.put(key, createdValue);if (mapValue != null) {map.put(key, mapValue);} else {size += safeSizeOf(key, createdValue);}}if (mapValue != null) {entryRemoved(false, key, createdValue, mapValue);return mapValue;} else {trimToSize(maxSize);return createdValue;}
}
  • 最终调用 LinkedHashMap#get() 方法,因为accessOrder为true ,因此元素会移动到表尾。
  • 如果没有获取到元素时,会调用 create() 方法创建元素,接着执行put()流程。

删除缓存

public final V remove(@NonNull K key) {if (key == null) {throw new NullPointerException("key == null");}V previous;synchronized (this) {// 调用LinkedHashMap#remove()方法删除元素previous = map.remove(key);if (previous != null) {size -= safeSizeOf(key, previous);}}if (previous != null) {entryRemoved(false, key, previous, null);}return previous;
}
  • 调用 LinkedHashMap#remove() 方法删除元素。
http://www.tj-hxxt.cn/news/10442.html

相关文章:

  • 天水做网站电话竞价排名规则
  • 关于华大18年专注seo服务网站制作应用开发朔州网站seo
  • 学校招办网站怎么做seo关键词排名优化品牌
  • 网站模板破解版知乎推广合作
  • 网站开发 常德百度搜索引擎介绍
  • 外贸网站建设定做说到很多seo人员都转行了
  • 企业网站申请永久百度快照功能
  • wordpress79元主题搜索引擎优化的核心及内容
  • 网站设计怎么自学自建网站平台
  • 优秀设计工作室网站百度客服在线咨询人工服务
  • 国内专门做旅游攻略的网站湖南网络推广服务
  • 微科技h5制作网站人员优化方案怎么写
  • 网站demo制作工具广州seo外包公司
  • 安徽网站建设认准-晨飞网络有哪些免费推广软件
  • 外贸商城网站制作百度网页翻译
  • wordpress首页模块排序权限修改黑帽seo
  • 免费素材网站pexels网站域名怎么注册
  • 网站建设用英语怎么说百度快速优化推广
  • 网站开发与服务合同范本seo网络搜索引擎优化
  • 网站建设公司的前景seo排名教程
  • 如何访问自己做的网站上海网站制作推广
  • 静宁县门户网成都百度seo推广
  • alexa全球网站排名昆明seo优化
  • 想自己做一个网站应该怎么弄免费域名注册服务网站
  • 常用搜索网站江门关键词排名工具
  • 四川省人民政府官网seo的工作原理
  • c语言做的网站有什么优缺点查网站排名
  • 网站活动策划怎么做网站建设明细报价表
  • 哈尔滨h5建站模板厦门网站制作
  • 怎样买空间做网站东莞寮步最新通知