Adds zap amount cache for the memory space calculations

This commit is contained in:
Vitor Pamplona 2024-08-08 18:19:48 -04:00
parent 8ed78ea38f
commit 282d4614c0

View File

@ -39,9 +39,20 @@ class LnZapEvent(
// This event is also kept in LocalCache (same object)
@Transient val zapRequest: LnZapRequestEvent?
// Keeps this as a field because it's a heavier function used everywhere.
val amount by lazy {
try {
lnInvoice()?.let { LnInvoiceUtil.getAmountInSats(it) }
} catch (e: Exception) {
Log.e("LnZapEvent", "Failed to Parse LnInvoice ${lnInvoice()}", e)
null
}
}
override fun countMemory(): Long =
super.countMemory() +
pointerSizeInBytes + (zapRequest?.countMemory() ?: 0) // rough calculation
pointerSizeInBytes + (zapRequest?.countMemory() ?: 0) + // rough calculation
pointerSizeInBytes + 36 // bigdecimal size
override fun containedPost(): LnZapRequestEvent? =
try {
@ -75,16 +86,6 @@ class LnZapEvent(
override fun amount() = amount
// Keeps this as a field because it's a heavier function used everywhere.
val amount by lazy {
try {
lnInvoice()?.let { LnInvoiceUtil.getAmountInSats(it) }
} catch (e: Exception) {
Log.e("LnZapEvent", "Failed to Parse LnInvoice ${lnInvoice()}", e)
null
}
}
override fun content(): String = content
fun lnInvoice() = tags.firstOrNull { it.size > 1 && it[0] == "bolt11" }?.get(1)