# CacheEvictor

# CacheEvictor

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

*** ** * ** ***

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

<br />

```
@UnstableApi
interface CacheEvictor : Cache.Listener
```

<br />

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

|------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------|
| [LeastRecentlyUsedCacheEvictor](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/LeastRecentlyUsedCacheEvictor) | Evicts least recently used cache files first. |
| [NoOpCacheEvictor](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/NoOpCacheEvictor)                           | Evictor that doesn't ever evict cache files.  |

*** ** * ** ***

Evicts data from a[Cache](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache). Implementations should call[removeSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache#removeSpan(androidx.media3.datasource.cache.CacheSpan))to evict cache entries based on their eviction policies.

## Summary

|                                ### Public functions                                 |
|-------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [onCacheInitialized](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheEvictor#onCacheInitialized())`()` Called when cache has been initialized.                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [onStartFile](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheEvictor#onStartFile(androidx.media3.datasource.cache.Cache,java.lang.String,long,long))`(cache: `[Cache](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache)`!, 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)`)` Called when a writer starts writing to the cache. |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | [requiresCacheSpanTouches](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheEvictor#requiresCacheSpanTouches())`()` Returns whether the evictor requires the[Cache](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache)to touch[CacheSpans](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)when it accesses them.                                                                                                                                                                                                     |

|                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ### Inherited functions                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| From[androidx.media3.datasource.cache.Cache.Listener](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache.Listener) |-------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [onSpanAdded](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache.Listener#onSpanAdded(androidx.media3.datasource.cache.Cache,androidx.media3.datasource.cache.CacheSpan))`(cache: `[Cache](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache)`!, span: `[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)`!)` Called when a[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)is added to the cache.                                                                                                                                                                                                 | | [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [onSpanRemoved](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache.Listener#onSpanRemoved(androidx.media3.datasource.cache.Cache,androidx.media3.datasource.cache.CacheSpan))`(cache: `[Cache](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache)`!, span: `[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)`!)` Called when a[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)is removed from the cache.                                                                                                                                                                                         | | [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [onSpanTouched](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache.Listener#onSpanTouched(androidx.media3.datasource.cache.Cache,androidx.media3.datasource.cache.CacheSpan,androidx.media3.datasource.cache.CacheSpan))`(cache: `[Cache](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache)`!, oldSpan: `[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)`!, newSpan: `[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)`!)` Called when an existing[CacheSpan](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)is touched, causing it to be replaced. | |

## Public functions

### onCacheInitialized

```
fun onCacheInitialized(): Unit
```

Called when cache has been initialized.  

### onStartFile

```
fun onStartFile(cache: Cache!, key: String!, position: Long, length: Long): Unit
```

Called when a writer starts writing to the cache.  

|                                                 Parameters                                                 |
|------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------|
| `cache: `[Cache](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache)`!` | The source of the event.                                                                                                                                  |
| `key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!`                | The key being written.                                                                                                                                    |
| `position: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)                  | The starting position of the data being 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. |

### requiresCacheSpanTouches

```
fun requiresCacheSpanTouches(): Boolean
```

Returns whether the evictor requires the[Cache](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache)to touch[CacheSpans](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan)when it accesses them. Implementations that do not use[lastTouchTimestamp](https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheSpan#lastTouchTimestamp())should return`false`.