Skip to content

Commit

Permalink
Use AndroidX's LruCache.
Browse files Browse the repository at this point in the history
  • Loading branch information
Isira-Seneviratne committed Sep 28, 2022
1 parent 6298eed commit 3c5dbfe
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ buildscript {
mockWebServer: "com.squareup.okhttp3:mockwebserver:${versions.okhttp}",
pollexor: 'com.squareup:pollexor:3.0.0',
androidxAnnotations: 'androidx.annotation:annotation:1.3.0',
androidxCore: 'androidx.core:core:1.6.0',
androidxCore: 'androidx.core:core-ktx:1.6.0',
androidxCursorAdapter: 'androidx.cursoradapter:cursoradapter:1.0.0',
androidxExifInterface: 'androidx.exifinterface:exifinterface:1.3.3',
androidxFragment: 'androidx.fragment:fragment:1.4.0',
Expand Down
13 changes: 4 additions & 9 deletions picasso/src/main/java/com/squareup/picasso3/PlatformLruCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,15 @@
package com.squareup.picasso3

import android.graphics.Bitmap
import android.util.LruCache
import androidx.core.graphics.BitmapCompat
import androidx.core.util.lruCache

/** A memory cache which uses a least-recently used eviction policy. */
internal class PlatformLruCache(maxByteCount: Int) {

/** Create a cache with a given maximum size in bytes. */
val cache =
object : LruCache<String, BitmapAndSize>(if (maxByteCount != 0) maxByteCount else 1) {
override fun sizeOf(
key: String,
value: BitmapAndSize
): Int = value.byteCount
}
val cache = lruCache<String, BitmapAndSize>(
maxByteCount.coerceAtLeast(1), { _, value -> value.byteCount }
)

operator fun get(key: String): Bitmap? = cache[key]?.bitmap

Expand Down

0 comments on commit 3c5dbfe

Please sign in to comment.