secretblob.SecretBlobCacheDecorator.kt Maven / Gradle / Ivy
package se.wollan.crypto.secretblob
import java.util.concurrent.ConcurrentHashMap
internal class SecretBlobCacheDecorator(private val inner: SecretBlob, keys: List) : SecretBlob {
private val cache = ConcurrentHashMap(keys.size)
override fun getSecretDataForKey(key: SecretBlobKey): ByteArray {
cache[key]?.let { return it.copyOf() }
val data = inner.getSecretDataForKey(key)
// no need to copy here as no one is holding a reference to data according to the contract of getSecretDataForKey
cache[key] = data
return data.copyOf()
}
}