# SimpleCache

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/SimpleCache.java+class:androidx.media3.datasource.cache.SimpleCache)

*** ** * ** ***

<br />

```
@UnstableApi
class SimpleCache : Cache
```

<br />

*** ** * ** ***

A `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache` implementation that maintains an in-memory representation.

Only one instance of SimpleCache is allowed for a given directory at a given time.

To delete a SimpleCache, use `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#delete(java.io.File,androidx.media3.database.DatabaseProvider)` rather than deleting the directory and its contents directly. This is necessary to ensure that associated index data is also removed.

## Summary

| ### Public constructors |
|---|
| `[SimpleCache](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#SimpleCache(java.io.File,androidx.media3.datasource.cache.CacheEvictor))(cacheDir: https://developer.android.com/reference/java/io/File.html!, evictor: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheEvictor!)` **This function is deprecated.** Use a constructor that takes a `https://developer.android.com/reference/kotlin/androidx/media3/database/DatabaseProvider` for improved performance. <br /> |
| `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#SimpleCache(java.io.File,androidx.media3.datasource.cache.CacheEvictor,androidx.media3.database.DatabaseProvider)( cacheDir: https://developer.android.com/reference/java/io/File.html!, evictor: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheEvictor!, databaseProvider: https://developer.android.com/reference/kotlin/androidx/media3/database/DatabaseProvider! )` Constructs the cache. |
| `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#SimpleCache(java.io.File,androidx.media3.datasource.cache.CacheEvictor,androidx.media3.database.DatabaseProvider,byte[],boolean,boolean)( cacheDir: https://developer.android.com/reference/java/io/File.html!, evictor: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheEvictor!, databaseProvider: https://developer.android.com/reference/kotlin/androidx/media3/database/DatabaseProvider?, legacyIndexSecretKey: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>?, legacyIndexEncrypt: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html, preferLegacyIndex: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html )` Constructs the cache. |

| ### Public functions |
|---|---|
| `synchronized https://developer.android.com/reference/java/util/NavigableSet.html<https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan!>!` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#addListener(java.lang.String,androidx.media3.datasource.cache.Cache.Listener)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!, listener: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache.Listener!)` Registers a listener to listen for changes to a given resource. |
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#applyContentMetadataMutations(java.lang.String,androidx.media3.datasource.cache.ContentMetadataMutations)( key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!, mutations: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/ContentMetadataMutations! )` Applies `mutations` to the `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/ContentMetadata` for the given resource. |
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#checkInitialization()()` Checks whether the cache was initialized successfully. |
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#commitFile(java.io.File,long)(file: https://developer.android.com/reference/java/io/File.html!, length: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Commits a file into the cache. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/WorkerThread https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#delete(java.io.File,androidx.media3.database.DatabaseProvider)(cacheDir: https://developer.android.com/reference/java/io/File.html!, databaseProvider: https://developer.android.com/reference/kotlin/androidx/media3/database/DatabaseProvider?)` Deletes all content belonging to a cache instance. |
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#getCacheSpace()()` Returns the total disk space in bytes used by the cache. |
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#getCachedBytes(java.lang.String,long,long)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!, position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, length: 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. |
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#getCachedLength(java.lang.String,long,long)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!, position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, length: 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. |
| `synchronized https://developer.android.com/reference/java/util/NavigableSet.html<https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan!>!` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#getCachedSpans(java.lang.String)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!)` Returns the cached spans for a given resource. |
| `synchronized https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/ContentMetadata!` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#getContentMetadata(java.lang.String)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!)` Returns a `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/ContentMetadata` for the given resource. |
| `synchronized (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-set/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-set/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!>!` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#getKeys()()` Returns the cache keys of all of the resources that are at least partially cached. |
| `synchronized java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#isCacheFolderLocked(java.io.File)(cacheFolder: https://developer.android.com/reference/java/io/File.html!)` Returns whether `cacheFolder` is locked by a `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache` instance. |
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#isCached(java.lang.String,long,long)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!, position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, length: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Returns whether the specified range of data in a resource is fully cached. |
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#release()()` Releases the cache. |
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#releaseHoleSpan(androidx.media3.datasource.cache.CacheSpan)(holeSpan: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan!)` Releases a `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan` obtained from `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. |
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#removeListener(java.lang.String,androidx.media3.datasource.cache.Cache.Listener)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!, listener: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache.Listener!)` Unregisters a listener. |
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#removeResource(java.lang.String)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!)` Removes all `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan` for a resource, deleting the underlying files. |
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#removeSpan(androidx.media3.datasource.cache.CacheSpan)(span: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan!)` Removes a cached `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan` from the cache, deleting the underlying file. |
| `synchronized https://developer.android.com/reference/java/io/File.html!` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#startFile(java.lang.String,long,long)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!, position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, length: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Obtains a cache file into which data can be written. |
| `synchronized https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan!` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#startReadWrite(java.lang.String,long,long)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!, position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, length: 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. |
| `synchronized https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan?` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#startReadWriteNonBlocking(java.lang.String,long,long)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!, position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, length: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Same as `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#startReadWrite(java.lang.String,long,long)`. |

