ckb_store/
cache.rs

1use ckb_app_config::StoreConfig;
2use ckb_types::{
3    bytes::Bytes,
4    core::{HeaderView, UncleBlockVecView},
5    packed::{self, Byte32, ProposalShortIdVec},
6};
7use ckb_util::Mutex;
8use lru::LruCache;
9
10/// The cache of chain store.
11pub struct StoreCache {
12    /// The cache of block headers
13    pub headers: Mutex<LruCache<Byte32, HeaderView>>,
14    /// The cache of cell data.
15    pub cell_data: Mutex<LruCache<Vec<u8>, (Bytes, Byte32)>>,
16    /// The cache of cell data hash.
17    pub cell_data_hash: Mutex<LruCache<Vec<u8>, Byte32>>,
18    /// The cache of block proposals.
19    pub block_proposals: Mutex<LruCache<Byte32, ProposalShortIdVec>>,
20    /// The cache of block transaction hashes.
21    pub block_tx_hashes: Mutex<LruCache<Byte32, Vec<Byte32>>>,
22    /// The cache of block uncles.
23    pub block_uncles: Mutex<LruCache<Byte32, UncleBlockVecView>>,
24    /// The cache of block extension sections.
25    pub block_extensions: Mutex<LruCache<Byte32, Option<packed::Bytes>>>,
26}
27
28impl Default for StoreCache {
29    fn default() -> Self {
30        StoreCache::from_config(StoreConfig::default())
31    }
32}
33
34impl StoreCache {
35    /// Allocate a new StoreCache with the given config
36    pub fn from_config(config: StoreConfig) -> Self {
37        StoreCache {
38            headers: Mutex::new(LruCache::new(config.header_cache_size)),
39            cell_data: Mutex::new(LruCache::new(config.cell_data_cache_size)),
40            cell_data_hash: Mutex::new(LruCache::new(config.cell_data_cache_size)),
41            block_proposals: Mutex::new(LruCache::new(config.block_proposals_cache_size)),
42            block_tx_hashes: Mutex::new(LruCache::new(config.block_tx_hashes_cache_size)),
43            block_uncles: Mutex::new(LruCache::new(config.block_uncles_cache_size)),
44            block_extensions: Mutex::new(LruCache::new(config.block_extensions_cache_size)),
45        }
46    }
47}