DiskLruCache part 1
2019独角兽企业重金招聘Python工程师标准>>>
首先看缓存单元Entry 和 entry的管理器editor的定义:
Entry: 即缓存项, 可以有valueCount个 value, 每个value是一个文件,文件名是[key].[index] , index的值从0到 valueCount . 每个entry的 sequenceNumber、readable、 lengths等值会在被Editor编辑并commit之后有 DiskLruCache负责更新。
所谓 dirty file, 就是处在编辑状态的file。
private final class Entry {private final String key;/** Lengths of this entry's files. */private final long[] lengths;/** True if this entry has ever been published. */private boolean readable;/** The ongoing edit or null if this entry is not being edited. */private Editor currentEditor;/** The sequence number of the most recently committed edit to this entry. */private long sequenceNumber;private Entry(String key) {this.key = key;this.lengths = new long[valueCount];}.../** Set lengths using decimal numbers like "10123". */private void setLengths(String[] strings) throws IOException {if (strings.length != valueCount) {throw invalidLengths(strings);}try {for (int i = 0; i < strings.length; i++) {lengths[i] = Long.parseLong(strings[i]);}} catch (NumberFormatException e) {throw invalidLengths(strings);}}...public File getCleanFile(int i) {return new File(directory, key + "." + i);}public File getDirtyFile(int i) {return new File(directory, key + "." + i + ".tmp");}}
Editor:编辑器,对entry进行编辑、更新、保存, 有点儿类似于 SharedPreferrence的 Editor。
/** Edits the values for an entry. */public final class Editor {private final Entry entry;private final boolean[] written;private boolean hasErrors;private boolean committed;private Editor(Entry entry) {this.entry = entry;this.written = (entry.readable) ? null : new boolean[valueCount];}/*** Returns an unbuffered input stream to read the last committed value,* or null if no value has been committed.*/public InputStream newInputStream(int index) throws IOException {synchronized (DiskLruCache.this) {if (entry.currentEditor != this) {throw new IllegalStateException();}if (!entry.readable) {return null;}try {return new FileInputStream(entry.getCleanFile(index));} catch (FileNotFoundException e) {return null;}}}public String getString(int index) throws IOException {InputStream in = newInputStream(index);return in != null ? inputStreamToString(in) : null;}public OutputStream newOutputStream(int index) throws IOException {if (index < 0 || index >= valueCount) {throw new IllegalArgumentException("Expected index " + index + " to "+ "be greater than 0 and less than the maximum value count "+ "of " + valueCount);}synchronized (DiskLruCache.this) {if (entry.currentEditor != this) {throw new IllegalStateException();}if (!entry.readable) {written[index] = true;}File dirtyFile = entry.getDirtyFile(index);FileOutputStream outputStream;try {outputStream = new FileOutputStream(dirtyFile);} catch (FileNotFoundException e) {// Attempt to recreate the cache directory.directory.mkdirs();try {outputStream = new FileOutputStream(dirtyFile);} catch (FileNotFoundException e2) {// We are unable to recover. Silently eat the writes.return NULL_OUTPUT_STREAM;}}return new FaultHidingOutputStream(outputStream);}}/** Sets the value at {@code index} to {@code value}. */public void set(int index, String value) throws IOException {Writer writer = null;try {writer = new OutputStreamWriter(newOutputStream(index), Util.UTF_8);writer.write(value);} finally {Util.closeQuietly(writer);}}/*** Commits this edit so it is visible to readers. This releases the* edit lock so another edit may be started on the same key.*/public void commit() throws IOException {if (hasErrors) {completeEdit(this, false);remove(entry.key); // The previous entry is stale.} else {completeEdit(this, true);}committed = true;}/*** Aborts this edit. This releases the edit lock so another edit may be* started on the same key.*/public void abort() throws IOException {completeEdit(this, false);}public void abortUnlessCommitted() {if (!committed) {try {abort();} catch (IOException ignored) {}}}private class FaultHidingOutputStream extends FilterOutputStream {private FaultHidingOutputStream(OutputStream out) {super(out);}@Override public void write(int oneByte) {try {out.write(oneByte);} catch (IOException e) {hasErrors = true;}}@Override public void write(byte[] buffer, int offset, int length) {try {out.write(buffer, offset, length);} catch (IOException e) {hasErrors = true;}}@Override public void close() {try {out.close();} catch (IOException e) {hasErrors = true;}}@Override public void flush() {try {out.flush();} catch (IOException e) {hasErrors = true;}}}}
转载于:https://my.oschina.net/u/255456/blog/346071
DiskLruCache part 1相关推荐
- Android使用磁盘缓存DiskLruCache
DiskLruCache 不同于LruCache,LruCache是将数据缓存到内存中去,而DiskLruCache是外部缓存,例如可以将网络下载的图片永久的缓存到手机外部存储中去,并可以将缓存数据取 ...
- DiskLruCache 源码解析
DiskLruCache 描述: DiskLruCache 是用来缓存一些数据,比如网络访问的Json,加载的图片等等. LruCache 是把数据缓存到内存中,而DisLruCache 是把数据缓存 ...
- Android之DiskLruCache源码解析
转载请标明出处: http://blog.csdn.net/hai_qing_xu_kong/article/details/73863258 本文出自:[顾林海的博客] 个人开发的微信小程序,目前功 ...
- 【Android 内存优化】Bitmap 硬盘缓存 ( Google 官方 Bitmap 示例 | DiskLruCache 开源库 | 代码示例 )
文章目录 一.Google 官方 Bitmap 相关示例参考 二.磁盘缓存类 DiskLruCache 三.磁盘缓存初始化 四.存储数据到磁盘缓存中 五.从磁盘缓存中读取数据 六. Android 1 ...
- 安卓开发笔记——关于图片的三级缓存策略(内存LruCache+磁盘DiskLruCache+网络Volley)...
在开发安卓应用中避免不了要使用到网络图片,获取网络图片很简单,但是需要付出一定的代价--流量.对于少数的图片而言问题不大,但如果手机应用中包含大量的图片,这势必会耗费用户的一定流量,如果我们不加以处理 ...
- Android照片墙完整版,完美结合 内存方案 LruCache 和 硬盘方案 DiskLruCache
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/34093441 在上一篇文章当中,我们学习了DiskLruCache的概念和基本用法 ...
- 三分钟学会缓存工具DiskLruCache
DiskLruCache是一个十分好用的android缓存工具,我们可以从GitHub上下载其源码:https://github.com/JakeWharton/DiskLruCache DiskLr ...
- Android之DiskLruCache(缓存工具)
DiskLruCache DiskLruCache是一个十分好用的android缓存工具,我们可以从GitHub上下载其源码:https://github.com/JakeWharton/DiskLr ...
- Android开源框架源码鉴赏:LruCache与DiskLruCache
关于作者 郭孝星,程序员,吉他手,主要从事Android平台基础架构方面的工作,欢迎交流技术方面的问题,可以去我的Github提issue或者发邮件至guoxiaoxingse@163.com与我交流 ...
最新文章
- [冲昏头脑]IDEA中的maven项目中学习log4j的日志操作
- JSR311发布Restful WebService工程
- 腾讯云助力中国信息通信研究院研发运维标准化体系建设
- Java中的HashSet
- ***PHP 去除换行符
- 【华为云技术分享】大数据容器化,头部玩家尝到了甜头
- pytorch得到中间层输出
- LINQ 学习路程 -- 查询操作 ElementAt, ElementAtOrDefault
- 文件流的使用以及序列化和反序列化的方法使用
- python如何屏幕截图_Python实现屏幕截图的两种方式
- python画彩虹圈_javascript – 如何使用HTML5画布生成彩虹圈?
- 如何将页脚(footer)保持在页面底部
- java数组逆序_Java 数组的排序、逆序
- 本地项目转git项目的详细操作方法
- Oracle和Postgre的TRUNC函数
- EXPLORATION BY RANDOM NETWORK DISTILLATION (RND)
- GD32学习笔记(3)NAND Flash管理
- 目标检测常用评价指标笔记
- 国外毕业论文写作论证方面如何做好?
- 3D模型拆分与合并展示,IVX真的可以简单实现