mirror of
https://github.com/corda/corda.git
synced 2025-06-19 07:38:22 +00:00
Add system property to disable public key caching (#7438)
This commit is contained in:
@ -7,6 +7,8 @@ import java.security.PublicKey
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
object PublicKeyCache {
|
||||
private val DISABLE = java.lang.Boolean.getBoolean("net.corda.core.pubkeycache.disable")
|
||||
|
||||
private val collectedWeakPubKeys = ReferenceQueue<PublicKey>()
|
||||
|
||||
private class WeakPubKey(key: PublicKey, val bytes: ByteSequence? = null) : WeakReference<PublicKey>(key, collectedWeakPubKeys) {
|
||||
@ -36,15 +38,18 @@ object PublicKeyCache {
|
||||
}
|
||||
|
||||
fun bytesForCachedPublicKey(key: PublicKey): ByteSequence? {
|
||||
if (DISABLE) return null
|
||||
val weakPubKey = WeakPubKey(key)
|
||||
return pubKeyToBytes[weakPubKey]
|
||||
}
|
||||
|
||||
fun publicKeyForCachedBytes(bytes: ByteSequence): PublicKey? {
|
||||
if (DISABLE) return null
|
||||
return bytesToPubKey[bytes]?.get()
|
||||
}
|
||||
|
||||
fun cachePublicKey(key: PublicKey): PublicKey {
|
||||
if (DISABLE) return key
|
||||
reapCollectedWeakPubKeys()
|
||||
val weakPubKey = WeakPubKey(key, ByteSequence.of(key.encoded))
|
||||
pubKeyToBytes.putIfAbsent(weakPubKey, weakPubKey.bytes!!)
|
||||
|
Reference in New Issue
Block a user