# LeastRecentlyUsedCacheEvictor

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

*** ** * ** ***


```
@UnstableApi
public final class LeastRecentlyUsedCacheEvictor implements CacheEvictor
```

<br />

*** ** * ** ***

Evicts least recently used cache files first.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/media3/datasource/cache/LeastRecentlyUsedCacheEvictor#LeastRecentlyUsedCacheEvictor(long)(long maxBytes)` |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/media3/datasource/cache/LeastRecentlyUsedCacheEvictor#onCacheInitialized()()` Called when cache has been initialized. |
| `void` | `https://developer.android.com/reference/androidx/media3/datasource/cache/LeastRecentlyUsedCacheEvictor#onSpanAdded(androidx.media3.datasource.cache.Cache,androidx.media3.datasource.cache.CacheSpan)(https://developer.android.com/reference/androidx/media3/datasource/cache/Cache cache, https://developer.android.com/reference/androidx/media3/datasource/cache/CacheSpan span)` Called when a `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheSpan` is added to the cache. |
| `void` | `https://developer.android.com/reference/androidx/media3/datasource/cache/LeastRecentlyUsedCacheEvictor#onSpanRemoved(androidx.media3.datasource.cache.Cache,androidx.media3.datasource.cache.CacheSpan)(https://developer.android.com/reference/androidx/media3/datasource/cache/Cache cache, https://developer.android.com/reference/androidx/media3/datasource/cache/CacheSpan span)` Called when a `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheSpan` is removed from the cache. |
| `void` | `https://developer.android.com/reference/androidx/media3/datasource/cache/LeastRecentlyUsedCacheEvictor#onSpanTouched(androidx.media3.datasource.cache.Cache,androidx.media3.datasource.cache.CacheSpan,androidx.media3.datasource.cache.CacheSpan)(https://developer.android.com/reference/androidx/media3/datasource/cache/Cache cache, https://developer.android.com/reference/androidx/media3/datasource/cache/CacheSpan oldSpan, https://developer.android.com/reference/androidx/media3/datasource/cache/CacheSpan newSpan)` Called when an existing `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheSpan` is touched, causing it to be replaced. |
| `void` | `https://developer.android.com/reference/androidx/media3/datasource/cache/LeastRecentlyUsedCacheEvictor#onStartFile(androidx.media3.datasource.cache.Cache,java.lang.String,long,long)(https://developer.android.com/reference/androidx/media3/datasource/cache/Cache cache, https://developer.android.com/reference/java/lang/String.html key, long position, long length)` Called when a writer starts writing to the cache. |
| `boolean` | `https://developer.android.com/reference/androidx/media3/datasource/cache/LeastRecentlyUsedCacheEvictor#requiresCacheSpanTouches()()` Returns whether the evictor requires the `https://developer.android.com/reference/androidx/media3/datasource/cache/Cache` to touch `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheSpan` when it accesses them. |

## Public constructors

### LeastRecentlyUsedCacheEvictor

```
public LeastRecentlyUsedCacheEvictor(long maxBytes)
```

## Public methods

### onCacheInitialized

```
public void onCacheInitialized()
```

Called when cache has been initialized.

### onSpanAdded

```
public void onSpanAdded(Cache cache, CacheSpan span)
```

Called when a `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheSpan` is added to the cache.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/cache/Cache cache` | The source of the event. |
| `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheSpan span` | The added `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheSpan`. |

### onSpanRemoved

```
public void onSpanRemoved(Cache cache, CacheSpan span)
```

Called when a `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheSpan` is removed from the cache.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/cache/Cache cache` | The source of the event. |
| `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheSpan span` | The removed `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheSpan`. |

### onSpanTouched

```
public void onSpanTouched(Cache cache, CacheSpan oldSpan, CacheSpan newSpan)
```

Called when an existing `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheSpan` is touched, causing it to be replaced. The new `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheSpan` is guaranteed to represent the same data as the one it replaces, however `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheSpan#file()` and `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheSpan#lastTouchTimestamp()` may have changed.

Note that for span replacement, `https://developer.android.com/reference/androidx/media3/datasource/cache/Cache.Listener#onSpanAdded(androidx.media3.datasource.cache.Cache,androidx.media3.datasource.cache.CacheSpan)` and `https://developer.android.com/reference/androidx/media3/datasource/cache/Cache.Listener#onSpanRemoved(androidx.media3.datasource.cache.Cache,androidx.media3.datasource.cache.CacheSpan)` are not called in addition to this method.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/cache/Cache cache` | The source of the event. |
| `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheSpan oldSpan` | The old `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheSpan`, which has been removed from the cache. |
| `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheSpan newSpan` | The new `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheSpan`, which has been added to the cache. |

### onStartFile

```
public void onStartFile(Cache cache, String key, long position, long length)
```

Called when a writer starts writing to the cache.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/cache/Cache cache` | The source of the event. |
| `https://developer.android.com/reference/java/lang/String.html key` | The key being written. |
| `long position` | The starting position of the data being written. |
| `long length` | The length of the data being written, or `https://developer.android.com/reference/androidx/media3/common/C#LENGTH_UNSET()` if unknown. |

### requiresCacheSpanTouches

```
public boolean requiresCacheSpanTouches()
```

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