Add caching to block_header()
This commit is contained in:
parent
8ea25eeb3c
commit
8a7ca6f0ba
@ -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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user