Skip to content

Commit

Permalink
server: Remove hardcoded 1000 on the BlockHeaderCache
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexITC committed Mar 24, 2019
1 parent b9ee34d commit d781a0f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
11 changes: 6 additions & 5 deletions server/app/com/xsn/explorer/cache/BlockHeaderCache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ class BlockHeaderCache(cache: Cache[BlockHeaderCache.Key, BlockHeaderCache.Encod
import BlockHeaderCache._

def getOrSet(
key: Key)(
key: Key,
entrySize: Int)(
f: => FutureApplicationResult[Value])(
implicit ec: ExecutionContext,
writes: Writes[Value]): FutureApplicationResult[EncodedValue] = {

if (isCacheable(key)) {
if (isCacheable(key, entrySize)) {
get(key)
.map(v => Future.successful(Good(v)))
.getOrElse {
Expand Down Expand Up @@ -65,12 +66,12 @@ class BlockHeaderCache(cache: Cache[BlockHeaderCache.Key, BlockHeaderCache.Encod
/**
* A block header list is cacheable if all the following meet:
* - The ordering is from oldest to newest
* - The limit is 1000
* - The limit matches the expected entry size
* - The entry is not the last one (TODO)
*/
def isCacheable(key: Key): Boolean = {
def isCacheable(key: Key, entrySize: Int): Boolean = {
key.orderingCondition == OrderingCondition.AscendingOrder &&
key.limit.int == 1000
key.limit.int == entrySize
}

def get(key: Key): Option[EncodedValue] = {
Expand Down
2 changes: 1 addition & 1 deletion server/app/com/xsn/explorer/services/BlockService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class BlockService @Inject() (
implicit writes: Writes[BlockHeader]) = {

val cacheKey = BlockHeaderCache.Key(limit, orderingCondition, lastSeenHash)
blockHeaderCache.getOrSet(cacheKey) {
blockHeaderCache.getOrSet(cacheKey, maxHeadersPerQuery) {
val result = for {
headers <-blockDataHandler.getHeaders(limit, orderingCondition, lastSeenHash).toFutureOr
} yield WrappedResult(headers)
Expand Down

0 comments on commit d781a0f

Please sign in to comment.