open_lark/core/
cache.rs

1use std::time::Duration;
2
3use quick_cache::sync::Cache;
4use tokio::time::Instant;
5
6#[derive(Debug)]
7pub struct QuickCache<T: Clone> {
8    cache: Cache<String, (T, Instant)>,
9}
10
11impl<T: Clone> Default for QuickCache<T> {
12    fn default() -> Self {
13        Self::new()
14    }
15}
16
17impl<T: Clone> QuickCache<T> {
18    pub fn new() -> Self {
19        let cache = Cache::new(10);
20        Self { cache } //
21    }
22
23    pub fn get(&self, key: &str) -> Option<T> {
24        match self.cache.get(key) {
25            None => None,
26            Some((value, expire_time)) => {
27                if expire_time > Instant::now() {
28                    Some(value)
29                } else {
30                    self.cache.remove(key);
31                    None
32                }
33            }
34        }
35    }
36
37    /// Set a key-value pair with an expire time in seconds.
38    pub fn set(&mut self, key: &str, value: T, expire_time: i32) {
39        let expire_time = Instant::now() + Duration::from_secs(expire_time as u64);
40        self.cache.insert(key.to_string(), (value, expire_time));
41    }
42
43    /// 获取值和过期信息
44    pub fn get_with_expiry(&self, key: &str) -> Option<CacheEntry<T>> {
45        match self.cache.get(key) {
46            None => None,
47            Some((value, expire_time)) => {
48                let now = Instant::now();
49                if expire_time > now {
50                    Some(CacheEntry {
51                        value,
52                        expires_at: expire_time,
53                        current_time: now,
54                    })
55                } else {
56                    self.cache.remove(key);
57                    None
58                }
59            }
60        }
61    }
62}
63
64/// 缓存条目信息,包含值和过期时间
65#[derive(Debug, Clone)]
66pub struct CacheEntry<T> {
67    pub value: T,
68    pub expires_at: Instant,
69    pub current_time: Instant,
70}
71
72impl<T> CacheEntry<T> {
73    /// 获取剩余的过期秒数
74    pub fn expiry_seconds(&self) -> u64 {
75        if self.expires_at > self.current_time {
76            (self.expires_at - self.current_time).as_secs()
77        } else {
78            0
79        }
80    }
81
82    /// 检查是否即将过期(剩余时间少于指定秒数)
83    pub fn expires_within(&self, seconds: u64) -> bool {
84        self.expiry_seconds() <= seconds
85    }
86}