# PrefetchCache

Artifact: [androidx.webkit:webkit](https://developer.android.com/jetpack/androidx/releases/webkit) [View Source](https://cs.android.com/search?q=file:androidx/webkit/PrefetchCache.java+class:androidx.webkit.PrefetchCache) Added in [1.16.0](https://developer.android.com/jetpack/androidx/releases/webkit#1.16.0)

*** ** * ** ***


```
@Profile.ExperimentalUrlPrefetch
public final class PrefetchCache
```

<br />

*** ** * ** ***

PrefetchCache manages the configuration of the prefetch cache for a `https://developer.android.com/reference/androidx/webkit/Profile`.

It allows applications to tune the behavior of prefetch cache by setting limits such as the maximum number of prefetches and the time-to-live for prefetched responses. These configurations are applied to all WebViews associated with the profile.

Use `https://developer.android.com/reference/androidx/webkit/Profile#getPrefetchCache()` to obtain the PrefetchCache instance for a specific profile.

## Summary

| ### Public methods |
|---|---|
| `void` | `@https://developer.android.com/reference/androidx/annotation/RequiresFeature(name = https://developer.android.com/reference/androidx/webkit/WebViewFeature.PREFETCH_CACHE_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") @https://developer.android.com/reference/androidx/annotation/UiThread @https://developer.android.com/reference/androidx/webkit/Profile.ExperimentalUrlPrefetch https://developer.android.com/reference/androidx/webkit/PrefetchCache#clearMaxPrefetches()()` Resets the maximum number of prefetches for the current browsing session to system default. |
| `void` | `@https://developer.android.com/reference/androidx/annotation/RequiresFeature(name = https://developer.android.com/reference/androidx/webkit/WebViewFeature.PREFETCH_CACHE_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") @https://developer.android.com/reference/androidx/annotation/UiThread @https://developer.android.com/reference/androidx/webkit/Profile.ExperimentalUrlPrefetch https://developer.android.com/reference/androidx/webkit/PrefetchCache#clearPrefetchTtl()()` Resets the maximum prefetch Time-to-Live (TTL) in seconds for the current browsing session to the system default. |
| `boolean` | `https://developer.android.com/reference/androidx/webkit/PrefetchCache#equals(java.lang.Object)(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/Object.html obj)` Compares this PrefetchCache with the specified object for equality. |
| `int` | `@https://developer.android.com/reference/androidx/annotation/RequiresFeature(name = https://developer.android.com/reference/androidx/webkit/WebViewFeature.PREFETCH_CACHE_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") @https://developer.android.com/reference/androidx/annotation/UiThread @https://developer.android.com/reference/androidx/webkit/Profile.ExperimentalUrlPrefetch https://developer.android.com/reference/androidx/webkit/PrefetchCache#getMaxPrefetches()()` Returns maximum prefetches set for this Profile. |
| `int` | `@https://developer.android.com/reference/androidx/annotation/RequiresFeature(name = https://developer.android.com/reference/androidx/webkit/WebViewFeature.PREFETCH_CACHE_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") @https://developer.android.com/reference/androidx/annotation/UiThread @https://developer.android.com/reference/androidx/webkit/Profile.ExperimentalUrlPrefetch https://developer.android.com/reference/androidx/webkit/PrefetchCache#getPrefetchTtlSeconds()()` Returns Prefetch TTL in Seconds set for this Profile. |
| `int` | `https://developer.android.com/reference/androidx/webkit/PrefetchCache#hashCode()()` Returns a hash code value for the object, consistent with `https://developer.android.com/reference/androidx/webkit/PrefetchCache#equals(java.lang.Object)`. |
| `void` | `@https://developer.android.com/reference/androidx/annotation/RequiresFeature(name = https://developer.android.com/reference/androidx/webkit/WebViewFeature.PROFILE_URL_PREFETCH, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") @https://developer.android.com/reference/androidx/annotation/AnyThread @https://developer.android.com/reference/androidx/webkit/Profile.ExperimentalUrlPrefetch https://developer.android.com/reference/androidx/webkit/PrefetchCache#prefetchUrlAsync(java.lang.String,android.os.CancellationSignal,java.util.concurrent.Executor,androidx.webkit.WebViewOutcomeReceiver<androidx.webkit.PrefetchResult,androidx.webkit.PrefetchException>)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html url, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/os/CancellationSignal.html cancellationSignal, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/util/concurrent/Executor.html callbackExecutor, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/webkit/WebViewOutcomeReceiver<@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/webkit/PrefetchResult, https://developer.android.com/reference/androidx/webkit/PrefetchException> outcomeReceiver )` Starts a URL prefetch request. |
| `void` | `@https://developer.android.com/reference/androidx/annotation/RequiresFeature(name = https://developer.android.com/reference/androidx/webkit/WebViewFeature.PROFILE_URL_PREFETCH, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") @https://developer.android.com/reference/androidx/annotation/AnyThread @https://developer.android.com/reference/androidx/webkit/Profile.ExperimentalUrlPrefetch https://developer.android.com/reference/androidx/webkit/PrefetchCache#prefetchUrlAsync(java.lang.String,android.os.CancellationSignal,java.util.concurrent.Executor,androidx.webkit.PrefetchParameters,androidx.webkit.WebViewOutcomeReceiver<androidx.webkit.PrefetchResult,androidx.webkit.PrefetchException>)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html url, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/os/CancellationSignal.html cancellationSignal, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/util/concurrent/Executor.html callbackExecutor, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/webkit/PrefetchParameters prefetchParameters, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/webkit/WebViewOutcomeReceiver<@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/webkit/PrefetchResult, https://developer.android.com/reference/androidx/webkit/PrefetchException> outcomeReceiver )` Starts a URL prefetch request. |
| `void` | `@https://developer.android.com/reference/androidx/annotation/RequiresFeature(name = https://developer.android.com/reference/androidx/webkit/WebViewFeature.PREFETCH_CACHE_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") @https://developer.android.com/reference/androidx/annotation/UiThread @https://developer.android.com/reference/androidx/webkit/Profile.ExperimentalUrlPrefetch https://developer.android.com/reference/androidx/webkit/PrefetchCache#setMaxPrefetches(int)(@https://developer.android.com/reference/androidx/annotation/IntRange(from = 1) int maxPrefetches)` Sets the maximum number of prefetches for the current browsing session. |
| `void` | `@https://developer.android.com/reference/androidx/annotation/RequiresFeature(name = https://developer.android.com/reference/androidx/webkit/WebViewFeature.PREFETCH_CACHE_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") @https://developer.android.com/reference/androidx/annotation/UiThread @https://developer.android.com/reference/androidx/webkit/Profile.ExperimentalUrlPrefetch https://developer.android.com/reference/androidx/webkit/PrefetchCache#setPrefetchTtlSeconds(int)(@https://developer.android.com/reference/androidx/annotation/IntRange(from = 1) int prefetchTtlSeconds)` Sets the maximum prefetch Time-to-Live (TTL) in seconds for the current browsing session. |

## Public methods

### clearMaxPrefetches

Added in [1.17.0](https://developer.android.com/jetpack/androidx/releases/webkit#1.17.0)

```
@RequiresFeature(name = WebViewFeature.PREFETCH_CACHE_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
public void clearMaxPrefetches()
```

Resets the maximum number of prefetches for the current browsing session to system default.

This configuration will be applied to any prefetch requests made after they are set; they will not be applied to in-flight requests.

This configuration will be applied to WebViews that are associated with the `https://developer.android.com/reference/androidx/webkit/Profile` that owns this `https://developer.android.com/reference/androidx/webkit/PrefetchCache`.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException.html` | if the `https://developer.android.com/reference/androidx/webkit/WebViewFeature#PREFETCH_CACHE_V1()` feature is not supported. This should be checked before use with `https://developer.android.com/reference/androidx/webkit/WebViewFeature#isFeatureSupported(java.lang.String)`. |

### clearPrefetchTtl

Added in [1.17.0](https://developer.android.com/jetpack/androidx/releases/webkit#1.17.0)

```
@RequiresFeature(name = WebViewFeature.PREFETCH_CACHE_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
public void clearPrefetchTtl()
```

Resets the maximum prefetch Time-to-Live (TTL) in seconds for the current browsing session to the system default.

This configuration will be applied to any prefetch requests made after they are set; they will not be applied to in-flight requests.

This configuration will be applied to WebViews that are associated with the `https://developer.android.com/reference/androidx/webkit/Profile` that owns this `https://developer.android.com/reference/androidx/webkit/PrefetchCache`.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException.html` | if the `https://developer.android.com/reference/androidx/webkit/WebViewFeature#PREFETCH_CACHE_V1()` feature is not supported. This should be checked before use with `https://developer.android.com/reference/androidx/webkit/WebViewFeature#isFeatureSupported(java.lang.String)`. |

### equals

```
public boolean equals(@Nullable Object obj)
```

Compares this PrefetchCache with the specified object for equality.

Two `https://developer.android.com/reference/androidx/webkit/PrefetchCache` instances are considered equal if they are associated with the same `https://developer.android.com/reference/androidx/webkit/Profile` (i.e., they have the same profile name).

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/Object.html obj` | the object to compare with. |

| Returns |
|---|---|
| `boolean` | `true` if the objects are equal; `false` otherwise. |

### getMaxPrefetches

Added in [1.17.0](https://developer.android.com/jetpack/androidx/releases/webkit#1.17.0)

```
@RequiresFeature(name = WebViewFeature.PREFETCH_CACHE_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
public int getMaxPrefetches()
```

Returns maximum prefetches set for this Profile.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException.html` | if the `https://developer.android.com/reference/androidx/webkit/WebViewFeature#PREFETCH_CACHE_V1()` feature is not supported. This should be checked before use with `https://developer.android.com/reference/androidx/webkit/WebViewFeature#isFeatureSupported(java.lang.String)`. |

### getPrefetchTtlSeconds

Added in [1.17.0](https://developer.android.com/jetpack/androidx/releases/webkit#1.17.0)

```
@RequiresFeature(name = WebViewFeature.PREFETCH_CACHE_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
public int getPrefetchTtlSeconds()
```

Returns Prefetch TTL in Seconds set for this Profile.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException.html` | if the `https://developer.android.com/reference/androidx/webkit/WebViewFeature#PREFETCH_CACHE_V1()` feature is not supported. This should be checked before use with `https://developer.android.com/reference/androidx/webkit/WebViewFeature#isFeatureSupported(java.lang.String)`. |

### hashCode

```
public int hashCode()
```

Returns a hash code value for the object, consistent with `https://developer.android.com/reference/androidx/webkit/PrefetchCache#equals(java.lang.Object)`.

### prefetchUrlAsync

Added in [1.17.0](https://developer.android.com/jetpack/androidx/releases/webkit#1.17.0)

```
@RequiresFeature(name = WebViewFeature.PROFILE_URL_PREFETCH, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@AnyThread
@Profile.ExperimentalUrlPrefetch
public void prefetchUrlAsync(
    @NonNull String url,
    @Nullable CancellationSignal cancellationSignal,
    @Nullable Executor callbackExecutor,
    @NonNull WebViewOutcomeReceiver<@NonNull PrefetchResult, PrefetchException> outcomeReceiver
)
```

Starts a URL prefetch request.

All WebViews associated with this Profile will use a URL request matching algorithm during execution of all variants of `https://developer.android.com/reference/android/webkit/WebView.html#loadUrl(java.lang.String)` for determining if there was already a prefetch request executed for the provided URL. This includes prefetches that are "in progress". If a prefetch is matched, WebView will leverage that for handling the URL, otherwise the URL will be handled normally (i.e. through a network request).

Applications will still be responsible for calling `https://developer.android.com/reference/android/webkit/WebView.html#loadUrl(java.lang.String)` to display web contents in a WebView.

NOTE: Additional headers passed to `https://developer.android.com/reference/android/webkit/WebView.html#loadUrl(java.lang.String, java.util.Map&lt;java.lang.String,java.lang.String&gt;)` are not considered in the matching algorithm for determining whether or not to serve a prefetched response to a navigation.

For max latency saving benefits, it is recommended to call this method as early as possible (i.e. before any WebView associated with this profile is created).

Only supports HTTPS scheme.

On success, the `outcomeReceiver` will receive a `https://developer.android.com/reference/androidx/webkit/PrefetchResult` which can be used to check if the prefetch was a duplicate of an existing request via `https://developer.android.com/reference/androidx/webkit/PrefetchResult#wasDuplicate()`.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html url` | the url associated with the prefetch request. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/os/CancellationSignal.html cancellationSignal` | will make the best effort to cancel an in-flight prefetch request, However cancellation is not guaranteed. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/util/concurrent/Executor.html callbackExecutor` | the executor to resolve the callback with. If `null`, the callback will be executed on the main thread. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/webkit/WebViewOutcomeReceiver<@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/webkit/PrefetchResult, https://developer.android.com/reference/androidx/webkit/PrefetchException> outcomeReceiver` | callbacks for reporting result back to application. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | if the url or callback is null. |
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException.html` | if the `https://developer.android.com/reference/androidx/webkit/WebViewFeature#PROFILE_URL_PREFETCH()` feature is not supported. This should be checked before use with `https://developer.android.com/reference/androidx/webkit/WebViewFeature#isFeatureSupported(java.lang.String)`. |

### prefetchUrlAsync

Added in [1.17.0](https://developer.android.com/jetpack/androidx/releases/webkit#1.17.0)

```
@RequiresFeature(name = WebViewFeature.PROFILE_URL_PREFETCH, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@AnyThread
@Profile.ExperimentalUrlPrefetch
public void prefetchUrlAsync(
    @NonNull String url,
    @Nullable CancellationSignal cancellationSignal,
    @Nullable Executor callbackExecutor,
    @NonNull PrefetchParameters prefetchParameters,
    @NonNull WebViewOutcomeReceiver<@NonNull PrefetchResult, PrefetchException> outcomeReceiver
)
```

Starts a URL prefetch request.

All WebViews associated with this Profile will use a URL request matching algorithm during execution of all variants of `https://developer.android.com/reference/android/webkit/WebView.html#loadUrl(java.lang.String)` for determining if there was already a prefetch request executed for the provided URL. This includes prefetches that are "in progress". If a prefetch is matched, WebView will leverage that for handling the URL, otherwise the URL will be handled normally (i.e. through a network request).

Applications will still be responsible for calling `https://developer.android.com/reference/android/webkit/WebView.html#loadUrl(java.lang.String)` to display web contents in a WebView.

NOTE: Additional headers passed to `https://developer.android.com/reference/android/webkit/WebView.html#loadUrl(java.lang.String, java.util.Map&lt;java.lang.String,java.lang.String&gt;)` are not considered in the matching algorithm for determining whether or not to serve a prefetched response to a navigation.

For max latency saving benefits, it is recommended to call this method as early as possible (i.e. before any WebView associated with this profile is created).

Only supports HTTPS scheme.

On success, the `outcomeReceiver` will receive a `https://developer.android.com/reference/androidx/webkit/PrefetchResult` which can be used to check if the prefetch was a duplicate of an existing request via `https://developer.android.com/reference/androidx/webkit/PrefetchResult#wasDuplicate()`.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html url` | the url associated with the prefetch request. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/os/CancellationSignal.html cancellationSignal` | will make the best effort to cancel an in-flight prefetch request, However cancellation is not guaranteed. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/util/concurrent/Executor.html callbackExecutor` | the executor to resolve the callback with. If `null`, the callback will be executed on the main thread. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/webkit/PrefetchParameters prefetchParameters` | parameters to customize the prefetch request. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/webkit/WebViewOutcomeReceiver<@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/webkit/PrefetchResult, https://developer.android.com/reference/androidx/webkit/PrefetchException> outcomeReceiver` | callbacks for reporting result back to application. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | if the url or callback is null. |
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException.html` | if the `https://developer.android.com/reference/androidx/webkit/WebViewFeature#PROFILE_URL_PREFETCH()` feature is not supported. This should be checked before use with `https://developer.android.com/reference/androidx/webkit/WebViewFeature#isFeatureSupported(java.lang.String)`. |

### setMaxPrefetches

Added in [1.17.0](https://developer.android.com/jetpack/androidx/releases/webkit#1.17.0)

```
@RequiresFeature(name = WebViewFeature.PREFETCH_CACHE_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
public void setMaxPrefetches(@IntRange(from = 1) int maxPrefetches)
```

Sets the maximum number of prefetches for the current browsing session.

This configuration will be applied to any prefetch requests made after they are set; they will not be applied to in-flight requests.

This configuration will be applied to WebViews that are associated with the `https://developer.android.com/reference/androidx/webkit/Profile` that owns this `https://developer.android.com/reference/androidx/webkit/PrefetchCache`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/IntRange(from = 1) int maxPrefetches` | the maximum number of prefetches to allow. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException.html` | if the `https://developer.android.com/reference/androidx/webkit/WebViewFeature#PREFETCH_CACHE_V1()` feature is not supported. This should be checked before use with `https://developer.android.com/reference/androidx/webkit/WebViewFeature#isFeatureSupported(java.lang.String)`. |

### setPrefetchTtlSeconds

Added in [1.17.0](https://developer.android.com/jetpack/androidx/releases/webkit#1.17.0)

```
@RequiresFeature(name = WebViewFeature.PREFETCH_CACHE_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
public void setPrefetchTtlSeconds(@IntRange(from = 1) int prefetchTtlSeconds)
```

Sets the maximum prefetch Time-to-Live (TTL) in seconds for the current browsing session.

These configurations will be applied to any prefetch requests made after they are set; they will not be applied to in-flight requests.

These configurations will be applied to WebViews that are associated with the `https://developer.android.com/reference/androidx/webkit/Profile` that owns this `https://developer.android.com/reference/androidx/webkit/PrefetchCache`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/IntRange(from = 1) int prefetchTtlSeconds` | the TTL in seconds. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException.html` | if the `https://developer.android.com/reference/androidx/webkit/WebViewFeature#PREFETCH_CACHE_V1()` feature is not supported. This should be checked before use with `https://developer.android.com/reference/androidx/webkit/WebViewFeature#isFeatureSupported(java.lang.String)`. |