-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update: use ThreadLocal replace for InheritableThreadLocal
- Loading branch information
Showing
2 changed files
with
18 additions
and
61 deletions.
There are no files selected for viewing
27 changes: 9 additions & 18 deletions
27
src/main/kotlin/com/muedsa/snapshot/tools/SimpleNoLimitedNetworkImageCache.kt
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 |
---|---|---|
@@ -1,54 +1,45 @@ | ||
package com.muedsa.snapshot.tools | ||
|
||
import java.net.URL | ||
import kotlin.concurrent.getOrSet | ||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
object SimpleNoLimitedNetworkImageCache : NetworkImageCache { | ||
|
||
var debug: Boolean = false | ||
|
||
const val NAME: String = "SimpleNoLimitedNetworkImageCache" | ||
|
||
private val threadLocalCache: InheritableThreadLocal<MutableMap<String, ByteArray>> = InheritableThreadLocal() | ||
|
||
private val cache: MutableMap<String, ByteArray> | ||
@Synchronized | ||
get() = threadLocalCache.getOrSet { mutableMapOf() } | ||
private val threadLocalCache: ThreadLocal<MutableMap<String, ByteArray>> = ThreadLocal.withInitial { | ||
ConcurrentHashMap() | ||
} | ||
|
||
override val name: String | ||
get() = NAME | ||
|
||
@Synchronized | ||
override fun getImage(url: String): ByteArray { | ||
if (debug) println("Thread[${Thread.currentThread().name}] try get image from cache: $url") | ||
return cache.getOrPut(url) { | ||
return threadLocalCache.get().computeIfAbsent(url) { | ||
if (debug) println("Thread[${Thread.currentThread().name}] not found in cache: $url") | ||
getImageOverHttp(url) | ||
} | ||
} | ||
|
||
@Synchronized | ||
override fun clearAll() { | ||
cache.clear() | ||
threadLocalCache.get().clear() | ||
} | ||
|
||
@Synchronized | ||
override fun clearImage(url: String) { | ||
cache.remove(url) | ||
threadLocalCache.get().remove(url) | ||
} | ||
|
||
@Synchronized | ||
override fun count(): Int = cache.size | ||
override fun count(): Int = threadLocalCache.get().size | ||
|
||
@Synchronized | ||
override fun size(): Int = cache.values.fold(0) { acc: Int, bytes: ByteArray -> | ||
override fun size(): Int = threadLocalCache.get().values.fold(0) { acc: Int, bytes: ByteArray -> | ||
acc + bytes.size | ||
} | ||
|
||
private fun getImageOverHttp(url: String): ByteArray { | ||
if (debug) println("Thread[${Thread.currentThread().name}] request http image: $url") | ||
return URL(url).openStream().use { it.readAllBytes() } | ||
} | ||
|
||
|
||
} |
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