Google berkomitmen untuk mendorong terwujudnya keadilan ras bagi komunitas Kulit Hitam. Lihat caranya.

LruCache

open class LruCache<K : Any!, V : Any!>
kotlin.Any
   ↳ androidx.collection.LruCache

Static library version of android.util.LruCache. Used to write apps that run on API levels prior to 12. When running on API level 12 or above, this implementation is still used; it does not try to switch to the framework's implementation. See the framework SDK documentation for a class overview.

Summary

Public constructors
<init>(maxSize: Int)

Public methods
Int

Returns the number of times create(Object) returned a value.

Unit

Clear the cache, calling entryRemoved on each removed entry.

Int

Returns the number of values that have been evicted.

V?
get(@NonNull key: K)

Returns the value for key if it exists in the cache or can be created by create.

Int

Returns the number of times get returned a value that was already present in the cache.

Int

For caches that do not override sizeOf, this returns the maximum number of entries in the cache.

Int

Returns the number of times get returned null or required a new value to be created.

V?
put(@NonNull key: K, @NonNull value: V)

Caches value for key.

Int

Returns the number of times put was called.

V?
remove(@NonNull key: K)

Removes the entry for key if it exists.

open Unit
resize(maxSize: Int)

Sets the size of the cache.

Int

For caches that do not override sizeOf, this returns the number of entries in the cache.

MutableMap<K, V>!

Returns a copy of the current contents of the cache, ordered from least recently accessed to most recently accessed.

String

open Unit
trimToSize(maxSize: Int)

Remove the eldest entries until the total of remaining entries is at or below the requested size.

Protected methods
open V?
create(@NonNull key: K)

Called after a cache miss to compute a value for the corresponding key.

open Unit
entryRemoved(evicted: Boolean, @NonNull key: K, @NonNull oldValue: V, @Nullable newValue: V?)

Called for entries that have been evicted or removed.

open Int
sizeOf(@NonNull key: K, @NonNull value: V)

Returns the size of the entry for key and value in user-defined units.

Public constructors

<init>

LruCache(maxSize: Int)
Parameters
maxSize Int: for caches that do not override sizeOf, this is the maximum number of entries in the cache. For all other caches, this is the maximum sum of the sizes of the entries in this cache.

Public methods

createCount

fun createCount(): Int

Returns the number of times create(Object) returned a value.

evictAll

fun evictAll(): Unit

Clear the cache, calling entryRemoved on each removed entry.

evictionCount

fun evictionCount(): Int

Returns the number of values that have been evicted.

get

@Nullable fun get(@NonNull key: K): V?

Returns the value for key if it exists in the cache or can be created by create. If a value was returned, it is moved to the head of the queue. This returns null if a value is not cached and cannot be created.

hitCount

fun hitCount(): Int

Returns the number of times get returned a value that was already present in the cache.

maxSize

fun maxSize(): Int

For caches that do not override sizeOf, this returns the maximum number of entries in the cache. For all other caches, this returns the maximum sum of the sizes of the entries in this cache.

missCount

fun missCount(): Int

Returns the number of times get returned null or required a new value to be created.

put

@Nullable fun put(
    @NonNull key: K,
    @NonNull