Add LruCache and thread-safety

This commit is contained in:
bemusementpark 2024-08-05 10:26:52 +09:30
parent fa0abef243
commit f9ace6a9b9

View File

@ -1,6 +1,7 @@
package org.session.libsignal.utilities
import android.annotation.SuppressLint
import android.util.LruCache
/**
* Create a Snode from a "-" delimited String if valid, null otherwise.
@ -39,10 +40,11 @@ class Snode(val address: String, val port: Int, val publicKeySet: KeySet?, val v
override fun toString(): String = "$address:$port"
companion object {
private val CACHE = mutableMapOf<String, Version>()
private val CACHE = LruCache<String, Version>(100)
@SuppressLint("NotConstructor")
fun Version(value: String) = CACHE[value] ?: Snode.Version(value).also { CACHE[value] = it }
@Synchronized
fun Version(value: String) = CACHE[value] ?: Snode.Version(value).also { CACHE.put(value, it) }
fun Version(parts: List<Int>) = Version(parts.joinToString("."))
}