Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class DiskLruCacheWrapper implements DiskCache {
private final SafeKeyGenerator safeKeyGenerator;
private final File directory;
private final long maxSize;
private final boolean memoizePathNames;
private final DiskCacheWriteLocker writeLocker = new DiskCacheWriteLocker();
private DiskLruCache diskLruCache;

Expand Down Expand Up @@ -60,7 +61,24 @@ public static synchronized DiskCache get(File directory, long maxSize) {
*/
@SuppressWarnings("deprecation")
public static DiskCache create(File directory, long maxSize) {
return new DiskLruCacheWrapper(directory, maxSize);
return new DiskLruCacheWrapper(directory, maxSize, /* memoizePathNames= */ true);
}

/**
* Create a new DiskCache in the given directory with a specified max size and memoization
* behavior.
*
* @param directory The directory for the disk cache
* @param maxSize The max size for the disk cache
* @param memoizePathNames Whether to memoize path names
* @return The new disk cache with the given arguments
* @deprecated Disabling the memoization is an experimental setting that may be removed in a
* future version.
*/
@SuppressWarnings("deprecation")
@Deprecated
public static DiskCache create(File directory, long maxSize, boolean memoizePathNames) {
return new DiskLruCacheWrapper(directory, maxSize, memoizePathNames);
}

/**
Expand All @@ -70,14 +88,21 @@ public static DiskCache create(File directory, long maxSize) {
// Deprecated public API.
@SuppressWarnings({"WeakerAccess", "DeprecatedIsStillUsed"})
protected DiskLruCacheWrapper(File directory, long maxSize) {
this(directory, maxSize, /* memoizePathNames= */ true);
}

protected DiskLruCacheWrapper(File directory, long maxSize, boolean memoizePathNames) {
this.directory = directory;
this.maxSize = maxSize;
this.memoizePathNames = memoizePathNames;
this.safeKeyGenerator = new SafeKeyGenerator();
}

private synchronized DiskLruCache getDiskCache() throws IOException {
if (diskLruCache == null) {
diskLruCache = DiskLruCache.open(directory, APP_VERSION, VALUE_COUNT, maxSize);
diskLruCache =
DiskLruCache.experimentalOpen(
directory, APP_VERSION, VALUE_COUNT, maxSize, memoizePathNames);
}
return diskLruCache;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public final class DiskLruCache implements Closeable {
private final int appVersion;
private long maxSize;
private final int valueCount;
private final boolean memoizePathNames;
private long size = 0;
private Writer journalWriter;
private final LinkedHashMap<String, Entry> lruEntries =
Expand Down Expand Up @@ -179,27 +180,50 @@ public Void call() throws Exception {
}
};

private DiskLruCache(File directory, int appVersion, int valueCount, long maxSize) {
private DiskLruCache(
File directory, int appVersion, int valueCount, long maxSize, boolean memoizePathNames) {
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;
this.memoizePathNames = memoizePathNames;
}

/**
* Opens the cache in {@code directory}, creating a cache if none exists
* there.
* Opens the cache in {@code directory}, creating a cache if none exists there.
*
* @param directory a writable directory
* @param appVersion the application's current version code
* @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 {
return experimentalOpen(
directory, appVersion, valueCount, maxSize, /* memoizePathNames= */ true);
}

/**
* Opens the cache in {@code directory}, creating a cache if none exists there, with explicit
* control over path name memoization.
*
* <p>Disabling the memoization is an experimental setting that may be removed in a future
* version.
*
* @param directory a writable directory
* @param appVersion the application's current version code
* @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
* @param memoizePathNames whether to memoize path names
* @return The new disk cache with the given arguments
*/
public static DiskLruCache experimentalOpen(
File directory, int appVersion, int valueCount, long maxSize, boolean memoizePathNames)
throws IOException {
if (maxSize <= 0) {
throw new IllegalArgumentException("maxSize <= 0");
}
Expand All @@ -220,7 +244,8 @@ public static DiskLruCache open(File directory, int appVersion, int valueCount,
}

// Prefer to pick up where we left off.
DiskLruCache cache = new DiskLruCache(directory, appVersion, valueCount, maxSize);
DiskLruCache cache =
new DiskLruCache(directory, appVersion, valueCount, maxSize, memoizePathNames);
if (cache.journalFile.exists()) {
try {
cache.readJournal();
Expand All @@ -239,7 +264,7 @@ public static DiskLruCache open(File directory, int appVersion, int valueCount,

// Create a new empty cache.
directory.mkdirs();
cache = new DiskLruCache(directory, appVersion, valueCount, maxSize);
cache = new DiskLruCache(directory, appVersion, valueCount, maxSize, memoizePathNames);
cache.rebuildJournal();
return cache;
}
Expand Down Expand Up @@ -420,7 +445,8 @@ public synchronized Value get(String key) throws IOException {
return null;
}

for (File file : entry.cleanFiles) {
for (int i = 0; i < valueCount; i++) {
File file = entry.getCleanFile(i);
// A file must have been deleted manually!
if (!file.exists()) {
return null;
Expand Down Expand Up @@ -740,12 +766,15 @@ public Editor edit() throws IOException {
}

public File getFile(int index) {
if (files != null) {
return files[index];
}
return new File(directory, key + "." + index);
}

/** Returns the string value for {@code index}. */
public String getString(int index) throws IOException {
InputStream is = new FileInputStream(files[index]);
InputStream is = new FileInputStream(getFile(index));
return inputStreamToString(is);
}

Expand Down Expand Up @@ -874,18 +903,20 @@ private final class Entry {
private Entry(String key) {
this.key = key;
this.lengths = new long[valueCount];
cleanFiles = new File[valueCount];
dirtyFiles = new File[valueCount];

// The names are repetitive so re-use the same builder to avoid allocations.
StringBuilder fileBuilder = new StringBuilder(key).append('.');
int truncateTo = fileBuilder.length();
for (int i = 0; i < valueCount; i++) {
fileBuilder.append(i);
cleanFiles[i] = new File(directory, fileBuilder.toString());
fileBuilder.append(".tmp");
dirtyFiles[i] = new File(directory, fileBuilder.toString());
fileBuilder.setLength(truncateTo);
if (memoizePathNames) {
cleanFiles = new File[valueCount];
dirtyFiles = new File[valueCount];
// The names are repetitive so re-use the same builder to avoid allocations.
StringBuilder fileBuilder = new StringBuilder(key).append('.');
int truncateTo = fileBuilder.length();
for (int i = 0; i < valueCount; i++) {
fileBuilder.append(i);
cleanFiles[i] = new File(directory, fileBuilder.toString());
fileBuilder.append(".tmp");
dirtyFiles[i] = new File(directory, fileBuilder.toString());
fileBuilder.setLength(truncateTo);
}
}
}

Expand Down Expand Up @@ -917,11 +948,17 @@ private IOException invalidLengths(String[] strings) throws IOException {
}

public File getCleanFile(int i) {
return cleanFiles[i];
if (cleanFiles != null) {
return cleanFiles[i];
}
return new File(directory, key + "." + i);
}

public File getDirtyFile(int i) {
return dirtyFiles[i];
if (dirtyFiles != null) {
return dirtyFiles[i];
}
return new File(directory, key + "." + i + ".tmp");
}
}

Expand Down
Loading