在开发安卓应用中避免不了要使用到网络图片,获取网络图片很简单,但是需要付出一定的代价——流量。对于少数的图片而言问题不大,但如果手机应用中包含大量的图片,这势必会耗费用户的一定流量,如果我们不加以处理,每次打开应用都去网络获取图片,那么用户可就不乐意了,这里的处理就是指今天要讲的缓存策略(缓存层分为三层:内存层,磁盘层,网络层)。
关于缓存层的工作,当我们第一次打开应用获取图片时,先到网络去下载图片,然后依次存入内存缓存,磁盘缓存,当我们再一次需要用到刚才下载的这张图片时,就不需要再重复的到网络上去下载,直接可以从内存缓存和磁盘缓存中找,由于内存缓存速度较快,我们优先到内存缓存中寻找该图片,如果找到则运用,如果没有找到(内存缓存大小有限),那么我们再到磁盘缓存中去找。只要我们合理的去协调这三层缓存运用,便可以提升应用性能和用户体验。
1、内存层:(手机内存)
内存缓存相对于磁盘缓存而言,速度要来的快很多,但缺点容量较小且会被系统回收,这里的实现我用到了LruCache。
LruCache这个类是Android3.1版本中提供的,如果你是在更早的Android版本中开发,则需要导入android-support-v4的jar包。
磁盘层:(SD卡)
相比内存缓存而言速度要来得慢很多,但容量很大,这里的实现我用到了DiskLruCache类。
DiskLruCache是非Google官方编写,但获得官方认证的硬盘缓存类,该类没有限定在Android内,所以理论上java应用也可以使用DiskLreCache来缓存。
这是DiskLruCache类的下载地址:http://pan.baidu.com/s/1hq0D53m
网络层:(移动网络,无线网络)
这个就没什么解释的了,就是我们上网用的流量。这里的网络访问实现我用到了开源框架Volley。
开源框架Volley是2013年Google I/O大会发布的,Volley是Android平台上的网络通信库,能使网络通信更快,更简单,更健壮。它的设计目标就是非常适合去进行数据量不大,但通信频繁的网络操作,而对于大数据量的网络操作,比如说下载文件等,Volley的表现就会非常糟糕。
这是Volley的下载地址:http://pan.baidu.com/s/1hq1t2yo
先来看下效果图:
正常网络下: 断开网络,飞行模式下:
Log日志打印:
来看下代码实现:
1、由于应用中很多地方需要用到上下文对象,这里我自定义了一个全局的application,用来提供上下文对象
1 package com.lcw.rabbit.image.utils; 2 3 import android.app.Application; 4 /** 5 * Application类,提供全局上下文对象 6 * @author Rabbit_Lee 7 * 8 */ 9 public class MyApplication extends Application {10 11 public static String TAG;12 public static MyApplication myApplication;13 14 public static MyApplication newInstance() {15 return myApplication;16 }17 18 @Override19 public void onCreate() {20 super.onCreate();21 TAG = this.getClass().getSimpleName();22 myApplication = this;23 24 }25 }
2、Volley请求队列处理类,用来管理Rquest请求对象操作
1 package com.lcw.rabbit.image; 2 3 import com.android.volley.Request; 4 import com.android.volley.RequestQueue; 5 import com.android.volley.toolbox.Volley; 6 import com.lcw.rabbit.image.utils.MyApplication; 7 8 /** 9 * 请求队列处理类10 * 获取RequestQueue对象11 */12 public class VolleyRequestQueueManager {13 // 获取请求队列类14 public static RequestQueue mRequestQueue = Volley.newRequestQueue(MyApplication.newInstance());15 16 //添加任务进任务队列17 public static void addRequest(Request<?> request, Object tag) {18 if (tag != null) {19 request.setTag(tag);20 }21 mRequestQueue.add(request);22 }23 24 //取消任务25 public static void cancelRequest(Object tag){26 mRequestQueue.cancelAll(tag);27 }28 29 30 31 }
3、这里附上2个工具类(生成md5序列帮助类,DiskLruCache磁盘缓存类)
1 package com.lcw.rabbit.image.utils; 2 3 import java.math.BigInteger; 4 import java.security.MessageDigest; 5 import java.security.NoSuchAlgorithmException; 6 7 public class MD5Utils { 8 /** 9 * 使用md5的算法进行加密10 */11 public static String md5(String plainText) {12 byte[] secretBytes = null;13 try {14 secretBytes = MessageDigest.getInstance("md5").digest(15 plainText.getBytes());16 } catch (NoSuchAlgorithmException e) {17 throw new RuntimeException("没有md5这个算法!");18 }19 String md5code = new BigInteger(1, secretBytes).toString(16);// 16进制数字20 // 如果生成数字未满32位,需要前面补021 for (int i = 0; i < 32 - md5code.length(); i++) {22 md5code = "0" + md5code;23 }24 return md5code;25 }26 27 }MD5转换类
1 /* 2 * Copyright (C) 2011 The Android Open Source PRoject 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.lcw.rabbit.image.utils; 18 19 import java.io.BufferedInputStream; 20 import java.io.BufferedWriter; 21 import java.io.Closeable; 22 import java.io.EOFException; 23 import java.io.File; 24 import java.io.FileInputStream; 25 import java.io.FileNotFoundException; 26 import java.io.FileOutputStream; 27 import java.io.FileWriter; 28 import java.io.FilterOutputStream; 29 import java.io.IOException; 30 import java.io.InputStream; 31 import java.io.InputStreamReader; 32 import java.io.OutputStream; 33 import java.io.OutputStreamWriter; 34 import java.io.Reader; 35 import java.io.StringWriter; 36 import java.io.Writer; 37 import java.lang.reflect.Array; 38 import java.nio.charset.Charset; 39 import java.util.ArrayList; 40 import java.util.Arrays; 41 import java.util.Iterator; 42 import java.util.LinkedHashMap; 43 import java.util.Map; 44 import java.util.concurrent.Callable; 45 import java.util.concurrent.ExecutorService; 46 import java.util.concurrent.LinkedBlockingQueue; 47 import java.util.concurrent.ThreadPoolExecutor; 48 import java.util.concurrent.TimeUnit; 49 50 /** 51 ****************************************************************************** 52 * Taken from the JB source code, can be found in: 53 * libcore/luni/src/main/java/libcore/io/DiskLruCache.java 54 * or direct link: 55 * https://android.googlesource.com/platform/libcore/+/android-4.1.1_r1/luni/src/main/java/libcore/io/DiskLruCache.java 56 ****************************************************************************** 57 * 58 * A cache that uses a bounded amount of space on a filesystem. Each cache 59 * entry has a string key and a fixed number of values. Values are byte 60 * sequences, accessible as streams or files. Each value must be between {@code 61 * 0} and {@code Integer.MAX_VALUE} bytes in length. 62 * 63 * <p>The cache stores its data in a directory on the filesystem. This 64 * directory must be exclusive to the cache; the cache may delete or overwrite 65 * files from its directory. It is an error for multiple processes to use the 66 * same cache directory at the same time. 67 * 68 * <p>This cache limits the number of bytes that it will store on the 69 * filesystem. When the number of stored bytes exceeds the limit, the cache will 70 * remove entries in the background until the limit is satisfied. The limit is 71 * not strict: the cache may temporarily exceed it while waiting for files to be 72 * deleted. The limit does not include filesystem overhead or the cache 73 * journal so space-sensitive applications should set a conservative limit. 74 * 75 * <p>Clients call {@link #edit} to create or update the values of an entry. An 76 * entry may have only one editor at one time; if a value is not available to be 77 * edited then {@link #edit} will return null. 78 * <ul> 79 * <li>When an entry is being <strong>created</strong> it is necessary to 80 * supply a full set of values; the empty value should be used as a 81 * placeholder if necessary. 82 * <li>When an entry is being <strong>edited</strong>, it is not necessary 83 * to supply data for every value; values default to their previous 84 * value. 85 * </ul> 86 * Every {@link #edit} call must be matched by a call to {@link Editor#commit} 87 * or {@link Editor#abort}. Committing is atomic: a read observes the full set 88 * of values as they were before or after the commit, but never a mix of values. 89 * 90 * <p>Clients call {@link #get} to read a snapshot of an entry. The read will 91 * observe the value at the time that {@link #get} was called. Updates and 92 * removals after the call do not impact ongoing reads. 93 * 94 * <p>This class is tolerant of some I/O errors. If files are missing from the 95 * filesystem, the corresponding entries will be dropped from the cache. If 96 * an error occurs while writing a cache value, the edit will fail silently. 97 * Callers should handle other problems by catching {@code IOException} and 98 * responding appropriately. 99 */100 public final class DiskLruCache implements Closeable {101 static final String JOURNAL_FILE = "journal";102 static final String JOURNAL_FILE_TMP = "journal.tmp";103 static final String MAGIC = "libcore.io.DiskLruCache";104 static final String VERSION_1 = "1";105 static final long ANY_SEQUENCE_NUMBER = -1;106 private static final String CLEAN = "CLEAN";107 private static final String DIRTY = "DIRTY";108 private static final String REMOVE = "REMOVE";109 private static final String READ = "READ";110 111 private static final Charset UTF_8 = Charset.forName("UTF-8");112 private static final int IO_BUFFER_SIZE = 8 * 1024;113 114 /*115 * This cache uses a journal file named "journal". A typical journal file116 * looks like this:117 * libcore.io.DiskLruCache118 * 1119 * 100120 * 2121 *122 * CLEAN 3400330d1dfc7f3f7f4b8d4d803dfcf6 832 21054123 * DIRTY 335c4c6028171cfddfbaae1a9c313c52124 * CLEAN 335c4c6028171cfddfbaae1a9c313c52 3934 2342125 * REMOVE 335c4c6028171cfddfbaae1a9c313c52126 * DIRTY 1ab96a171faeeee38496d8b330771a7a127 * CLEAN 1ab96a171faeeee38496d8b330771a7a 1600 234128 * READ 335c4c6028171cfddfbaae1a9c313c52129 * READ 3400330d1dfc7f3f7f4b8d4d803dfcf6130 *131 * The first five lines of the journal form its header. They are the132 * constant string "libcore.io.DiskLruCache", the disk cache's version,133 * the application's version, the value count, and a blank line.134 *135 * Each of the subsequent lines in the file is a record of the state of a136 * cache entry. Each line contains space-separated values: a state, a key,137 * and optional state-specific values.138 * o DIRTY lines track that an entry is actively being created or updated.139 * Every successful DIRTY action should be followed by a CLEAN or REMOVE140 * action. DIRTY lines without a matching CLEAN or REMOVE indicate that141 * temporary files may need to be deleted.142 * o CLEAN lines track a cache entry that has been successfully published143 * and may be read. A publish line is followed by the lengths of each of144 * its values.145 * o READ lines track accesses for LRU.146 * o REMOVE lines track entries that have been deleted.147 *148 * The journal file is appended to as cache Operations occur. The journal may149 * occasionally be compacted by dropping redundant lines. A temporary file named150 * "journal.tmp" will be used during compaction; that file should be deleted if151 * it exists when the cache is opened.152 */153 154 private final File directory;155 private final File journalFile;156 private final File journalFileTmp;157 private final int appVersion;158 private final long maxSize;159 private final int valueCount;160 private long size = 0;161 private Writer journalWriter;162 private final LinkedHashMap<String, Entry> lruEntries163 = new LinkedHashMap<String, Entry>(0, 0.75f, true);164 private int redundantOpCount;165 166 /**167 * To differentiate between old and current snapshots, each entry is given168 * a sequence number each time an edit is committed. A snapshot is stale if169 * its sequence number is not equal to its entry's sequence number.170 */171 private long nextSequenceNumber = 0;172 173 /* From java.util.Arrays */174 @SuppressWarnings("unchecked")175 private static <T> T[] copyOfRange(T[] original, int start, int end) {176 final int originalLength = original.length; // For exception priority compatibility.177 if (start > end) {178 throw new IllegalArgumentException();179 }180 if (start < 0 || start > originalLength) {181 throw new ArrayIndexOutOfBoundsException();182 }183 final int resultLength = end - start;184 final int copyLength = Math.min(resultLength, originalLength - start);185 final T[] result = (T[]) Array186 .newInstance(original.getClass().getComponentType(), resultLength);187 System.arraycopy(original, start, result, 0, copyLength);188 return result;189 }190 191 /**192 * Returns the remainder of 'reader' as a string, closing it when done.193 */194 public static String readFully(Reader reader) throws IOException {195 try {196 StringWriter writer = new StringWriter();197 char[] buffer = new char[1024];198 int count;199 while ((count = reader.read(buffer)) != -1) {200 writer.write(buffer, 0, count);201 }202 return writer.toString();203 } finally {204 reader.close();205 }206 }207 208 /**209 * Returns the ASCII characters up to but not including the next "/r/n", or210 * "/n".211 *212 * @throws java.io.EOFException if the stream is exhausted before the next newline213 * character.214 */215 public static String readAsciiLine(InputStream in) throws IOException {216 // TODO: support UTF-8 here instead217 218 StringBuilder result = new StringBuilder(80);219 while (true) {220 int c = in.read();221 if (c == -1) {222 throw new EOFException();223 } else if (c == '/n') {224 break;225 }226 227 result.append((char) c);228 }229 int length = result.length();230 if (length > 0 && result.charAt(length - 1) == '/r') {231 result.setLength(length - 1);232 }233 return result.toString();234 }235 236 /**237 * Closes 'closeable', ignoring any checked exceptions. Does nothing if 'closeable' is null.238 */239 public static void closeQuietly(Closeable closeable) {240 if (closeable != null) {241 try {242 closeable.close();243 } catch (RuntimeException rethrown) {244 throw rethrown;245 } catch (Exception ignored) {246 }247 }248 }249 250 /**251 * Recursively delete everything in {@code dir}.252 */253 // TODO: this should specify paths as Strings rather than as Files254 public static void deleteContents(File dir) throws IOException {255 File[] files = dir.listFiles();256 if (files == null) {257 throw new IllegalArgumentException("not a directory: " + dir);258 }259 for (File file : files) {260 if (file.isDirectory()) {261 deleteContents(file);262 }263 if (!file.delete()) {264 throw new IOException("failed to delete file: " + file);265 }266 }267 }268 269 /** This cache uses a single background thread to evict entries. */270 private final ExecutorService executorService = new ThreadPoolExecutor(0, 1,271 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());272 private final Callable<Void> cleanupCallable = new Callable<Void>() {273 @Override public Void call() throws Exception {274 synchronized (DiskLruCache.this) {275 if (journalWriter == null) {276 return null; // closed277 }278 trimToSize();279 if (journalRebuildRequired()) {280 rebuildJournal();281 redundantOpCount = 0;282 }283 }284 return null;285 }286 };287 288 private DiskLruCache(File directory, int appVersion, int valueCount, long maxSize) {289 this.directory = directory;290 this.appVersion = appVersion;291 this.journalFile = new File(directory, JOURNAL_FILE);292 this.journalFileTmp = new File(directory, JOURNAL_FILE_TMP);293 this.valueCount = valueCount;294 this.maxSize = maxSize;295 }296 297 /**298 * Opens the cache in {@code directory}, creating a cache if none exists299 * there.300 *301 * @param directory a writable directory302 * @param appVersion303 * @param valueCount the number of values per cache entry. Must be positive.304 * @param maxSize the maximum number of bytes this cache should use to store305 * @throws java.io.IOException if reading or writing the cache directory fails306 */307 public static DiskLruCache open(File directory, int appVersion, int valueCount, long maxSize)308 throws IOException {309 if (maxSize <= 0) {310 throw new IllegalArgumentException("maxSize <= 0");311 }312 if (valueCount <= 0) {313 throw new IllegalArgumentException("valueCount <= 0");314 }315 316 // prefer to pick up where we left off317 DiskLruCache cache = new DiskLruCache(directory, appVersion, valueCount, maxSize);318 if (cache.journalFile.exists()) {319 try {320 cache.readJournal();321 cache.processJournal();322 cache.journalWriter = new BufferedWriter(new FileWriter(cache.journalFile, true),323 IO_BUFFER_SIZE);324 return cache;325 } catch (IOException journalIsCorrupt) {326 // System.logW("DiskLruCache " + directory + " is corrupt: "327 // + journalIsCorrupt.getMessage() + ", removing");328 cache.delete();329 }330 }331 332 // create a new empty cache333 directory.mkdirs();334 cache = new DiskLruCache(directory, appVersion, valueCount, maxSize);335 cache.rebuildJournal();336 return cache;337 }338 339 private void readJournal() throws IOException {340 InputStream in = new BufferedInputStream(new FileInputStream(journalFile), IO_BUFFER_SIZE);341 try {342 String magic = readAsciiLine(in);343 String version = readAsciiLine(in);344 String appVersionString = readAsciiLine(in);345 String valueCountString = readAsciiLine(in);346 String blank = readAsciiLine(in);347 if (!MAGIC.equals(magic)348 || !VERSION_1.equals(version)349 || !Integer.toString(appVersion).equals(appVersionString)350 || !Integer.toString(valueCount).equals(valueCountString)351 || !"".equals(blank)) {352 throw new IOException("unexpected journal header: ["353 + magic + ", " + version + ", " + valueCountString + ", " + blank + "]");354 }355 356 while (true) {357 try {358 readJournalLine(readAsciiLine(in));359 } catch (EOFException endOfJournal) {360 break;361 }362 }363 } finally {364 closeQuietly(in);365 }366 }367 368 private void readJournalLine(String line) throws IOException {369 String[] parts = line.split(" ");370 if (parts.length < 2) {371 throw new IOException("unexpected journal line: " + line);372 }373 374 String key = parts[1];375 if (parts[0].equals(REMOVE) && parts.length == 2) {376 lruEntries.remove(key);377 return;378 }379 380 Entry entry = lruEntries.get(key);381 if (entry == null) {382 entry = new Entry(key);383 lruEntries.put(key, entry);384 }385 386 if (parts[0].equals(CLEAN) && parts.length == 2 + valueCount) {387 entry.readable = true;388 entry.currentEditor = null;389 entry.setLengths(copyOfRange(parts, 2, parts.length));390 } else if (parts[0].equals(DIRTY) && parts.length == 2) {391 entry.currentEditor = new Editor(entry);392 } else if (parts[0].equals(READ) && parts.length == 2) {393 // this work was already done by calling lruEntries.get()394 } else {395 throw new IOException("unexpected journal line: " + line);396 }397 }398 399 /**400 * Computes the initial size and collects garbage as a part of opening the401 * cache. Dirty entries are assumed to be inconsistent and will be deleted.402 */403 private void processJournal() throws IOException {404 deleteIfExists(journalFileTmp);405 for (Iterator<Entry> i = lruEntries.values().iterator(); i.hasNext(); ) {406 Entry entry = i.next();407 if (entry.currentEditor == null) {408 for (int t = 0; t < valueCount; t++) {409 size += entry.lengths[t];410 }411 } else {412 entry.currentEditor = null;413 for (int t = 0; t < valueCount; t++) {414 deleteIfExists(entry.getCleanFile(t));415 deleteIfExists(entry.getDirtyFile(t));416 }417 i.remove();418 }419 }420 }421 422 /**423 * Creates a new journal that omits redundant information. This replaces the424 * current journal if it exists.425 */426 private synchronized void rebuildJournal() throws IOException {427 if (journalWriter != null) {428 journalWriter.close();429 }430 431 Writer writer = new BufferedWriter(new FileWriter(journalFileTmp), IO_BUFFER_SIZE);432 writer.write(MAGIC);433 writer.write("/n");434 writer.write(VERSION_1);435 writer.write("/n");436 writer.write(Integer.toString(appVersion));437 writer.write("/n");438 writer.write(Integer.toString(valueCount));439 writer.write("/n");440 writer.write("/n");441 442 for (Entry entry : lruEntries.values()) {443 if (entry.currentEditor != null) {444 writer.write(DIRTY + ' ' + entry.key + '/n');445 } else {446 writer.write(CLEAN + ' ' + entry.key + entry.getLengths() + '/n');447 }448 }449 450 writer.close();451 journalFileTmp.renameTo(journalFile);452 journalWriter = new BufferedWriter(new FileWriter(journalFile, true), IO_BUFFER_SIZE);453 }454 455 private static void deleteIfExists(File file) throws IOException {456 // try {457 // Libcore.os.remove(file.getPath());458 // } catch (ErrnoException errnoException) {459 // if (errnoException.errno != OsConstants.ENOENT) {460 // throw errnoException.rethrowAsIOException();461 // }462 // }463 if (file.exists() && !file.delete()) {464 throw new IOException();465 }466 }467 468 /**469 * Returns a snapshot of the entry named {@code key}, or null if it doesn't470 * exist is not currently readable. If a value is returned, it is moved to471 * the head of the LRU queue.472 */473 public synchronized Snapshot get(String key) throws IOException {474 checkNotClosed();475 validateKey(key);476 Entry entry = lruEntries.get(key);477 if (entry == null) {478 return null;479 }480 481 if (!entry.readable) {482 return null;483 }484 485 /*486 * Open all streams eagerly to guarantee that we see a single published487 * snapshot. If we opened streams lazily then the streams could come488 * from different edits.489 */490 InputStream[] ins = new InputStream[valueCount];491 try {492 for (int i = 0; i < valueCount; i++) {493 ins[i] = new FileInputStream(entry.getCleanFile(i));494 }495 } catch (FileNotFoundException e) {496 // a file must have been deleted manually!497 return null;498 }499 500 redundantOpCount++;501 journalWriter.append(READ + ' ' + key + '/n');502 if (journalRebuildRequired()) {503 executorService.submit(cleanupCallable);504 }505 506 return new Snapshot(key, entry.sequenceNumber, ins);507 }508 509 /**510 * Returns an editor for the entry named {@code key}, or null if another511 * edit is in progress.512 */513 public Editor edit(String key) throws IOException {514 return edit(key, ANY_SEQUENCE_NUMBER);515 }516 517 private synchronized Editor edit(String key, long expectedSequenceNumber) throws IOException {518 checkNotClosed();519 validateKey(key);520 Entry entry = lruEntries.get(key);521 if (expectedSequenceNumber != ANY_SEQUENCE_NUMBER522 && (entry == null || entry.sequenceNumber != expectedSequenceNumber)) {523 return null; // snapshot is stale524 }525 if (entry == null) {526 entry = new Entry(key);527 lruEntries.put(key, entry);528 } else if (entry.currentEditor != null) {529 return null; // another edit is in progress530 }531 532 Editor editor = new Editor(entry);533 entry.currentEditor = editor;534 535 // flush the journal before creating files to prevent file leaks536 journalWriter.write(DIRTY + ' ' + key + '/n');537 journalWriter.flush();538 return editor;539 }540 541 /**542 * Returns the directory where this cache stores its data.543 */544 public File getDirectory() {545 return directory;546 }547 548 /**549 * Returns the maximum number of bytes that this cache should use to store550 * its data.551 */552 public long maxSize() {553 return maxSize;554 }555 556 /**557 * Returns the number of bytes currently being used to store the values in558 * this cache. This may be greater than the max size if a background559 * deletion is pending.560 */561 public synchronized long size() {562 return size;563 }564 565 private synchronized void completeEdit(Editor editor, boolean success) throws IOException {566 Entry entry = editor.entry;567 if (entry.currentEditor != editor) {568 throw new IllegalStateException();569 }570 571 // if this edit is creating the entry for the first time, every index must have a value572 if (success && !entry.readable) {573 for (int i = 0; i < valueCount; i++) {574 if (!entry.getDirtyFile(i).exists()) {575 editor.abort();576 throw new IllegalStateException("edit didn't create file " + i);577 }578 }579 }580 581 for (int i = 0; i < valueCount; i++) {582 File dirty = entry.getDirtyFile(i);583 if (success) {584 if (dirty.exists()) {585 File clean = entry.getCleanFile(i);586 dirty.renameTo(clean);587 long oldLength = entry.lengths[i];588 long newLength = clean.length();589 entry.lengths[i] = newLength;590 size = size - oldLength + newLength;591 }592 } else {593 deleteIfExists(dirty);594 }595 }596 597 redundantOpCount++;598 entry.currentEditor = null;599 if (entry.readable | success) {600 entry.readable = true;601 journalWriter.write(CLEAN + ' ' + entry.key + entry.getLengths() + '/n');602 if (success) {603 entry.sequenceNumber = nextSequenceNumber++;604 }605 } else {606 lruEntries.remove(entry.key);607 journalWriter.write(REMOVE + ' ' + entry.key + '/n');608 }609 610 if (size > maxSize || journalRebuildRequired()) {611 executorService.submit(cleanupCallable);612 }613 }614 615 /**616 * We only rebuild the journal when it will halve the size of the journal617 * and eliminate at least 2000 ops.618 */619 private boolean journalRebuildRequired() {620 final int REDUNDANT_OP_COMPACT_THRESHOLD = 2000;621 return redundantOpCount >= REDUNDANT_OP_COMPACT_THRESHOLD622 && redundantOpCount >= lruEntries.size();623 }624 625 /**626 * Drops the entry for {@code key} if it exists and can be removed. Entries627 * actively being edited cannot be removed.628 *629 * @return true if an entry was removed.630 */631 public synchronized boolean remove(String key) throws IOException {632 checkNotClosed();633 validateKey(key);634 Entry entry = lruEntries.get(key);635 if (entry == null || entry.currentEditor != null) {636 return false;637 }638 639 for (int i = 0; i < valueCount; i++) {640 File file = entry.getCleanFile(i);641 if (!file.delete()) {642 throw new IOException("failed to delete " + file);643 }644 size -= entry.lengths[i];645 entry.lengths[i] = 0;646 }647 648 redundantOpCount++;649 journalWriter.append(REMOVE + ' ' + key + '/n');650 lruEntries.remove(key);651 652 if (journalRebuildRequired()) {653 executorService.submit(cleanupCallable);654 }655 656 return true;657 }658 659 /**660 * Returns true if this cache has been closed.661 */662 public boolean isClosed() {663 return journalWriter == null;664 }665 666 private void checkNotClosed() {667 if (journalWriter == null) {668 throw new IllegalStateException("cache is closed");669 }670 }671 672 /**673 * Force buffered operations to the filesystem.674 */675 public synchronized void flush() throws IOException {676 checkNotClosed();677 trimToSize();678 journalWriter.flush();679 }680 681 /**682 * Closes this cache. Stored values will remain on the filesystem.683 */684 public synchronized void close() throws IOException {685 if (journalWriter == null) {686 return; // already closed687 }688 for (Entry entry : new ArrayList<Entry>(lruEntries.values())) {689 if (entry.currentEditor != null) {690 entry.currentEditor.abort();691 }692 }693 trimToS
新闻热点
疑难解答