| ### Public properties |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#uid()` |

### Inherited Constants

From [androidx.media3.datasource.cache.Cache](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache) `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#UID_UNSET() = -1`

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

## Public constructors

### SimpleCache

```
[SimpleCache](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache#SimpleCache(java.io.File,androidx.media3.datasource.cache.CacheEvictor))(cacheDir: File!, evictor: CacheEvictor!)
```

> [!CAUTION]
> **This function is deprecated.**   
>
> Use a constructor that takes a `https://developer.android.com/reference/kotlin/androidx/media3/database/DatabaseProvider` for improved performance.

Constructs the cache. The cache will delete any unrecognized files from the directory. Hence the directory cannot be used to store other files.

| Parameters |
|---|---|
| `cacheDir: https://developer.android.com/reference/java/io/File.html!` | A dedicated cache directory. |
| `evictor: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheEvictor!` | The evictor to be used. For download use cases where cache eviction should not occur, use `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/NoOpCacheEvictor`. |

### SimpleCache

```
SimpleCache(
    cacheDir: File!,
    evictor: CacheEvictor!,
    databaseProvider: DatabaseProvider!
)
```

Constructs the cache. The cache will delete any unrecognized files from the directory. Hence the directory cannot be used to store other files.

| Parameters |
|---|---|
| `cacheDir: https://developer.android.com/reference/java/io/File.html!` | A dedicated cache directory. |
| `evictor: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheEvictor!` | The evictor to be used. For download use cases where cache eviction should not occur, use `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/NoOpCacheEvictor`. |
| `databaseProvider: https://developer.android.com/reference/kotlin/androidx/media3/database/DatabaseProvider!` | Provides the database in which the cache index is stored. |

### SimpleCache

```
SimpleCache(
    cacheDir: File!,
    evictor: CacheEvictor!,
    databaseProvider: DatabaseProvider?,
    legacyIndexSecretKey: ByteArray<Byte>?,
    legacyIndexEncrypt: Boolean,
    preferLegacyIndex: Boolean
)
```

Constructs the cache. The cache will delete any unrecognized files from the cache directory. Hence the directory cannot be used to store other files.

