Skip to main content

futu_gateway_core/bridge/
cache_bundle.rs

1//! Cache ownership boundary for [`super::GatewayBridge`].
2//!
3//! `GatewayBridge` owns cache state through this bundle so handlers and startup
4//! workers do not depend on individual bridge cache fields.
5
6use std::sync::Arc;
7
8use crate::stock_db::StockDb;
9use futu_cache::login_cache::LoginCache;
10use futu_cache::option_chain_statistic::OptionChainStatisticCache;
11use futu_cache::qot_cache::QotCache;
12use futu_cache::qot_right::QotRightCache;
13use futu_cache::risk_free_rate::RiskFreeRateCache;
14use futu_cache::static_data::StaticDataCache;
15use futu_cache::trd_cache::TrdCache;
16use futu_cache::user_profile::UserProfileCache;
17
18#[derive(Clone)]
19pub struct CacheBundle {
20    pub qot_cache: Arc<QotCache>,
21    pub trd_cache: Arc<TrdCache>,
22    pub static_cache: Arc<StaticDataCache>,
23    pub login_cache: Arc<LoginCache>,
24    /// CMD7506 用户昵称/头像缓存。GetUserInfo Basic 对齐 C++
25    /// `NNProto_UserProfile::QueryProfile -> INNData_UserProfile`.
26    pub user_profile_cache: Arc<UserProfileCache>,
27    /// 行情权限缓存 (CMD 6024 响应).
28    pub qot_right_cache: Arc<QotRightCache>,
29    /// CMD20231 无风险利率缓存。C++ 在登录成功后刷新
30    /// `INNData_Qot_RiskFreeRate`,3255/3257 break-even 概率读取该快照。
31    pub risk_free_rate_cache: Arc<RiskFreeRateCache>,
32    /// CMD20675 期权链统计缓存。C++ 3255/3257 在 snapshot 前拉链统计,
33    /// break-even 概率优先读 strike-date IV,再读 underlying IV/HV。
34    pub option_chain_statistic_cache: Arc<OptionChainStatisticCache>,
35    /// PriceReminder 数量上限 cooldown cache.
36    ///
37    /// 抽自 SetPriceReminder handler, 对齐 C++ `INNData_PriceReminder` 的
38    /// cooldown 状态机:total limit 与 per-stock-type limit 均为 ack-then-commit.
39    pub price_reminder_cooldown:
40        Arc<futu_cache::price_reminder_cooldown::PriceReminderCooldownCache>,
41    /// Crypto LV2 多交易所缓存,供 crypto order book handler 与 push parser 共享.
42    pub crypto_exchange_cache: Arc<futu_cache::crypto_exchange_cache::CryptoExchangeCache>,
43    /// C++ SecListDBHelper 等价的本地静态证券 SQLite 句柄。
44    ///
45    /// stock-list updater 和 GetReference(Warrant) 共享同一实例,避免 handler
46    /// 重新打开 SQLite 形成独立视图。
47    pub stock_db: Arc<arc_swap::ArcSwapOption<StockDb>>,
48}
49
50impl CacheBundle {
51    pub fn new() -> Self {
52        Self {
53            qot_cache: Arc::new(QotCache::new()),
54            trd_cache: Arc::new(TrdCache::new()),
55            static_cache: Arc::new(StaticDataCache::new()),
56            login_cache: Arc::new(LoginCache::new()),
57            user_profile_cache: Arc::new(UserProfileCache::new()),
58            qot_right_cache: Arc::new(QotRightCache::new()),
59            risk_free_rate_cache: RiskFreeRateCache::new(),
60            option_chain_statistic_cache: OptionChainStatisticCache::new(),
61            price_reminder_cooldown:
62                futu_cache::price_reminder_cooldown::PriceReminderCooldownCache::new(),
63            crypto_exchange_cache: futu_cache::crypto_exchange_cache::CryptoExchangeCache::new(),
64            stock_db: Arc::new(arc_swap::ArcSwapOption::from(None)),
65        }
66    }
67}
68
69impl Default for CacheBundle {
70    fn default() -> Self {
71        Self::new()
72    }
73}
74
75#[cfg(test)]
76mod tests;