cache_fn/
options.rs

1use serde::{Deserialize, Serialize};
2use std::time::Duration;
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct CacheOptions {
6    pub worker: String,
7    pub api_key: String,
8    #[serde(with = "duration_millis")]
9    pub revalidate: Option<Duration>,
10    pub tags: Vec<String>,
11    pub should_cache: Option<bool>,
12}
13
14impl Default for CacheOptions {
15    fn default() -> Self {
16        CacheOptions {
17            worker: String::new(),
18            api_key: String::new(),
19            revalidate: Some(Duration::from_secs(604800)), // 1 week
20            tags: Vec::new(),
21            should_cache: None,
22        }
23    }
24}
25
26mod duration_millis {
27    use serde::{Deserialize, Deserializer, Serializer};
28    use std::time::Duration;
29
30    pub fn serialize<S>(dur: &Option<Duration>, ser: S) -> Result<S::Ok, S::Error>
31    where
32        S: Serializer,
33    {
34        match dur {
35            Some(d) => ser.serialize_u64(d.as_millis() as u64),
36            None => ser.serialize_none(),
37        }
38    }
39
40    pub fn deserialize<'de, D>(de: D) -> Result<Option<Duration>, D::Error>
41    where
42        D: Deserializer<'de>,
43    {
44        let millis: Option<u64> = Deserialize::deserialize(de)?;
45        Ok(millis.map(Duration::from_millis))
46    }
47}