Skip to content

Commit

Permalink
Use ConcurrentReferenceHashMap with SOFT references for caches
Browse files Browse the repository at this point in the history
  • Loading branch information
jvmusin committed Mar 18, 2024
1 parent 00cc829 commit d46335b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.withContext
import org.springframework.stereotype.Component
import java.util.concurrent.ConcurrentHashMap
import org.springframework.util.ConcurrentReferenceHashMap

/**
* Polygon problem downloader
Expand Down Expand Up @@ -55,7 +55,7 @@ class PolygonProblemDownloader(
/**
* Problems cache.
*/
private val cache = ConcurrentHashMap<FullPackageId, IRProblem>()
private val cache = ConcurrentReferenceHashMap<FullPackageId, IRProblem>()

/**
* Returns problem using Polygon API.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import io.github.jvmusin.polybacs.polygon.exception.response.NoSuchProblemExcept
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import org.apache.commons.compress.archivers.zip.ZipFile
import org.springframework.util.ConcurrentReferenceHashMap
import java.util.concurrent.ConcurrentHashMap

private val packagesCache = ConcurrentHashMap<Int, ByteArray>()
private val packagesCacheLocks = ConcurrentHashMap<Int, Mutex>()
private val packagesCache = ConcurrentReferenceHashMap<Int, ByteArray>()

private suspend fun PolygonApi.downloadPackageZip(problemId: Int, packageId: Int): ByteArray {
return packagesCacheLocks.computeIfAbsent(packageId) { Mutex() }.withLock {
Expand Down

0 comments on commit d46335b

Please sign in to comment.