# Caches

# Caches


```
public interface Caches
```

<br />

*** ** * ** ***

Provides an interface to explicitly manage caches for `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerativeModel`.

## Summary

| ### Public methods |
|---|---|
| `abstract @https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/CachedContext` | `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/Caches#create(com.google.mlkit.genai.prompt.CreateCachedContextRequest)(@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/CreateCachedContextRequest request)` Creates a cached context that can be used in future `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest`s. |
| `abstract boolean` | `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/Caches#delete(kotlin.String)(@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developer.android.com/reference/java/lang/String.html name)` Deletes the cache with the given name. |
| `abstract https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/CachedContext` | `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/Caches#get(kotlin.String)(@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developer.android.com/reference/java/lang/String.html name)` Returns the `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/CachedContext` with the given name. |
| `abstract @https://developer.android.com/reference/androidx/annotation/NonNull.html https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/CachedContext>` | `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/Caches#list()()` Queries the `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/CachedContext`s created by `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/Caches#create(com.google.mlkit.genai.prompt.CreateCachedContextRequest)`. |

## Public methods

### create

```
abstract @NonNull CachedContext create(@NonNull CreateCachedContextRequest request)
```

Creates a cached context that can be used in future `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest`s.

This experimental method can be used to create a cached context that stores the pre-processed state of a reusable part of a prompt to improve inference performance. This is useful for scenarios where a common prefix is used across multiple generation requests, such as with few-shot examples.

The created `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/CachedContext` can be used in a `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerateContentRequest`. Each cache is identified by a unique name across all `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/GenerativeModel` instances within the same app. If `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/Caches#create(com.google.mlkit.genai.prompt.CreateCachedContextRequest)` is called with a `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/CreateCachedContextRequest` for an existing cache name, the old cache will be overwritten.

Caches may become invalid over time (e.g., due to model update). If an invalid cache is used, a GenAiException with ErrorCode.CACHE_PROCESSING_ERROR will be thrown, indicating that the cache should be deleted or recreated.

This is a non-blocking, cancellable coroutine. If the operation is no longer needed, cancelling the coroutine will stop the execution.

Note that cache creation requests may fail under certain conditions such as:

- The prefix prompt length exceeds model limitations.

- The `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/CreateCachedContextRequest` contains invalid or malformed input.

- Usage quotas are exceeded or safety checks fail.

| Throws |
|---|---|
| `GenAiException` | if the cache creation fails. |

### delete

```
abstract boolean delete(@NonNull String name)
```

Deletes the cache with the given name.

This experimental method removes the cached context created by `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/Caches#create(com.google.mlkit.genai.prompt.CreateCachedContextRequest)` from storage, freeing up space.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developer.android.com/reference/java/lang/String.html name` | the name of the cache to delete. |

### get

```
abstract CachedContext get(@NonNull String name)
```

Returns the `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/CachedContext` with the given name.

This experimental method retrieves a `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/CachedContext` created by `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/Caches#create(com.google.mlkit.genai.prompt.CreateCachedContextRequest)`, returning `null` if no cache with that name exists.

### list

```
abstract @NonNull List<@NonNull CachedContext> list()
```

Queries the `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/CachedContext`s created by `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/Caches#create(com.google.mlkit.genai.prompt.CreateCachedContextRequest)`.

This experimental method returns a list of `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/CachedContext` objects. This method can be used to inspect and manage existing caches.

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/CachedContext>` | a `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html` of `https://developers.google.com/android/reference/com/google/mlkit/genai/prompt/CachedContext`s. |