Skip to content

Commit

Permalink
Allow adding a cache listener
Browse files Browse the repository at this point in the history
  • Loading branch information
mizosoft committed Dec 15, 2024
1 parent 515319e commit d5a6d17
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ interface CacheSpec {

fun executor(executor: Executor)

fun listener(listener: CacheListener)

fun synchronizeWrites(synchronizeWrites: Boolean = true)
}

Expand All @@ -56,6 +58,10 @@ private class CacheFactorySpec(private val builder: HttpCache.Builder = Cache.ne
builder.cacheOn(storageExtension)
}

override fun listener(listener: CacheListener) {
builder.listener(listener)
}

override fun executor(executor: Executor) {
builder.executor(executor)
}
Expand Down Expand Up @@ -84,6 +90,8 @@ private class CacheChainFactorySpec(private val caches: MutableList<Cache> = Arr

typealias Cache = HttpCache

typealias CacheListener = HttpCache.Listener

/** A series of caches invoked sequentially during an HTTP call. */
typealias CacheChain = List<Cache>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ public static final class Builder {
/** Specifies that HTTP responses are to be cached on memory with the given size bound. */
@CanIgnoreReturnValue
public Builder cacheOnMemory(long maxSize) {
return cacheOn(StorageExtension.memory(maxSize));
return cacheOn(StorageExtension.inMemory(maxSize));
}

/**
Expand All @@ -1041,15 +1041,16 @@ public Builder cacheOnMemory(long maxSize) {
*/
@CanIgnoreReturnValue
public Builder cacheOnDisk(Path directory, long maxSize) {
return cacheOn(StorageExtension.disk(directory, maxSize));
return cacheOn(StorageExtension.onDisk(directory, maxSize));
}

/** Specifies that HTTP responses are to be persisted on the given storage extension. */
@CanIgnoreReturnValue
public Builder cacheOn(StorageExtension storageExtension) {
requireNonNull(storageExtension);
requireArgument(
storageExtension instanceof InternalStorageExtension,
"unrecognized StorageExtension: %s",
"Unrecognized StorageExtension: %s",
storageExtension);
this.storageExtension = (InternalStorageExtension) storageExtension;
return this;
Expand Down Expand Up @@ -1099,7 +1100,7 @@ Builder clock(Clock clock) {

private InternalStorageExtension storageExtension() {
var storeExtension = this.storageExtension;
requireState(storeExtension != null, "a storage backend must be specified");
requireState(storeExtension != null, "A storage backend must be specified");
return storeExtension;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,20 @@

/** An extension that provides a storage backend for an {@link HttpCache}. */
public interface StorageExtension {
static StorageExtension memory(long maxSize) {
requireArgument(maxSize > 0, "non-positive maxSize: %d", maxSize);

/** Returns a {@code StorageExtension} for saving data in memory, not exceeding the given size. */
static StorageExtension inMemory(long maxSize) {
requireArgument(maxSize > 0, "Non-positive maxSize: %d", maxSize);
return (InternalStorageExtension) (executor, appVersion) -> new MemoryStore(maxSize);
}

static StorageExtension disk(Path directory, long maxSize) {
/**
* Returns a {@code StorageExtension} for saving data on disk under the given directory, not
* exceeding the given size.
*
* @throws IllegalArgumentException if {@code maxSize} is not positive
*/
static StorageExtension onDisk(Path directory, long maxSize) {
requireNonNull(directory);
requireArgument(maxSize > 0, "Non-positive maxSize: %d", maxSize);
return (InternalStorageExtension)
Expand Down

0 comments on commit d5a6d17

Please sign in to comment.