Skip to content

Commit

Permalink
Created builder for DiskBasedAsyncCache (google#360)
Browse files Browse the repository at this point in the history
Co-authored-by: Scott Phillips <[email protected]>
Co-authored-by: Jeff Davidson <[email protected]>
  • Loading branch information
3 people committed Aug 19, 2020
1 parent 3ff2427 commit d777e8a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 11 deletions.
66 changes: 57 additions & 9 deletions src/main/java/com/android/volley/toolbox/DiskBasedAsyncCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,10 @@ public class DiskBasedAsyncCache extends AsyncCache {
/**
* Constructs an instance of the DiskBasedAsyncCache at the specified directory.
*
* @param rootDirectory The root directory of the cache.
* @param rootDirectorySupplier The root directory supplier of the cache.
*/
public DiskBasedAsyncCache(final File rootDirectory, int maxCacheSizeInBytes) {
mRootDirectorySupplier =
new FileSupplier() {
@Override
public File get() {
return rootDirectory;
}
};
private DiskBasedAsyncCache(FileSupplier rootDirectorySupplier, int maxCacheSizeInBytes) {
mRootDirectorySupplier = rootDirectorySupplier;
mMaxCacheSizeInBytes = maxCacheSizeInBytes;
}

Expand Down Expand Up @@ -411,4 +405,58 @@ private void deleteFileAndInvokeCallback(
mTotalSize = DiskBasedCacheUtility.removeEntry(key, mTotalSize, mEntries);
callback.onGetComplete(null);
}

/**
* Builder is used to build an instance of {@link DiskBasedAsyncCache} from values configured by
* the setters.
*/
public static class Builder {
@Nullable private FileSupplier rootDirectorySupplier = null;
@Nullable private File rootDirectory = null;
private int maxCacheSizeInBytes = DiskBasedCacheUtility.DEFAULT_DISK_USAGE_BYTES;

/**
* Sets the root directory of the cache. Must be called if {@link
* Builder#setRootDirectorySupplier(FileSupplier)} is not.
*/
public Builder setRootDirectory(File rootDirectory) {
this.rootDirectory = rootDirectory;
return this;
}

/**
* Sets the root directory supplier of the cache. Must be called if {@link
* Builder#setRootDirectory(File)} is not.
*/
public Builder setRootDirectorySupplier(FileSupplier rootDirectorySupplier) {
this.rootDirectorySupplier = rootDirectorySupplier;
return this;
}

/**
* Sets the max size of the cache in bytes. Will default to {@link
* DiskBasedCacheUtility#DEFAULT_DISK_USAGE_BYTES} if not called.
*/
public Builder setMaxCacheSizeInBytes(int maxCacheSizeInBytes) {
this.maxCacheSizeInBytes = maxCacheSizeInBytes;
return this;
}

/** Builds a DiskBasedAsyncCache from the provided parameters. */
public DiskBasedAsyncCache build() {
if (rootDirectory == null && rootDirectorySupplier == null) {
throw new IllegalArgumentException("Must set either file or supplier");
}
if (rootDirectorySupplier == null) {
rootDirectorySupplier =
new FileSupplier() {
@Override
public File get() {
return rootDirectory;
}
};
}
return new DiskBasedAsyncCache(rootDirectorySupplier, maxCacheSizeInBytes);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ public void onWriteComplete() {
}
};
// Initialize empty cache
cache = new DiskBasedAsyncCache(temporaryFolder.getRoot(), MAX_SIZE);
cache =
new DiskBasedAsyncCache.Builder()
.setRootDirectory(temporaryFolder.getRoot())
.setMaxCacheSizeInBytes(MAX_SIZE)
.build();
cache.initialize(futureCallback);
future.get();
}
Expand Down Expand Up @@ -310,7 +314,11 @@ public void testReinitialize() throws ExecutionException, InterruptedException {
Cache.Entry entry = CacheTestUtils.randomData(1023);
putEntry("key", entry).get();

final AsyncCache copy = new DiskBasedAsyncCache(temporaryFolder.getRoot(), MAX_SIZE);
final AsyncCache copy =
new DiskBasedAsyncCache.Builder()
.setRootDirectory(temporaryFolder.getRoot())
.setMaxCacheSizeInBytes(MAX_SIZE)
.build();
final CompletableFuture<Cache.Entry> getEntry = new CompletableFuture<>();
copy.initialize(
new AsyncCache.OnWriteCompleteCallback() {
Expand Down

0 comments on commit d777e8a

Please sign in to comment.