-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
使用ConcurrentLinkedHashMap和AtomicLong优化缓存性能
- Loading branch information
1 parent
7564c05
commit f589cd4
Showing
2 changed files
with
12 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,9 @@ | |
|
||
package com.lt.load_the_image.cache | ||
|
||
import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap | ||
import java.util.concurrent.atomic.AtomicLong | ||
|
||
/** | ||
* creator: lt 2022/4/8 [email protected] | ||
* effect : Memory cache configuration of network image, cache use LRU | ||
|
@@ -25,24 +28,25 @@ open class ImageLruMemoryCache( | |
private val maxMemorySize: Long = getMemoryWithOnePercent() | ||
) : ImageCache { | ||
//image lru cache | ||
private val cacheMap = LinkedHashMap<String, ByteArray>(35, 1f, true) | ||
private val cacheMap = ConcurrentLinkedHashMap.Builder<String, ByteArray>() | ||
.initialCapacity(35) | ||
.maximumWeightedCapacity(9999) | ||
.build() | ||
|
||
//image cache byte size sum | ||
private var cacheSize: Long = 0 | ||
private var cacheSize = AtomicLong(0) | ||
|
||
@Synchronized | ||
override fun saveCache(url: String, t: ByteArray) { | ||
if (t.size > maxMemorySize) | ||
return | ||
cacheMap[url] = t | ||
cacheSize += t.size | ||
while (cacheSize > maxMemorySize && cacheMap.isNotEmpty()) { | ||
cacheSize.getAndAdd(t.size.toLong()) | ||
while (cacheSize.get() > maxMemorySize && cacheMap.isNotEmpty()) { | ||
val byteArray = cacheMap.remove(cacheMap.keys.first()) | ||
cacheSize -= byteArray?.size ?: 0 | ||
cacheSize.getAndAdd(-(byteArray?.size ?: 0).toLong()) | ||
} | ||
} | ||
|
||
@Synchronized | ||
override fun getCache(url: String): ByteArray? { | ||
return cacheMap[url] | ||
} | ||
|