futu_cache/
login_cache.rs

1// 登录信息缓存
2
3use parking_lot::RwLock;
4
5/// 登录状态
6#[derive(Debug, Clone)]
7pub struct LoginState {
8    pub user_id: u32,
9    pub is_logged_in: bool,
10    pub login_account: String,
11    pub region: String,
12    pub server_addr: String,
13}
14
15/// 登录缓存
16pub struct LoginCache {
17    state: RwLock<Option<LoginState>>,
18}
19
20impl LoginCache {
21    pub fn new() -> Self {
22        Self {
23            state: RwLock::new(None),
24        }
25    }
26
27    pub fn set_login_state(&self, state: LoginState) {
28        *self.state.write() = Some(state);
29    }
30
31    pub fn get_login_state(&self) -> Option<LoginState> {
32        self.state.read().clone()
33    }
34
35    pub fn is_logged_in(&self) -> bool {
36        self.state
37            .read()
38            .as_ref()
39            .map(|s| s.is_logged_in)
40            .unwrap_or(false)
41    }
42
43    pub fn clear(&self) {
44        *self.state.write() = None;
45    }
46}
47
48impl Default for LoginCache {
49    fn default() -> Self {
50        Self::new()
51    }
52}