|
@ -1,16 +1,27 @@ |
|
|
use crate::errors::*; |
|
|
use crate::errors::*; |
|
|
|
|
|
use crate::metrics::{Counter, MetricOpts, Metrics}; |
|
|
use bitcoin_hashes::sha256d::Hash as Sha256dHash; |
|
|
use bitcoin_hashes::sha256d::Hash as Sha256dHash; |
|
|
use lru::LruCache; |
|
|
use lru::LruCache; |
|
|
use std::sync::Mutex; |
|
|
use std::sync::Mutex; |
|
|
|
|
|
|
|
|
pub struct BlockTxIDsCache { |
|
|
pub struct BlockTxIDsCache { |
|
|
map: Mutex<LruCache<Sha256dHash /* blockhash */, Vec<Sha256dHash /* txid */>>>, |
|
|
map: Mutex<LruCache<Sha256dHash /* blockhash */, Vec<Sha256dHash /* txid */>>>, |
|
|
|
|
|
hits: Counter, |
|
|
|
|
|
misses: Counter, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
impl BlockTxIDsCache { |
|
|
impl BlockTxIDsCache { |
|
|
pub fn new(capacity: usize) -> BlockTxIDsCache { |
|
|
pub fn new(capacity: usize, metrics: &Metrics) -> BlockTxIDsCache { |
|
|
BlockTxIDsCache { |
|
|
BlockTxIDsCache { |
|
|
map: Mutex::new(LruCache::new(capacity)), |
|
|
map: Mutex::new(LruCache::new(capacity)), |
|
|
|
|
|
hits: metrics.counter(MetricOpts::new( |
|
|
|
|
|
"electrs_blocktxids_cache_hits", |
|
|
|
|
|
"# of cache hits for list of transactions in a block", |
|
|
|
|
|
)), |
|
|
|
|
|
misses: metrics.counter(MetricOpts::new( |
|
|
|
|
|
"electrs_blocktxids_cache_misses", |
|
|
|
|
|
"# of cache misses for list of transactions in a block", |
|
|
|
|
|
)), |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -23,9 +34,11 @@ impl BlockTxIDsCache { |
|
|
F: FnOnce() -> Result<Vec<Sha256dHash>>, |
|
|
F: FnOnce() -> Result<Vec<Sha256dHash>>, |
|
|
{ |
|
|
{ |
|
|
if let Some(txids) = self.map.lock().unwrap().get(blockhash) { |
|
|
if let Some(txids) = self.map.lock().unwrap().get(blockhash) { |
|
|
|
|
|
self.hits.inc(); |
|
|
return Ok(txids.clone()); |
|
|
return Ok(txids.clone()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
self.misses.inc(); |
|
|
let txids = load_txids_func()?; |
|
|
let txids = load_txids_func()?; |
|
|
self.map.lock().unwrap().put(*blockhash, txids.clone()); |
|
|
self.map.lock().unwrap().put(*blockhash, txids.clone()); |
|
|
Ok(txids) |
|
|
Ok(txids) |
|
|