# 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
class PrefetchCache
```

<br />

*** ** * ** ***

PrefetchCache manages the configuration of the prefetch cache for a `https://developer.android.com/reference/kotlin/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/kotlin/androidx/webkit/Profile#getPrefetchCache()` to obtain the PrefetchCache instance for a specific profile.

## Summary

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

## Public functions

### 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
fun clearMaxPrefetches(): Unit
```

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/kotlin/androidx/webkit/Profile` that owns this `https://developer.android.com/reference/kotlin/androidx/webkit/PrefetchCache`.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException.html` | if the `https://developer.android.com/reference/kotlin/androidx/webkit/WebViewFeature#PREFETCH_CACHE_V1()` feature is not supported. This should be checked before use with `https://developer.android.com/reference/kotlin/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
fun clearPrefetchTtl(): Unit
```

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/kotlin/androidx/webkit/Profile` that owns this `https://developer.android.com/reference/kotlin/androidx/webkit/PrefetchCache`.

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

### equals

```
fun equals(obj: Any?): Boolean
```

Compares this PrefetchCache with the specified object for equality.

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

| Parameters |
|---|---|
| `obj: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?` | the object to compare with. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `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
fun getMaxPrefetches(): Int
```

Returns maximum prefetches set for this Profile.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException.html` | if the `https://developer.android.com/reference/kotlin/androidx/webkit/WebViewFeature#PREFETCH_CACHE_V1()` feature is not supported. This should be checked before use with `https://developer.android.com/reference/kotlin/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
fun getPrefetchTtlSeconds(): Int
```

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/kotlin/androidx/webkit/WebViewFeature#PREFETCH_CACHE_V1()` feature is not supported. This should be checked before use with `https://developer.android.com/reference/kotlin/androidx/webkit/WebViewFeature#isFeatureSupported(java.lang.String)`. |

### hashCode

```
fun hashCode(): Int
```

Returns a hash code value for the object, consistent with `https://developer.android.com/reference/kotlin/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
fun prefetchUrlAsync(
    url: String,
    cancellationSignal: CancellationSignal?,
    callbackExecutor: Executor?,
    outcomeReceiver: WebViewOutcomeReceiver<PrefetchResult, PrefetchException!>
): Unit
```

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/kotlin/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/kotlin/androidx/webkit/PrefetchResult#wasDuplicate()`.

| Parameters |
|---|---|
| `url: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | the url associated with the prefetch request. |
| `cancellationSignal: https://developer.android.com/reference/android/os/CancellationSignal.html?` | will make the best effort to cancel an in-flight prefetch request, However cancellation is not guaranteed. |
| `callbackExecutor: https://developer.android.com/reference/java/util/concurrent/Executor.html?` | the executor to resolve the callback with. If `null`, the callback will be executed on the main thread. |
| `outcomeReceiver: https://developer.android.com/reference/kotlin/androidx/webkit/WebViewOutcomeReceiver<https://developer.android.com/reference/kotlin/androidx/webkit/PrefetchResult, https://developer.android.com/reference/kotlin/androidx/webkit/PrefetchException!>` | 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/kotlin/androidx/webkit/WebViewFeature#PROFILE_URL_PREFETCH()` feature is not supported. This should be checked before use with `https://developer.android.com/reference/kotlin/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
fun prefetchUrlAsync(
    url: String,
    cancellationSignal: CancellationSignal?,
    callbackExecutor: Executor?,
    prefetchParameters: PrefetchParameters,
    outcomeReceiver: WebViewOutcomeReceiver<PrefetchResult, PrefetchException!>
): Unit
```

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/kotlin/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/kotlin/androidx/webkit/PrefetchResult#wasDuplicate()`.

| Parameters |
|---|---|
| `url: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | the url associated with the prefetch request. |
| `cancellationSignal: https://developer.android.com/reference/android/os/CancellationSignal.html?` | will make the best effort to cancel an in-flight prefetch request, However cancellation is not guaranteed. |
| `callbackExecutor: https://developer.android.com/reference/java/util/concurrent/Executor.html?` | the executor to resolve the callback with. If `null`, the callback will be executed on the main thread. |
| `prefetchParameters: https://developer.android.com/reference/kotlin/androidx/webkit/PrefetchParameters` | parameters to customize the prefetch request. |
| `outcomeReceiver: https://developer.android.com/reference/kotlin/androidx/webkit/WebViewOutcomeReceiver<https://developer.android.com/reference/kotlin/androidx/webkit/PrefetchResult, https://developer.android.com/reference/kotlin/androidx/webkit/PrefetchException!>` | 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/kotlin/androidx/webkit/WebViewFeature#PROFILE_URL_PREFETCH()` feature is not supported. This should be checked before use with `https://developer.android.com/reference/kotlin/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
fun setMaxPrefetches(maxPrefetches: @IntRange(from = 1) Int): Unit
```

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/kotlin/androidx/webkit/Profile` that owns this `https://developer.android.com/reference/kotlin/androidx/webkit/PrefetchCache`.

| Parameters |
|---|---|
| `maxPrefetches: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 1) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | the maximum number of prefetches to allow. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException.html` | if the `https://developer.android.com/reference/kotlin/androidx/webkit/WebViewFeature#PREFETCH_CACHE_V1()` feature is not supported. This should be checked before use with `https://developer.android.com/reference/kotlin/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
fun setPrefetchTtlSeconds(prefetchTtlSeconds: @IntRange(from = 1) Int): Unit
```

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/kotlin/androidx/webkit/Profile` that owns this `https://developer.android.com/reference/kotlin/androidx/webkit/PrefetchCache`.

| Parameters |
|---|---|
| `prefetchTtlSeconds: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 1) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | the TTL in seconds. |

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