problem: doesn't cache block by height for old blocks
This commit is contained in:
@@ -106,22 +106,8 @@ open class Caches(
|
||||
fun cache(tag: Tag, block: BlockContainer) {
|
||||
val job = ArrayList<Mono<Void>>()
|
||||
if (tag == Tag.LATEST) {
|
||||
//for LATEST data cache in memory, it will be short living so better to avoid Redis
|
||||
memBlocksByHash.add(block)
|
||||
val replaced = blocksByHeight.add(block)
|
||||
//evict cached transactions if an existing block was updated
|
||||
replaced?.let { replacedBlockHash ->
|
||||
var evicted = false
|
||||
redisBlocksByHash?.evict(replacedBlockHash)
|
||||
memBlocksByHash.get(replacedBlockHash)?.let { block ->
|
||||
memTxsByHash.evict(block)
|
||||
redisTxsByHash?.evict(block)
|
||||
evicted = true
|
||||
}
|
||||
if (!evicted) {
|
||||
memTxsByHash.evict(replacedBlockHash)
|
||||
}
|
||||
}
|
||||
//for LATEST data cache it in memory, it may be short living so better to avoid Redis
|
||||
memoizeBlock(block)
|
||||
} else if (tag == Tag.REQUESTED) {
|
||||
var blockOnlyContainer: BlockContainer? = null
|
||||
var jsonValue: BlockJson<*>? = null
|
||||
@@ -133,6 +119,7 @@ open class Caches(
|
||||
} else {
|
||||
blockOnlyContainer = block
|
||||
}
|
||||
memoizeBlock(blockOnlyContainer)
|
||||
memBlocksByHash.add(blockOnlyContainer)
|
||||
redisBlocksByHash?.add(blockOnlyContainer)?.let(job::add)
|
||||
|
||||
@@ -155,6 +142,29 @@ open class Caches(
|
||||
Flux.fromIterable(job).flatMap { it }.subscribe() //TODO move out to a caller
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache the block only in memory
|
||||
*/
|
||||
fun memoizeBlock(block: BlockContainer) {
|
||||
memBlocksByHash.add(block)
|
||||
val replaced = blocksByHeight.add(block)
|
||||
//evict cached transactions if an existing block was updated
|
||||
replaced?.let { evict(it) }
|
||||
}
|
||||
|
||||
fun evict(blockId: BlockId) {
|
||||
var evicted = false
|
||||
redisBlocksByHash?.evict(blockId)
|
||||
memBlocksByHash.get(blockId)?.let { block ->
|
||||
memTxsByHash.evict(block)
|
||||
redisTxsByHash?.evict(block)
|
||||
evicted = true
|
||||
}
|
||||
if (!evicted) {
|
||||
memTxsByHash.evict(blockId)
|
||||
}
|
||||
}
|
||||
|
||||
fun getBlocksByHash(): Reader<BlockId, BlockContainer> {
|
||||
return blocksByHash
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user