Add caching to block_header()

This commit is contained in:
Adrian Brink 2017-04-06 15:46:59 +02:00
parent 8ea25eeb3c
commit 8a7ca6f0ba
1 changed files with 14 additions and 5 deletions

View File

@ -365,11 +365,20 @@ impl HeaderChain {
/// will be returned. /// will be returned.
pub fn block_header(&self, id: BlockId) -> Option<encoded::Header> { pub fn block_header(&self, id: BlockId) -> Option<encoded::Header> {
let load_from_db = |hash: H256| { let load_from_db = |hash: H256| {
match self.db.get(self.col, &hash) { let cached = {
Ok(val) => val.map(|x| x.to_vec()).map(encoded::Header::new), let mut cache = self.cache.lock();
Err(e) => { cache.block_header(&hash)
warn!(target: "chain", "Failed to read from database: {}", e); };
None
if cached.is_some() {
return cached
} else {
match self.db.get(self.col, &hash) {
Ok(val) => val.map(|x| x.to_vec()).map(encoded::Header::new),
Err(e) => {
warn!(target: "chain", "Failed to read from database: {}", e);
None
}
} }
} }
}; };