Android提供的LruCache类简介(1)
-
*
If your cached values hold resources that need to be explicitly released,
-
* override {@link #entryRemoved}.
-
* 如果你cache的某个值需要明确释放,重写entryRemoved()
-
*
If a cache miss should be computed on demand for the corresponding keys,
-
* override {@link #create}. This simplifies the calling code, allowing it to
-
* assume a value will always be returned, even when there’s a cache miss.
-
* 如果key相对应的item丢掉啦,重写create().这简化了调用代码,即使丢失了也总会返回。
-
*
By default, the cache size is measured in the number of entries. Override
-
* {@link #sizeOf} to size the cache in different units. For example, this cache
-
* is limited to 4MiB of bitmaps: 默认cache大小是测量的item的数量,重写sizeof计算不同item的
-
* 大小。
-
*
{@code
-
* int cacheSize = 4 * 1024 * 1024; // 4MiB
-
* LruCache<String, Bitmap> bitmapCache = new LruCache<String, Bitmap>(cacheSize) {
-
* protected int sizeOf(String key, Bitmap value) {
-
* return value.getByteCount();
-
* }
-
* }}
-
*
-
*
This class is thread-safe. Perform multiple cache operations atomically by
-
* synchronizing on the cache:
{@code
-
* synchronized (cache) {
-
* if (cache.get(key) == null) {
-
* cache.put(key, value);
-
* }
-
* }}
-
*
-
*
This class does not allow null to be used as a key or value. A return
-
* value of null from {@link #get}, {@link #put} or {@link #remove} is
-
* unambiguous: the key was not in the cache.
-
* 不允许key或者value为null
-
* 当get(),put(),remove()返回值为null时,key相应的项不在cache中
-
*/
-
public class LruCache<K, V> {
-
private final LinkedHashMap<K, V> map;
-
/** Size of this cache in units. Not necessarily the number of elements. */
-
private int size; //已经存储的大小
-
private int maxSize; //规定的最大存储空间
-
private int putCount; //put的次数
-
private int createCount; //create的次数
-
private int evictionCount; //回收的次数
-
private int hitCount; //命中的次数
-
private int missCount; //丢失的次数
-
/**
-
* @param maxSize for caches that do not override {@link #sizeOf}, this is
-
* the maximum number of entries in the cache. For all other caches,
-
* this is the maximum sum of the sizes of the entries in this cache.
-
*/
-
public LruCache(int maxSize) {
-
if (maxSize <= 0) {
-
throw new IllegalArgumentException(“maxSize <= 0”);
-
}
-
this.maxSize = maxSize;
-
this.map = new LinkedHashMap<K, V>(0, 0.75f, true);
-
}
-
/**
-
* Returns the value for {@code key} if it exists in the cache or can be
-
* created by {@code #create}. If a value was returned, it is moved to the
-
* head of the queue. This returns null if a value is not cached and cannot
-
* be created. 通过key返回相应的item,或者创建返回相应的item。相应的item会移动到队列的头部,
-
* 如果item的value没有被cache或者不能被创建,则返回null。
-
*/
-
public final V get(K key) {
-
if (key == null) {
-
throw new NullPointerException(“key == null”);
-
}
-
V mapValue;
-
synchronized (this) {
-
mapValue = map.get(key);
-
if (mapValue != null) {
-
hitCount++; //命中
-
return mapValue;
-
}
-
missCount++; //丢失
-
}
-
/*
-
* Attempt to create a value. This may take a long time, and the map
-
* may be different when create() returns. If a conflicting value was
-
* added to the map while create() was working, we leave that value in
-
* the map and release the created value.
-
* 如果丢失了就试图创建一个item
-
*/
-
V createdValue = create(key);
-
if (createdValue == null) {
-
return null;
-
}
-
synchronized (this) {
-
createCount++;//创建++
-
mapValue = map.put(key, createdValue);
-
if (mapValue != null) {
-
// There was a conflict so undo that last put
-
//如果前面存在oldValue,那么撤销put()
-
map.put(key, mapValue);
-
} else {
-
size += safeSizeOf(key, createdValue);
-
}
-
}
-
if (mapValue != null) {
-
entryRemoved(false, key, createdValue, mapValue);
-
return mapValue;
-
} else {
-
trimToSize(maxSize);
-
return createdValue;
-
}
-
}
-
/**
-
* Caches {@code value} for {@code key}. The value is moved to the head of
-
* the queue.
-
*
-
* @return the previous value mapped by {@code key}.
-
*/
-
public final V put(K key, V value) {
-
if (key == null || value == null) {
-
throw new NullPointerException(“key == null || value == null”);
-
}
-
V previous;
-
synchronized (this) {
-
putCount++;
-
size += safeSizeOf(key, value);
-
previous = map.put(key, value);
-
if (previous != null) { //返回的先前的value值
-
size -= safeSizeOf(key, previous);
-
}
-
}
-
if (previous != null) {
-
entryRemoved(false, key, previous, value);
-
}
-
trimToSize(maxSize);
-
return previous;
-
}
-
/**
-
* @param maxSize the maximum size of the cache before returning. May be -1
-
* to evict even 0-sized elements.
-
* 清空cache空间
-
*/
-
private void trimToSize(int maxSize) {
-
while (true) {
-
K key;
-
V value;
-
synchronized (this) {
-
if (size < 0 || (map.isEmpty() && size != 0)) {
-
throw new IllegalStateException(getClass().getName()
-
+ “.sizeOf() is reporting inconsistent results!”);
-
}
-
if (size <= maxSize) {
-
break;
-
}
-
Map.Entry<K, V> toEvict = map.eldest();
-
if (toEvict == null) {
-
break;
-
}
-
key = toEvict.getKey();
-
value = toEvict.getValue();
-
map.remove(key);
-
size -= safeSizeOf(key, value);
-
evictionCount++;
-
}
-
entryRemoved(true, key, value, null);
-
}
-
}
-
/**
-
* Removes the entry for {@code key} if it exists.
-
* 删除key相应的cache项,返回相应的value
-
* @return the previous value mapped by {@code key}.
-
*/
-
public final V remove(K key) {
-
if (key == null) {
-
throw new NullPointerException(“key == null”);
-
}
-
V previous;
-
synchronized (this) {
-
previous = map.remove(key);
-
if (previous != null) {
-
size -= safeSizeOf(key, previous);
-
}
-
}
-
if (previous != null) {
-
entryRemoved(false, key, previous, null);
-
}
-
return previous;
-
}
-
/**
-
* Called for entries that have been evicted or removed. This method is
-
* invoked when a value is evicted to make space, removed by a call to
-
* {@link #remove}, or replaced by a call to {@link #put}. The default
-
* implementation does nothing.
-
* 当item被回收或者删掉时调用。改方法当value被回收释放存储空间时被remove调用,
-
* 或者替换item值时put调用,默认
实现什么都没做。 -
*
The method is called without synchronization: other threads may
-
* access the cache while this method is executing.
-
*
-
* @param evicted true if the entry is being removed to make space, false
-
* if the removal was caused by a {@link #put} or {@link #remove}.
-
* true—为释放空间被删除;false—put或remove导致
-
* @param newValue the new value for {@code key}, if it exists. If non-null,
-
* this removal was caused by a {@link #put}. Otherwise it was caused by
-
* an eviction or a {@link #remove}.
-
*/
-
protected void entryRemoved(boolean evicted, K key, V oldValue, V newValue) {}
-
/**
-
* Called after a cache miss to compute a value for the corresponding key.
-
* Returns the computed value or null if no value can be computed. The
-
* default implementation returns null.
-
* 当某Item丢失时会调用到,返回计算的相应的value或者null
-
*
The method is called without synchronization: other threads may
-
* access the cache while this method is executing.
-
*
-
*
If a value for {@code key} exists in the cache when this method
-
* returns, the created value will be released with {@link #entryRemoved}
-
* and discarded. This can occur when multiple threads request the same key
-
* at the same time (causing multiple values to be created), or when one
-
* thread calls {@link #put} while another is creating a value for the same
-
* key.
-
*/
-
protected V create(K key) {
-
return null;
-
}
-
private int safeSizeOf(K key, V value) {
-
int result = sizeOf(key, value);
-
if (result < 0) {
-
throw new IllegalStateException("Negative size: " + key + “=” + value);
-
}
-
return result;
-
}
-
/**
-
* Returns the size of the entry for {@code key} and {@code value} in
-
* user-defined units. The default implementation returns 1 so that size
-
* is the number of entries and max size is the maximum number of entries.
-
* 返回用户定义的item的大小,默认返回1代表item的数量,最大size就是最大item值
-
*
An entry’s size must not change while it is in the cache.
-
*/
-
protected int sizeOf(K key, V value) {
-
return 1;
-
}
-
/**
-
* Clear the cache, calling {@link #entryRemoved} on each removed entry.
最后
小编这些年深知大多数初中级Android工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人
都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
资料⬅专栏获取
0. */
-
protected int sizeOf(K key, V value) {
-
return 1;
-
}
-
/**
-
* Clear the cache, calling {@link #entryRemoved} on each removed entry.
最后
小编这些年深知大多数初中级Android工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
[外链图片转存中…(img-7R2o989d-1718991877996)]
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人
都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
资料⬅专栏获取
相关文章:

Android提供的LruCache类简介(1)
* If your cached values hold resources that need to be explicitly released, * override {link #entryRemoved}. * 如果你cache的某个值需要明确释放,重写entryRemoved() * If a cache miss should be computed on demand for the corresponding keys, * ov…...

【分布式系列】分布式锁timeout了怎么办?
💝💝💝欢迎来到我的博客,很高兴能够在这里和您见面!希望您在这里可以感受到一份轻松愉快的氛围,不仅可以获得有趣的内容和知识,也可以畅所欲言、分享您的想法和见解。 推荐:kwan 的首页,持续学…...
System.getProperty()方法总结
System.getProperty()方法总结 大家好,我是免费搭建查券返利机器人省钱赚佣金就用微赚淘客系统3.0的小编,也是冬天不穿秋裤,天冷也要风度的程序猿!System.getProperty()方法是Java中用于获取系统属性的方法之一。它允许我们访问J…...

大型语言模型在AMD GPU上的推理优化
Large language model inference optimizations on AMD GPUs — ROCm Blogs 大型语言模型(LLMs)已经改变了自然语言处理和理解,促进了在多个领域中的众多人工智能应用。LLMs在包括AI助手、聊天机器人、编程、游戏、学习、搜索和推荐系统在内的…...

Apple - Core Foundation Design Concepts
本文翻译整理自:Core Foundation Design Concepts(更新日期:2013-12-16 https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFDesignConcepts/CFDesignConcepts.html#//apple_ref/doc/uid/10000122i 文章…...
lua中的lfs库介绍
lua中的lfs库介绍 说明常用函数解析lfs.attributeslfs.chdirlfs.currentdirlfs.dirlfs.mkdirlfs.rmdirlfs.locklfs.touchlfs.linklfs.setmodelfs.symlinkattributes 说明 lfs是lua中的一个文件系统库,提供了更多高级的文件和目录操作功能,使得lua可以更方…...
PyCharm 快捷键积累
1、快速格式化:Ctrl Alt L Ctrl Alt L 快捷键在 PyCharm 中是用于格式化代码的,它不仅仅适用于 HTML 代码,而是适用于多种编程和标记语言。...

C++进阶之AVL树
个人主页:点我进入主页 专栏分类:C语言初阶 C语言进阶 数据结构初阶 Linux C初阶 C进阶 算法 欢迎大家点赞,评论,收藏。 一起努力,一起奔赴大厂 目录 一.前言 二.插入 三.旋转 3.1右旋 …...
sizeof 和 strlen 比较
sizeof 和 strlen 在 C 语言中都是用于获取某种“大小”的,但它们之间有着显著的区别。 sizeof sizeof 是一个运算符,用于计算数据类型或对象在内存中的大小(以字节为单位)。它可以在编译时确定结果,因为它计算的是类…...

音视频开发—FFmpeg 打开摄像头进行RTMP推流
实验平台:Ubuntu20.04 摄像头:普通USB摄像头,输出格式为YUV422 1.配置RTMP服务器推流平台 使用Nginx 配置1935端口即可,贴上教程地址 ubuntu20.04搭建Nginxrtmp服务器) 2.配置FFmpeg开发环境 过程较为简单,这里不…...

D触发器(D Flip-Flop)与D锁存器(D Latch)
1 基础概念 我们先来简单回顾一下D触发器(D flip-flop)和D锁存器(D latch)的概念,以及它们在数字电路中的作用。 1.1 D触发器(D Flip-Flop) D触发器是一种数字存储器件,它在时钟信号…...

JDK19特性
JDK19特性 一、JAVA19概述 JDK 19 2022 年 9 月 20 日正式发布以供生产使用,非长期支持版本。不过,JDK 19 中有一些比较重要的新特性值得关注。 JDK 19 只有 7 个新特性: JEP 405: Record Patterns(记录模式)[1] (预览)JEP 422: Linux/RISC-V Port[2]JEP 424: Foreign …...
sql语句中常用的函数有那些
1、字符串函数 CONCAT(string1, string2, ...): 连接两个或多个字符串。 UPPER(string): 将字符串转换为大写。 LOWER(string): 将字符串转换为小写。 TRIM(string): 去除字符串两端的空格。 LENGTH(string): 返回字符串的长度。 SUBSTRING(string, start, length): 从字符串中…...
odoo17 小变更3 Warning、 “attrs “和 “states “不再用
odoo17 小变更 1、Warning from odoo.exceptions import ValidationError,Warning ImportError: cannot import name Warning from odoo.exceptions (D:\od172406\odoo\exceptions.py) 2、自 17.0 版起,不再使用 "attrs "和 "states "属性。 …...

Unity3d 游戏暂停(timeScale=0)引起的deltaTime关联的系列问题解决
问题描述 游戏暂停的功能是通过设置timeScale0实现的,不过在暂停游戏的时候,需要对角色进行预览和设置,为了实现这个功能,是通过鼠标控制相机的操作,为了使相机的操作丝滑,获取鼠标操作系数乘以Time.delta…...

服务端代码编写中MySql大小写在Java中报错问题解决
报错信息: 原因:MySql和Java变量大小写产生的冲突。 经过查阅各个博客等,得出浅显结论(不一定对):MySql大小写不敏感,Java大小写敏感,当Javabean转为MySql数据库表时,Ja…...

CRMEB 多店商品详情页装修说明
一、功能介绍 商家可调整商品详情各板块样式,可根据不同的需求开启或关闭单独的板块 二、操作流程 装修 > 商品详情 三、功能说明 1、商品信息 可控制商品详情页面商品信息的显示与隐藏 2、会员信息,排行榜 控制商品详情页面会员信息及排行榜的…...

Redis-使用 jedis 操作数据
文章目录 1、Jedis简介2、环境准备3、创建maven普通项目,导入如下依赖4、测试JAVA程序和Redis之间的通信 1、Jedis简介 "Jedis" 通常是作为 "Java Redis" 的缩写或简称来理解的。Java Embedded Data Structures Interface 表示 Java嵌入式数据结构接口 2、…...
简说PIP换源
概述 PIP(Python Package Installer)是 Python 的包管理工具,用于安装和管理 Python 包。默认情况下,PIP 从 Python 官方的包仓库(即 PyPI)下载和安装包。然而,由于网络原因,访问官…...

django学习入门系列之第三点《CSS基础样式介绍2》
文章目录 文字对齐方式外边距内边距往期回顾 文字对齐方式 水平对齐方式 text-align: center;垂直对齐方式 /* 注意,这个只能是一行来居中 */ line-height:/*长度*/ ;样例 <!DOCTYPE html> <html lang"en"> <head><meta charset…...
<6>-MySQL表的增删查改
目录 一,create(创建表) 二,retrieve(查询表) 1,select列 2,where条件 三,update(更新表) 四,delete(删除表…...
多场景 OkHttpClient 管理器 - Android 网络通信解决方案
下面是一个完整的 Android 实现,展示如何创建和管理多个 OkHttpClient 实例,分别用于长连接、普通 HTTP 请求和文件下载场景。 <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas…...
线程同步:确保多线程程序的安全与高效!
全文目录: 开篇语前序前言第一部分:线程同步的概念与问题1.1 线程同步的概念1.2 线程同步的问题1.3 线程同步的解决方案 第二部分:synchronized关键字的使用2.1 使用 synchronized修饰方法2.2 使用 synchronized修饰代码块 第三部分ÿ…...

对WWDC 2025 Keynote 内容的预测
借助我们以往对苹果公司发展路径的深入研究经验,以及大语言模型的分析能力,我们系统梳理了多年来苹果 WWDC 主题演讲的规律。在 WWDC 2025 即将揭幕之际,我们让 ChatGPT 对今年的 Keynote 内容进行了一个初步预测,聊作存档。等到明…...
Nginx server_name 配置说明
Nginx 是一个高性能的反向代理和负载均衡服务器,其核心配置之一是 server 块中的 server_name 指令。server_name 决定了 Nginx 如何根据客户端请求的 Host 头匹配对应的虚拟主机(Virtual Host)。 1. 简介 Nginx 使用 server_name 指令来确定…...

MySQL 8.0 OCP 英文题库解析(十三)
Oracle 为庆祝 MySQL 30 周年,截止到 2025.07.31 之前。所有人均可以免费考取原价245美元的MySQL OCP 认证。 从今天开始,将英文题库免费公布出来,并进行解析,帮助大家在一个月之内轻松通过OCP认证。 本期公布试题111~120 试题1…...
安卓基础(aar)
重新设置java21的环境,临时设置 $env:JAVA_HOME "D:\Android Studio\jbr" 查看当前环境变量 JAVA_HOME 的值 echo $env:JAVA_HOME 构建ARR文件 ./gradlew :private-lib:assembleRelease 目录是这样的: MyApp/ ├── app/ …...

视频行为标注工具BehaviLabel(源码+使用介绍+Windows.Exe版本)
前言: 最近在做行为检测相关的模型,用的是时空图卷积网络(STGCN),但原有kinetic-400数据集数据质量较低,需要进行细粒度的标注,同时粗略搜了下已有开源工具基本都集中于图像分割这块,…...

STM32HAL库USART源代码解析及应用
STM32HAL库USART源代码解析 前言STM32CubeIDE配置串口USART和UART的选择使用模式参数设置GPIO配置DMA配置中断配置硬件流控制使能生成代码解析和使用方法串口初始化__UART_HandleTypeDef结构体浅析HAL库代码实际使用方法使用轮询方式发送使用轮询方式接收使用中断方式发送使用中…...
MySQL 8.0 事务全面讲解
以下是一个结合两次回答的 MySQL 8.0 事务全面讲解,涵盖了事务的核心概念、操作示例、失败回滚、隔离级别、事务性 DDL 和 XA 事务等内容,并修正了查看隔离级别的命令。 MySQL 8.0 事务全面讲解 一、事务的核心概念(ACID) 事务是…...