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,11 +365,20 @@ impl HeaderChain {
/// will be returned.
pub fn block_header(&self, id: BlockId) -> Option<encoded::Header> {
let load_from_db = |hash: H256| {
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
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) {
Ok(val) => val.map(|x| x.to_vec()).map(encoded::Header::new),
Err(e) => {
warn!(target: "chain", "Failed to read from database: {}", e);
None
}
}
}
};