- 发布日期:2023-10-30 04:39 点击次数:201 排列三捕鱼博彩平台投注_[[423666]] 序论
DiskLruCache是一种处理数据存储的技艺,单从Cache的字面酷好也不错交融到,"Cache","高速缓存";
之前咱们先容过lrucache,莫得看过老铁,不错从历史记载看;
今天咱们来从源码上分析下DiskLruCache;
Android进阶之透澈交融LruCache缓存机制旨趣
排列三捕鱼手机应用皇冠作为一家博彩平台,提供全球范围内最新、热门赛事优秀博彩服务,不仅畅享极致博彩体验,更能成为博彩赢家。皇冠博彩下载 一、为什么用DiskLruCache 1、LruCache和DiskLruCacheLruCache和DiskLruCache两者齐是利用到LRU算法,通过LRU算法对缓存进行处理,以最近最少使用手脚处理的依据,删除最近最少使用的数据,保留最近最常用的数据;
LruCache利用于内存缓存,而DiskLruCache是存储竖立缓存;
2、为何使用DiskLruCache离线数据存在的意旨,当无网络粗鄙是网络情景不好时,APP还是具备部分功能是一种很好的用户体验;
皇冠客服飞机:@seo3687
博彩平台投注365bet网站假定网易新闻这类新闻客户端,数据完竣存储在缓存中而不使用DiskLruCache技艺存储,那么当客户端被放手,缓存被开释,意味着再次掀开APP将是一派空缺;
另外DiskLruCache技艺也可为app“离线阅读”这一功能作念技艺维持;
皇冠hg86a
DiskLruCache的存储旅途是不错自界说的,不外也不错是默许的存储旅途,而默许的存储旅途一般是这么的:/sdcard/Android/data/包名/cache,包名是指APP的包名。咱们不错在手机上掀开,浏览这一起径;
二、DiskLruCache使用 1、添加依赖// add dependence implementation 'com.jakewharton:disklrucache:2.0.2'2、创建DiskLruCache对象
/* * directory – 缓存目次 * appVersion - 缓存版块 * valueCount – 每个key对应value的个数 * maxSize – 缓存大小的上限 */ DiskLruCache diskLruCache = DiskLruCache.open(directory, 1, 1, 1024 * 1024 * 10);3、添加 / 赢得 缓存(一双一)
/** * 添加一条缓存,一个key对应一个value */ public void addDiskCache(String key, String value) throws IOException { File cacheDir = context.getCacheDir(); DiskLruCache diskLruCache = DiskLruCache.open(cacheDir, 1, 1, 1024 * 1024 * 10); DiskLruCache.Editor editor = diskLruCache.edit(key); // index与valueCount对应,区分为0,1,2...valueCount-1 editor.newOutputStream(0).write(value.getBytes()); editor.commit(); diskLruCache.close(); } /** * 赢得一条缓存,欧博娱乐代理一个key对应一个value */ public void getDiskCache(String key) throws IOException { File directory = context.getCacheDir(); DiskLruCache diskLruCache = DiskLruCache.open(directory, 1, 1, 1024 * 1024 * 10); String value = diskLruCache.get(key).getString(0); diskLruCache.close(); }4、添加 / 赢得 缓存(一双多)
/** * 添加一条缓存,1个key对应2个value */ public void addDiskCache(String key, String value1, String value2) throws IOException { File directory = context.getCacheDir(); DiskLruCache diskLruCache = DiskLruCache.open(directory, 1, 2, 1024 * 1024 * 10); DiskLruCache.Editor editor = diskLruCache.edit(key); editor.newOutputStream(0).write(value1.getBytes()); editor.newOutputStream(1).write(value2.getBytes()); editor.commit(); diskLruCache.close(); } /** * 添加一条缓存,1个key对应2个value */ public void getDiskCache(String key) throws IOException { File directory = context.getCacheDir(); DiskLruCache diskLruCache = DiskLruCache.open(directory, 1, 2, 1024); DiskLruCache.Snapshot snapshot = diskLruCache.get(key); String value1 = snapshot.getString(0); String value2 = snapshot.getString(1); diskLruCache.close(); }三、源码分析 1、open()
DiskLruCache的构造措施是private修饰,这也即是告诉咱们,不可通过new DiskLruCache来赢得实例,构造措施如下:
private DiskLruCache(File directory, int appVersion, int valueCount, long maxSize) { this.directory = directory; this.appVersion = appVersion; this.journalFile = new File(directory, JOURNAL_FILE); this.journalFileTmp = new File(directory, JOURNAL_FILE_TEMP); this.journalFileBackup = new File(directory, JOURNAL_FILE_BACKUP); this.valueCount = valueCount; this.maxSize = maxSize; }
然则提供了open()措施,供咱们赢得DiskLruCache的实例,open措施如下:
太平洋在线直营网/** * Opens the cache in {@code directory}, creating a cache if none exists * there. * * @param directory a writable directory * @param valueCount the number of values per cache entry. Must be positive. * @param maxSize the maximum number of bytes this cache should use to store * @throws IOException if reading or writing the cache directory fails */ public static DiskLruCache open(File directory, int appVersion, int valueCount, long maxSize) throws IOException { if (maxSize <= 0) { throw new IllegalArgumentException("maxSize <= 0"); } if (valueCount <= 0) { throw new IllegalArgumentException("valueCount <= 0"); } // If a bkp file exists, use it instead. //看备份文献是否存在 File backupFile = new File(directory, JOURNAL_FILE_BACKUP); //若是备份文献存在,况且日记文献也存在,就把备份文献删除 //若是备份文献存在,日记文献不存在,就把备份文献重定名为日记文献 if (backupFile.exists()) { File journalFile = new File(directory, JOURNAL_FILE); // If journal file also exists just delete backup file. // if (journalFile.exists()) { backupFile.delete(); } else { renameTo(backupFile, journalFile, false); } } // Prefer to pick up where we left off. //运行化DiskLruCache,包括,大小,版块,旅途,key对应若干value DiskLruCache cache = new DiskLruCache(directory, appVersion, valueCount, maxSize); //若是日记文献存在,就脱手赌文献信息,并复返 //主要即是构建entry列表 if (cache.journalFile.exists()) { try { cache.readJournal(); cache.processJournal(); return cache; } catch (IOException journalIsCorrupt) { System.out .println("DiskLruCache " + directory + " is corrupt: " + journalIsCorrupt.getMessage() + ", removing"); cache.delete(); } } //不存在就新建一个 // Create a new empty cache. directory.mkdirs(); cache = new DiskLruCache(directory, appVersion, valueCount, maxSize); cache.rebuildJournal(); return cache; }
open函数:若是日记文献存在,径直去构建entry列表;若是不存在,就构建日记文献;
银河娱乐app白色的 2、rebuildJournal()构建文献: //这个即是咱们不错径直在disk内部看到的journal文献 主要即是对他的操作 private final File journalFile; //journal文献的temp 缓存文献,一般齐是先构建这个缓存文献,恭候构建完成以后将这个缓存文献从头定名为journal private final File journalFileTmp; /** * Creates a new journal that omits redundant information. This replaces the * current journal if it exists. */ private synchronized void rebuildJournal() throws IOException { if (journalWriter != null) { journalWriter.close(); } //指向journalFileTmp这个日记文献的缓存 Writer writer = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(journalFileTmp), Util.US_ASCII)); try { writer.write(MAGIC); writer.write("\n"); writer.write(VERSION_1); writer.write("\n"); writer.write(Integer.toString(appVersion)); writer.write("\n"); writer.write(Integer.toString(valueCount)); writer.write("\n"); writer.write("\n"); for (Entry entry : lruEntries.values()) { if (entry.currentEditor != null) { writer.write(DIRTY + ' ' + entry.key + '\n'); } else { writer.write(CLEAN + ' ' + entry.key + entry.getLengths() + '\n'); } } } finally { writer.close(); } if (journalFile.exists()) { renameTo(journalFile, journalFileBackup, true); } //是以这个地点 构建日记文献的经过主要即是先构建出日记文献的缓存文献,若是缓存构建奏凯 那就径直重定名这个缓存文献,这么作念克己在何处? renameTo(journalFileTmp, journalFile, false); journalFileBackup.delete(); //这里亦然把写入日记文献的writer运行化 journalWriter = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(journalFile, true), Util.US_ASCII)); }
再来看当日记文献存在的期间沙巴龙虎斗,作念了什么
3、readJournal()private void readJournal() throws IOException { StrictLineReader reader = new StrictLineReader(new FileInputStream(journalFile), Util.US_ASCII); try { //读日记文献的头信息 String magic = reader.readLine(); String version = reader.readLine(); String appVersionString = reader.readLine(); String valueCountString = reader.readLine(); String blank = reader.readLine(); if (!MAGIC.equals(magic)
欧博博彩网站
热点资讯
- 排列三色碟博彩平台游戏优惠(www.betlikeakinghome.com)
- 北京赛车娱乐城博彩网站客服_“七大反诈利器”有用防护电信网罗诈欺
- 亚新三公蓝鲸体育苹果版app下载_危地马拉16岁男孩在好意思国工场责任时亏空 工东谈主曾听到尖叫求救
- 网络游戏平台最新博彩评级 | 欢聚文化嘉会 担当文静做事
- 新宝代理博彩平台游戏优惠券活动 | 规复中超冠名是拨乱归正 但这遮羞布没事谋事
- 排列三捕鱼博彩平台投注_Android妙手进阶之透澈了解DiskLruCache磁盘缓存机制旨趣
- ag正网2014欧洲杯分组_劳斯莱斯限量两座敞篷跑车 Drop Tail 发布: 颠覆失掉界说
- 排列五骰宝欧洲杯比分结果_磨真金不怕火东说念主生, 越来越庞杂的三个星座
- 澳门新葡京大酒店亚慱体育app官网合法吗 | 大舅晚上霎时来我家,我不想让他留宿,只可用钱买个舒心
- 沙巴炸金花博彩活动_如何辩别社会讲授?国博:当今主要看是否有导览成立