Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 36 additions & 22 deletions src/main/java/com/jakewharton/disklrucache/DiskLruCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public final class DiskLruCache implements Closeable {
private static final String REMOVE = "REMOVE";
private static final String READ = "READ";

/*
/*
* This cache uses a journal file named "journal". A typical journal file
* looks like this:
* libcore.io.DiskLruCache
Expand Down Expand Up @@ -149,7 +149,7 @@ public final class DiskLruCache implements Closeable {
private long size = 0;
private Writer journalWriter;
private final LinkedHashMap<String, Entry> lruEntries =
new LinkedHashMap<String, Entry>(0, 0.75f, true);
new LinkedHashMap<String, Entry>(0, 0.75f, true);
private int redundantOpCount;

/**
Expand All @@ -161,7 +161,7 @@ public final class DiskLruCache implements Closeable {

/** This cache uses a single background thread to evict entries. */
final ThreadPoolExecutor executorService =
new ThreadPoolExecutor(0, 1, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
new ThreadPoolExecutor(0, 1, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
private final Callable<Void> cleanupCallable = new Callable<Void>() {
public Void call() throws Exception {
synchronized (DiskLruCache.this) {
Expand Down Expand Up @@ -198,7 +198,7 @@ private DiskLruCache(File directory, int appVersion, int valueCount, long maxSiz
* @throws IOException if reading or writing the cache directory fails
*/
public static DiskLruCache open(File directory, int appVersion, int valueCount, long maxSize)
throws IOException {
throws IOException {
if (maxSize <= 0) {
throw new IllegalArgumentException("maxSize <= 0");
}
Expand Down Expand Up @@ -227,11 +227,11 @@ public static DiskLruCache open(File directory, int appVersion, int valueCount,
return cache;
} catch (IOException journalIsCorrupt) {
System.out
.println("DiskLruCache "
+ directory
+ " is corrupt: "
+ journalIsCorrupt.getMessage()
+ ", removing");
.println("DiskLruCache "
+ directory
+ " is corrupt: "
+ journalIsCorrupt.getMessage()
+ ", removing");
cache.delete();
}
}
Expand All @@ -252,12 +252,12 @@ private void readJournal() throws IOException {
String valueCountString = reader.readLine();
String blank = reader.readLine();
if (!MAGIC.equals(magic)
|| !VERSION_1.equals(version)
|| !Integer.toString(appVersion).equals(appVersionString)
|| !Integer.toString(valueCount).equals(valueCountString)
|| !"".equals(blank)) {
|| !VERSION_1.equals(version)
|| !Integer.toString(appVersion).equals(appVersionString)
|| !Integer.toString(valueCount).equals(valueCountString)
|| !"".equals(blank)) {
throw new IOException("unexpected journal header: [" + magic + ", " + version + ", "
+ valueCountString + ", " + blank + "]");
+ valueCountString + ", " + blank + "]");
}

int lineCount = 0;
Expand All @@ -276,7 +276,7 @@ private void readJournal() throws IOException {
rebuildJournal();
} else {
journalWriter = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(journalFile, true), Util.US_ASCII));
new FileOutputStream(journalFile, true), Util.US_ASCII));
}
} finally {
Util.closeQuietly(reader);
Expand Down Expand Up @@ -355,7 +355,7 @@ private synchronized void rebuildJournal() throws IOException {
}

Writer writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(journalFileTmp), Util.US_ASCII));
new OutputStreamWriter(new FileOutputStream(journalFileTmp), Util.US_ASCII));
try {
writer.write(MAGIC);
writer.write("\n");
Expand Down Expand Up @@ -385,7 +385,7 @@ private synchronized void rebuildJournal() throws IOException {
journalFileBackup.delete();

journalWriter = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(journalFile, true), Util.US_ASCII));
new OutputStreamWriter(new FileOutputStream(journalFile, true), Util.US_ASCII));
}

private static void deleteIfExists(File file) throws IOException {
Expand Down Expand Up @@ -446,7 +446,14 @@ public synchronized Snapshot get(String key) throws IOException {
executorService.submit(cleanupCallable);
}

return new Snapshot(key, entry.sequenceNumber, ins, entry.lengths);
long createdTime = 0L;
try {
File file = entry.getCleanFile( 0 );
createdTime = file.lastModified();
} catch ( Exception e ) {
}

return new Snapshot(key, entry.sequenceNumber, ins, entry.lengths, createdTime);
}

/**
Expand All @@ -462,7 +469,7 @@ private synchronized Editor edit(String key, long expectedSequenceNumber) throws
validateKey(key);
Entry entry = lruEntries.get(key);
if (expectedSequenceNumber != ANY_SEQUENCE_NUMBER && (entry == null
|| entry.sequenceNumber != expectedSequenceNumber)) {
|| entry.sequenceNumber != expectedSequenceNumber)) {
return null; // Snapshot is stale.
}
if (entry == null) {
Expand Down Expand Up @@ -574,7 +581,7 @@ private synchronized void completeEdit(Editor editor, boolean success) throws IO
private boolean journalRebuildRequired() {
final int redundantOpCompactThreshold = 2000;
return redundantOpCount >= redundantOpCompactThreshold //
&& redundantOpCount >= lruEntries.size();
&& redundantOpCount >= lruEntries.size();
}

/**
Expand Down Expand Up @@ -679,12 +686,14 @@ public final class Snapshot implements Closeable {
private final long sequenceNumber;
private final InputStream[] ins;
private final long[] lengths;
private final long createdTime;

private Snapshot(String key, long sequenceNumber, InputStream[] ins, long[] lengths) {
private Snapshot(String key, long sequenceNumber, InputStream[] ins, long[] lengths, long createdTime ) {
this.key = key;
this.sequenceNumber = sequenceNumber;
this.ins = ins;
this.lengths = lengths;
this.createdTime = createdTime;
}

/**
Expand Down Expand Up @@ -716,6 +725,11 @@ public void close() {
Util.closeQuietly(in);
}
}

/** Returns cache entry create time */
public long getCreatedTime() {
return createdTime;
}
}

private static final OutputStream NULL_OUTPUT_STREAM = new OutputStream() {
Expand Down Expand Up @@ -940,4 +954,4 @@ public File getDirtyFile(int i) {
return new File(directory, key + "." + i + ".tmp");
}
}
}
}