Add caching to block_header()

This commit is contained in:
Adrian Brink 2017-04-06 15:46:59 +02:00
parent 8ea25eeb3c
commit 8a7ca6f0ba

View File

@ -365,6 +365,14 @@ 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| {
let cached = {
let mut cache = self.cache.lock();
cache.block_header(&hash)
};
if cached.is_some() {
return cached
} else {
match self.db.get(self.col, &hash) { match self.db.get(self.col, &hash) {
Ok(val) => val.map(|x| x.to_vec()).map(encoded::Header::new), Ok(val) => val.map(|x| x.to_vec()).map(encoded::Header::new),
Err(e) => { Err(e) => {
@ -372,6 +380,7 @@ impl HeaderChain {
None None
} }
} }
}
}; };
match id { match id {