riftenlabs-indexer/src/cache.rs

25 lines
924 B
Rust
Raw Normal View History

2026-03-19 13:46:16 +01:00
// Copyright (C) 2024-2026 Whiterun LLC
//
// This software is licensed under the GNU Affero General Public License (AGPL), version 3.0 or later.
// A copy of the license can be found in the LICENSE file or at https://www.gnu.org/licenses/agpl-3.0.html
use crate::rpc::candlesticks::CandlestickData;
use moka::future::Cache;
use std::sync::Arc;
/// Cache key: (token_display_hex, start_ts, end_ts, stepsize_secs)
type CandleCacheKey = (String, i64, i64, i64);
pub type CandlestickCache = Cache<CandleCacheKey, Arc<Vec<CandlestickData>>>;
/// Max total candles stored across all entries.
/// Each CandlestickData is ~72 bytes; 500_000 candles ≈ 36 MB upper bound.
const MAX_CANDLE_CAPACITY: u64 = 500_000;
pub fn new_candlestick_cache() -> CandlestickCache {
Cache::builder()
.max_capacity(MAX_CANDLE_CAPACITY)
.weigher(|_k, v: &Arc<Vec<CandlestickData>>| v.len().max(1) as u32)
.build()
}