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 22, 2024
1 parent 6f279fb commit 0344165
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 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 @@ -34,7 +34,7 @@ suspend inline fun <reified T> ResponsePayload.to(): T {
}

/** Converts this payload to [T] using the given body handler. */
suspend fun <T> ResponsePayload.with(bodyHandler: BodyHandler<T>): T {
suspend fun <T> ResponsePayload.getWith(bodyHandler: BodyHandler<T>): T {
return handleWithAsync(bodyHandler).await()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,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 @@ -1071,15 +1071,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 @@ -1129,7 +1130,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
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ private boolean isCacheable(HttpRequest request, TrackedResponse<?> response) {

Set<String> varyFields;
try {
// Don't crash because of server's ill-formed Vary.
varyFields = CacheResponseMetadata.varyFields(response.headers());
} catch (IllegalArgumentException e) {
// Don't crash because of server's ill-formed Vary.
logger.log(Level.WARNING, "Invalid response Vary", e);
return false;
}
Expand Down

0 comments on commit 0344165

Please sign in to comment.