| Parameters |
|---|---|
| `cacheDir: https://developer.android.com/reference/java/io/File.html!` | A dedicated cache directory. |
| `evictor: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheEvictor!` | The evictor to be used. For download use cases where cache eviction should not occur, use `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/NoOpCacheEvictor`. |
| `databaseProvider: https://developer.android.com/reference/kotlin/androidx/media3/database/DatabaseProvider?` | Provides the database in which the cache index is stored, or ` null` to use a legacy index. Using a database index is highly recommended for performance reasons. |
| `legacyIndexSecretKey: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>?` | A 16 byte AES key for reading, and optionally writing, the legacy index. Not used by the database index, however should still be provided when using the database index in cases where upgrading from the legacy index may be necessary. |
| `legacyIndexEncrypt: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | Whether to encrypt when writing to the legacy index. Must be ` false` if `legacyIndexSecretKey` is `null`. Not used by the database index. |
| `preferLegacyIndex: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | Whether to use the legacy index even if a `databaseProvider` is provided. Should be `false` in nearly all cases. Setting this to `true` is only useful for downgrading from the database index back to the legacy index. |

## Public functions

### addListener

```
synchronized 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: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!` | The cache key of the resource. |
| `listener: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache.Listener!` | The listener to add. |

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

### applyContentMetadataMutations

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

Applies `mutations` to the `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/ContentMetadata` for the given resource. A new `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: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!` | The cache key of the resource. |
| `mutations: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/ContentMetadataMutations!` | Contains mutations to be applied to the metadata. |

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

### checkInitialization

```
synchronized fun checkInitialization(): Unit
```

Checks whether the cache was initialized successfully.

| Throws |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache.CacheException` | If an error occurred during initialization. |

### commitFile

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

Commits a file into the cache. Must only be called when holding a corresponding hole `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan` obtained from `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: https://developer.android.com/reference/java/io/File.html!` | A newly written cache file. |
| `length: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The length of the newly written cache file in bytes. |

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

### delete

```
@WorkerThread
java-static fun delete(cacheDir: File!, databaseProvider: DatabaseProvider?): Unit
```

Deletes all content belonging to a cache instance.

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

| Parameters |
|---|---|
| `cacheDir: https://developer.android.com/reference/java/io/File.html!` | The cache directory. |
| `databaseProvider: https://developer.android.com/reference/kotlin/androidx/media3/database/DatabaseProvider?` | The database in which index data is stored, or `null` if the cache used a legacy index. |

### getCacheSpace

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

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

### getCachedBytes

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

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The total number of cached bytes. |

### getCachedLength

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

| Returns |
|---|---|
| `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

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

Returns the cached spans for a given resource.

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

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

### getContentMetadata

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

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

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

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

### getKeys

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

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

### isCacheFolderLocked

```
synchronized java-static fun isCacheFolderLocked(cacheFolder: File!): Boolean
```

Returns whether `cacheFolder` is locked by a `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache` instance. To unlock the folder the `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/SimpleCache` instance should be released.

### isCached

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

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

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

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

### release

```
synchronized 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

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

Releases a `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan` obtained from `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: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan!` | The `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan` being released. |

### removeListener

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

Unregisters a listener.

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

### removeResource

```
synchronized fun removeResource(key: String!): Unit
```

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

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

### removeSpan

```
synchronized fun removeSpan(span: CacheSpan!): Unit
```

Removes a cached `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: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan!` | The `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan` to remove. |

### startFile

```
synchronized 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 `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan` obtained from `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: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!` | The cache key of the resource being written. |
| `position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The starting position in the resource from which data will be written. |
| `length: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The length of the data being written, or `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 |
|---|---|
| `https://developer.android.com/reference/java/io/File.html!` | The file into which data should be written. |

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

### startReadWrite

```
synchronized 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 `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan` defines the file in which the data is stored. `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 `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 `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 `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 `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: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!` | The cache key of the resource. |
| `position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The starting position in the resource from which data is required. |
| `length: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The length of the data being requested, or `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 `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 `https://developer.android.com/reference/kotlin/androidx/media3/common/C#LENGTH_UNSET()` when possible. |

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

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

### startReadWriteNonBlocking

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

Same as `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 `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: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!` | The cache key of the resource. |
| `position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The starting position in the resource from which data is required. |
| `length: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The length of the data being requested, or `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 `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan`. |

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

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

## Public properties

### uid

```
val uid: Long
```