# Cache

# Cache

Artifact:[androidx.media3:media3-datasource](https://developer.android.com/jetpack/androidx/releases/media3)  
[View Source](https://cs.android.com/search?q=file:androidx/media3/datasource/cache/Cache.java+class:androidx.media3.datasource.cache.Cache)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/media3/datasource/cache/Cache "View this page in Java")

<br />

```
@UnstableApi
interface Cache
```

<br />

Known direct subclasses  
[SimpleCache](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache)  

|------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------|
| [SimpleCache](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache) | A[Cache](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache)implementation that maintains an in-memory representation. |

*** ** * ** ***

A cache that supports partial caching of resources.

## Terminology

- A*resource*is a complete piece of logical data, for example a complete media file.
- A*cache key*uniquely identifies a resource. URIs are often suitable for use as cache keys, however this is not always the case. URIs are not suitable when caching resources obtained from a service that generates multiple URIs for the same underlying resource, for example because the service uses expiring URIs as a form of access control.
- A*cache span* is a byte range within a resource, which may or may not be cached. A cache span that's not cached is called a*hole span*. A cache span that is cached corresponds to a single underlying file in the cache.

## Summary

|                                                                                                                                ### Nested types                                                                                                                                 |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `class `[Cache.CacheException](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache.CacheException)` : `[IOException](https://developer.android.com/reference/java/io/IOException.html) Thrown when an error is encountered when writing data. |
| `interface `[Cache.Listener](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache.Listener) Listener of[Cache](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache)events.                                   |

|                                     ### Constants                                     |
|---------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `const `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | [UID_UNSET](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#UID_UNSET())` = -1` Returned by[getUid](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#getUid())if initialization failed before the unique identifier was read or generated. |

|                                                                                                                                   ### Public functions                                                                                                                                    |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [NavigableSet](https://developer.android.com/reference/java/util/NavigableSet.html)`<`[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)`!>!`                                                                                         | [addListener](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#addListener(java.lang.String,androidx.media3.datasource.cache.Cache.Listener))`(key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!, listener: `[Cache.Listener](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache.Listener)`!)` Registers a listener to listen for changes to a given resource.                                                                                                                                                                                                                                                                                   |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                                                                                                                                             | `@`[WorkerThread](https://developer.android.com/reference/kotlin/androidx/annotation/WorkerThread) [applyContentMetadataMutations](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#applyContentMetadataMutations(java.lang.String,androidx.media3.datasource.cache.ContentMetadataMutations))`(` ` key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!,` ` mutations: `[ContentMetadataMutations](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/ContentMetadataMutations)`!` `)` Applies`mutations`to the[ContentMetadata](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/ContentMetadata)for the given resource. |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                                                                                                                                             | `@`[WorkerThread](https://developer.android.com/reference/kotlin/androidx/annotation/WorkerThread) [commitFile](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#commitFile(java.io.File,long))`(file: `[File](https://developer.android.com/reference/java/io/File.html)`!, length: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` Commits a file into the cache.                                                                                                                                                                                                                                                                                                                       |
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)                                                                                                                                                                                                             | [getCacheSpace](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#getCacheSpace())`()` Returns the total disk space in bytes used by the cache.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)                                                                                                                                                                                                             | [getCachedBytes](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#getCachedBytes(java.lang.String,long,long))`(key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!, position: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`, length: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` Returns the total number of cached bytes between`position`(inclusive) and` (position + length)`(exclusive) of a resource.                                                                                                                                                                                                    |
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)                                                                                                                                                                                                             | [getCachedLength](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#getCachedLength(java.lang.String,long,long))`(key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!, position: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`, length: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` Returns the length of continuously cached data starting from`position`, up to a maximum of`maxLength`, of a resource.                                                                                                                                                                                                      |
| [NavigableSet](https://developer.android.com/reference/java/util/NavigableSet.html)`<`[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)`!>!`                                                                                         | [getCachedSpans](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#getCachedSpans(java.lang.String))`(key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!)` Returns the cached spans for a given resource.                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| [ContentMetadata](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/ContentMetadata)`!`                                                                                                                                                                     | [getContentMetadata](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#getContentMetadata(java.lang.String))`(key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!)` Returns a[ContentMetadata](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/ContentMetadata)for the given resource.                                                                                                                                                                                                                                                                                                                                                                 |
| `(`[Mutable](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-set/index.html)`)`[Set](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-set/index.html)`<`[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!>!` | [getKeys](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#getKeys())`()` Returns the cache keys of all of the resources that are at least partially cached.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)                                                                                                                                                                                                             | [getUid](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#getUid())`()` Returns a non-negative unique identifier for the cache, or[UID_UNSET](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#UID_UNSET())if initialization failed before the unique identifier was determined.                                                                                                                                                                                                                                                                                                                                                                                                       |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)                                                                                                                                                                                                       | [isCached](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#isCached(java.lang.String,long,long))`(key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!, position: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`, length: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` Returns whether the specified range of data in a resource is fully cached.                                                                                                                                                                                                                                                               |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                                                                                                                                             | `@`[WorkerThread](https://developer.android.com/reference/kotlin/androidx/annotation/WorkerThread) [release](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#release())`()` Releases the cache.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                                                                                                                                             | [releaseHoleSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#releaseHoleSpan(androidx.media3.datasource.cache.CacheSpan))`(holeSpan: `[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)`!)` Releases a[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)obtained from[startReadWrite](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#startReadWrite(java.lang.String,long,long))which corresponded to a hole in the cache.                                                                                                                                            |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                                                                                                                                             | [removeListener](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#removeListener(java.lang.String,androidx.media3.datasource.cache.Cache.Listener))`(key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!, listener: `[Cache.Listener](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache.Listener)`!)` Unregisters a listener.                                                                                                                                                                                                                                                                                                                     |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                                                                                                                                             | `@`[WorkerThread](https://developer.android.com/reference/kotlin/androidx/annotation/WorkerThread) [removeResource](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#removeResource(java.lang.String))`(key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!)` Removes all[CacheSpans](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)for a resource, deleting the underlying files.                                                                                                                                                                                                                                                        |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                                                                                                                                             | `@`[WorkerThread](https://developer.android.com/reference/kotlin/androidx/annotation/WorkerThread) [removeSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#removeSpan(androidx.media3.datasource.cache.CacheSpan))`(span: `[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)`!)` Removes a cached[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)from the cache, deleting the underlying file.                                                                                                                                                                                                             |
| [File](https://developer.android.com/reference/java/io/File.html)`!`                                                                                                                                                                                                                      | `@`[WorkerThread](https://developer.android.com/reference/kotlin/androidx/annotation/WorkerThread) [startFile](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#startFile(java.lang.String,long,long))`(key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!, position: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`, length: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` Obtains a cache file into which data can be written.                                                                                                                                                                                |
| [CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)`!`                                                                                                                                                                                 | `@`[WorkerThread](https://developer.android.com/reference/kotlin/androidx/annotation/WorkerThread) [startReadWrite](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#startReadWrite(java.lang.String,long,long))`(key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!, position: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`, length: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` A caller should invoke this method when they require data starting from a given position in a given resource.                                                                                                             |
| [CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)`?`                                                                                                                                                                                 | `@`[WorkerThread](https://developer.android.com/reference/kotlin/androidx/annotation/WorkerThread) [startReadWriteNonBlocking](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#startReadWriteNonBlocking(java.lang.String,long,long))`(key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!, position: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`, length: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` Same as[startReadWrite](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#startReadWrite(java.lang.String,long,long)).                                          |

## Constants

### UID_UNSET

```
const val UID_UNSET = -1: Long
```

Returned by[getUid](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#getUid())if initialization failed before the unique identifier was read or generated.  

## Public functions

### addListener

```
fun addListener(key: String!, listener: Cache.Listener!): NavigableSet<CacheSpan!>!
```

Registers a listener to listen for changes to a given resource.

No guarantees are made about the thread or threads on which the listener is called, but it is guaranteed that listener methods will be called in a serial fashion (i.e. one at a time) and in the same order as events occurred.  

|                                                           Parameters                                                            |
|---------------------------------------------------------------------------------------------------------------------------------|--------------------------------|
| `key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!`                                     | The cache key of the resource. |
| `listener: `[Cache.Listener](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache.Listener)`!` | The listener to add.           |

|                                                                                              Returns                                                                                              |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------|
| [NavigableSet](https://developer.android.com/reference/java/util/NavigableSet.html)`<`[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)`!>!` | The current spans for the resource. |

### applyContentMetadataMutations

```
@WorkerThread
fun applyContentMetadataMutations(
    key: String!,
    mutations: ContentMetadataMutations!
): Unit
```

Applies`mutations`to the[ContentMetadata](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/ContentMetadata)for the given resource. A new[CachedContent](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CachedContent)is added if there isn't one already for the resource.

This method may be slow and shouldn't normally be called on the main thread.  

|                                                                      Parameters                                                                      |
|------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------|
| `key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!`                                                          | The cache key of the resource.                    |
| `mutations: `[ContentMetadataMutations](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/ContentMetadataMutations)`!` | Contains mutations to be applied to the metadata. |

|                                                                            Throws                                                                             |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------|
| [androidx.media3.datasource.cache.Cache.CacheException](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache.CacheException) | If an error is encountered. |

### commitFile

```
@WorkerThread
fun commitFile(file: File!, length: Long): Unit
```

Commits a file into the cache. Must only be called when holding a corresponding hole[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)obtained from[startReadWrite](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#startReadWrite(java.lang.String,long,long)).

This method may be slow and shouldn't normally be called on the main thread.  

|                                       Parameters                                        |
|-----------------------------------------------------------------------------------------|------------------------------------------------------|
| `file: `[File](https://developer.android.com/reference/java/io/File.html)`!`            | A newly written cache file.                          |
| `length: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | The length of the newly written cache file in bytes. |

|                                                                            Throws                                                                             |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------|
| [androidx.media3.datasource.cache.Cache.CacheException](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache.CacheException) | If an error is encountered. |

### getCacheSpace

```
fun getCacheSpace(): Long
```

Returns the total disk space in bytes used by the cache.  

### getCachedBytes

```
fun getCachedBytes(key: String!, position: Long, length: Long): Long
```

Returns the total number of cached bytes between`position`(inclusive) and`
(position + length)`(exclusive) of a resource.  

|                                         Parameters                                          |
|---------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!` | The cache key of the resource.                                                                                                                                                                                                                                        |
| `position: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)   | The starting position of the data in the resource.                                                                                                                                                                                                                    |
| `length: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)     | The length of the data to check.[LENGTH_UNSET](https://developer.android.com/reference/kotlin/androidx/media3/common/C#LENGTH_UNSET())is permitted, and is equivalent to passing[MAX_VALUE](https://developer.android.com/reference/java/lang/Long.html#MAX_VALUE()). |

|                                    Returns                                    |
|-------------------------------------------------------------------------------|-----------------------------------|
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | The total number of cached bytes. |

### getCachedLength

```
fun getCachedLength(key: String!, position: Long, length: Long): Long
```

Returns the length of continuously cached data starting from`position`, up to a maximum of`maxLength`, of a resource. If`position`isn't cached then`-holeLength`is returned, where`holeLength`is the length of continuously uncached data starting from`position`, up to a maximum of`maxLength`.  

|                                         Parameters                                          |
|---------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!` | The cache key of the resource.                                                                                                                                                                                                                                                              |
| `position: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)   | The starting position of the data in the resource.                                                                                                                                                                                                                                          |
| `length: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)     | The maximum length of the data or hole to be returned.[LENGTH_UNSET](https://developer.android.com/reference/kotlin/androidx/media3/common/C#LENGTH_UNSET())is permitted, and is equivalent to passing[MAX_VALUE](https://developer.android.com/reference/java/lang/Long.html#MAX_VALUE()). |

|                                    Returns                                    |
|-------------------------------------------------------------------------------|--------------------------------------------------------------------------------------|
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | The length of the continuously cached data, or`-holeLength`if`position`isn't cached. |

### getCachedSpans

```
fun getCachedSpans(key: String!): NavigableSet<CacheSpan!>!
```

Returns the cached spans for a given resource.  

|                                         Parameters                                          |
|---------------------------------------------------------------------------------------------|--------------------------------|
| `key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!` | The cache key of the resource. |

|                                                                                              Returns                                                                                              |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------|
| [NavigableSet](https://developer.android.com/reference/java/util/NavigableSet.html)`<`[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)`!>!` | The spans for the key. |

### getContentMetadata

```
fun getContentMetadata(key: String!): ContentMetadata!
```

Returns a[ContentMetadata](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/ContentMetadata)for the given resource.  

|                                         Parameters                                          |
|---------------------------------------------------------------------------------------------|--------------------------------|
| `key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!` | The cache key of the resource. |

|                                                        Returns                                                        |
|-----------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------|
| [ContentMetadata](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/ContentMetadata)`!` | The[ContentMetadata](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/ContentMetadata)for the resource. |

### getKeys

```
fun getKeys(): (Mutable)Set<String!>!
```

Returns the cache keys of all of the resources that are at least partially cached.  

### getUid

```
fun getUid(): Long
```

Returns a non-negative unique identifier for the cache, or[UID_UNSET](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#UID_UNSET())if initialization failed before the unique identifier was determined.

Implementations are expected to generate and store the unique identifier alongside the cached content. If the location of the cache is deleted or swapped, it is expected that a new unique identifier will be generated when the cache is recreated.  

### isCached

```
fun isCached(key: String!, position: Long, length: Long): Boolean
```

Returns whether the specified range of data in a resource is fully cached.  

|                                         Parameters                                          |
|---------------------------------------------------------------------------------------------|----------------------------------------------------|
| `key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!` | The cache key of the resource.                     |
| `position: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)   | The starting position of the data in the resource. |
| `length: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)     | The length of the data.                            |

|                                       Returns                                       |
|-------------------------------------------------------------------------------------|-------------------------------------------------------------|
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | true if the data is available in the Cache otherwise false; |

### release

```
@WorkerThread
fun release(): Unit
```

Releases the cache. This method must be called when the cache is no longer required. The cache must not be used after calling this method.

This method may be slow and shouldn't normally be called on the main thread.  

### releaseHoleSpan

```
fun releaseHoleSpan(holeSpan: CacheSpan!): Unit
```

Releases a[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)obtained from[startReadWrite](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#startReadWrite(java.lang.String,long,long))which corresponded to a hole in the cache.  

|                                                      Parameters                                                       |
|-----------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------|
| `holeSpan: `[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)`!` | The[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)being released. |

### removeListener

```
fun removeListener(key: String!, listener: Cache.Listener!): Unit
```

Unregisters a listener.  

|                                                           Parameters                                                            |
|---------------------------------------------------------------------------------------------------------------------------------|--------------------------------|
| `key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!`                                     | The cache key of the resource. |
| `listener: `[Cache.Listener](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache.Listener)`!` | The listener to remove.        |

### removeResource

```
@WorkerThread
fun removeResource(key: String!): Unit
```

Removes all[CacheSpans](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)for a resource, deleting the underlying files.  

|                                         Parameters                                          |
|---------------------------------------------------------------------------------------------|----------------------------------------------|
| `key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!` | The cache key of the resource being removed. |

### removeSpan

```
@WorkerThread
fun removeSpan(span: CacheSpan!): Unit
```

Removes a cached[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)from the cache, deleting the underlying file.

This method may be slow and shouldn't normally be called on the main thread.  

|                                                    Parameters                                                     |
|-------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------|
| `span: `[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)`!` | The[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)to remove. |

### startFile

```
@WorkerThread
fun startFile(key: String!, position: Long, length: Long): File!
```

Obtains a cache file into which data can be written. Must only be called when holding a corresponding hole[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)obtained from[startReadWrite](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#startReadWrite(java.lang.String,long,long)).

This method may be slow and shouldn't normally be called on the main thread.  

|                                         Parameters                                          |
|---------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!` | The cache key of the resource being written.                                                                                                                                                                           |
| `position: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)   | The starting position in the resource from which data will be written.                                                                                                                                                 |
| `length: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)     | The length of the data being written, or[LENGTH_UNSET](https://developer.android.com/reference/kotlin/androidx/media3/common/C#LENGTH_UNSET())if unknown. Used only to ensure that there is enough space in the cache. |

|                               Returns                                |
|----------------------------------------------------------------------|---------------------------------------------|
| [File](https://developer.android.com/reference/java/io/File.html)`!` | The file into which data should be written. |

|                                                                            Throws                                                                             |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------|
| [androidx.media3.datasource.cache.Cache.CacheException](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache.CacheException) | If an error is encountered. |

### startReadWrite

```
@WorkerThread
fun startReadWrite(key: String!, position: Long, length: Long): CacheSpan!
```

A caller should invoke this method when they require data starting from a given position in a given resource.

If there is a cache entry that overlaps the position, then the returned[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)defines the file in which the data is stored.[isCached](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan#isCached())is true. The caller may read from the cache file, but does not acquire any locks.

If there is no cache entry overlapping`position`, then the returned[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)defines a hole in the cache starting at`position`into which the caller may write as it obtains the data from some other source. The returned[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)serves as a lock. Whilst the caller holds the lock it may write data into the hole. It may split data into multiple files. When the caller has finished writing a file it should commit it to the cache by calling[commitFile](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#commitFile(java.io.File,long)). When the caller has finished writing, it must release the lock by calling[releaseHoleSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#releaseHoleSpan(androidx.media3.datasource.cache.CacheSpan)).

This method may be slow and shouldn't normally be called on the main thread.  

|                                         Parameters                                          |
|---------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!` | The cache key of the resource.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| `position: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)   | The starting position in the resource from which data is required.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| `length: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)     | The length of the data being requested, or[LENGTH_UNSET](https://developer.android.com/reference/kotlin/androidx/media3/common/C#LENGTH_UNSET())if unbounded. The length is ignored if there is a cache entry that overlaps the position. Else, it defines the maximum length of the hole[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)that's returned. Cache implementations may support parallel writes into non-overlapping holes, and so passing the actual required length should be preferred to passing[LENGTH_UNSET](https://developer.android.com/reference/kotlin/androidx/media3/common/C#LENGTH_UNSET())when possible. |

|                                                  Returns                                                  |
|-----------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------|
| [CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)`!` | The[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan). |

|                                                                            Throws                                                                             |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------|
| [java.lang.InterruptedException](https://developer.android.com/reference/java/lang/InterruptedException.html)                                                 | If the thread was interrupted. |
| [androidx.media3.datasource.cache.Cache.CacheException](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache.CacheException) | If an error is encountered.    |

### startReadWriteNonBlocking

```
@WorkerThread
fun startReadWriteNonBlocking(key: String!, position: Long, length: Long): CacheSpan?
```

Same as[startReadWrite](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#startReadWrite(java.lang.String,long,long)). However, if the cache entry is locked, then instead of blocking, this method will return null as the[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan).

This method may be slow and shouldn't normally be called on the main thread.  

|                                         Parameters                                          |
|---------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!` | The cache key of the resource.                                                                                                                                                                                                                                                                                                                                                                             |
| `position: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)   | The starting position in the resource from which data is required.                                                                                                                                                                                                                                                                                                                                         |
| `length: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)     | The length of the data being requested, or[LENGTH_UNSET](https://developer.android.com/reference/kotlin/androidx/media3/common/C#LENGTH_UNSET())if unbounded. The length is ignored if there is a cache entry that overlaps the position. Else, it defines the range of data locked by the returned[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan). |

|                                                  Returns                                                  |
|-----------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------|
| [CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)`?` | The[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan). Or null if the cache entry is locked. |

|                                                                            Throws                                                                             |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------|
| [androidx.media3.datasource.cache.Cache.CacheException](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache.CacheException) | If an error is encountered. |