problem: doesn't cache transactions requested for a block

This commit is contained in:
Igor Artamonov
2020-07-20 20:34:37 -04:00
parent 9d752fc8f1
commit d3a3b6606a
4 changed files with 71 additions and 25 deletions

View File

@@ -97,9 +97,10 @@ open class Caches(
return
}
memTxsByHash.add(tx)
memBlocksByHash.get(tx.blockId)?.let { block ->
redisTxsByHash?.add(tx, block)
}
//TODO move subscription to the caller
getBlocksByHash().read(tx.blockId).flatMap { block ->
redisTxsByHash?.add(tx, block) ?: Mono.empty()
}.subscribe()
}
fun cache(tag: Tag, block: BlockContainer) {
@@ -142,11 +143,11 @@ open class Caches(
val transactions = plainTransactions.map { tx ->
TxContainer.from(tx)
}
transactions.forEach {
cache(Tag.REQUESTED, it)
}
if (redisTxsByHash != null) {
job.add(Flux.fromIterable(transactions).flatMap { redisTxsByHash.add(it, block) }.then())
job.add(Flux.fromIterable(transactions)
.doOnNext { memTxsByHash.add(it) }
.flatMap { redisTxsByHash.add(it, block) }
.then())
}
}
}

View File

@@ -117,7 +117,7 @@ abstract class OnTxRedisCache<T>(
}
}
fun add(id: TxId, value: T, block: BlockContainer?, blockHeight: Long?): Mono<Void> {
open fun add(id: TxId, value: T, block: BlockContainer?, blockHeight: Long?): Mono<Void> {
return Mono.just(id)
.flatMap {
val key = key(it)

View File

@@ -35,7 +35,7 @@ import kotlin.math.min
/**
* Cache transactions in Redis, up to 24 hours.
*/
class TxRedisCache(
open class TxRedisCache(
private val redis: RedisReactiveCommands<String, ByteArray>,
private val chain: Chain
) : Reader<TxId, TxContainer>,
@@ -74,7 +74,7 @@ class TxRedisCache(
)
}
fun add(tx: TxContainer, block: BlockContainer): Mono<Void> {
open fun add(tx: TxContainer, block: BlockContainer): Mono<Void> {
return super.add(tx.hash, tx, block, tx.height)
}