Skip to main content

futu_cache/
login_cache.rs

1// 登录信息缓存
2
3use parking_lot::RwLock;
4
5/// 登录状态
6#[derive(Debug, Clone)]
7pub struct LoginState {
8    /// 登录后服务端分配的 user_id
9    pub user_id: u32,
10    /// 是否处于已登录状态
11    pub is_logged_in: bool,
12    /// 登录账号(归一化后,不含区号前缀)
13    pub login_account: String,
14    /// 归属地区标识
15    pub region: String,
16    /// 用户注册归属地,数值对齐 Common.UserAttribution / GetUserInfo.userAttribution。
17    pub user_attribution: Option<i32>,
18    /// 已建立连接的服务端地址
19    pub server_addr: String,
20}
21
22/// 登录缓存
23pub struct LoginCache {
24    state: RwLock<Option<LoginState>>,
25}
26
27impl LoginCache {
28    pub fn new() -> Self {
29        Self {
30            state: RwLock::new(None),
31        }
32    }
33
34    pub fn set_login_state(&self, state: LoginState) {
35        *self.state.write() = Some(state);
36    }
37
38    pub fn get_login_state(&self) -> Option<LoginState> {
39        self.state.read().clone()
40    }
41
42    pub fn is_logged_in(&self) -> bool {
43        self.state
44            .read()
45            .as_ref()
46            .map(|s| s.is_logged_in)
47            .unwrap_or(false)
48    }
49
50    pub fn clear(&self) {
51        *self.state.write() = None;
52    }
53}
54
55impl Default for LoginCache {
56    fn default() -> Self {
57        Self::new()
58    }
59}