dioxus_isrg/
freshness.rs

1use std::time::Duration;
2
3use chrono::{DateTime, Utc};
4
5/// Information about the freshness of a rendered response
6#[derive(Debug, Clone, Copy)]
7pub struct RenderFreshness {
8    /// The age of the rendered response
9    age: u64,
10    /// The maximum age of the rendered response
11    max_age: Option<u64>,
12    /// The time the response was rendered
13    timestamp: DateTime<Utc>,
14}
15
16impl RenderFreshness {
17    /// Create new freshness information
18    pub(crate) fn new(age: u64, max_age: u64, timestamp: DateTime<Utc>) -> Self {
19        Self {
20            age,
21            max_age: Some(max_age),
22            timestamp,
23        }
24    }
25
26    /// Create new freshness information with only the age
27    pub(crate) fn new_age(age: u64, timestamp: DateTime<Utc>) -> Self {
28        Self {
29            age,
30            max_age: None,
31            timestamp,
32        }
33    }
34
35    /// Create new freshness information from a timestamp
36    pub(crate) fn created_at(timestamp: DateTime<Utc>, max_age: Option<Duration>) -> Self {
37        Self {
38            age: timestamp
39                .signed_duration_since(Utc::now())
40                .num_seconds()
41                .unsigned_abs(),
42            max_age: max_age.map(|d| d.as_secs()),
43            timestamp,
44        }
45    }
46
47    /// Create new freshness information at the current time
48    pub fn now(max_age: Option<Duration>) -> Self {
49        Self {
50            age: 0,
51            max_age: max_age.map(|d| d.as_secs()),
52            timestamp: Utc::now(),
53        }
54    }
55
56    /// Get the age of the rendered response in seconds
57    pub fn age(&self) -> u64 {
58        self.age
59    }
60
61    /// Get the maximum age of the rendered response in seconds
62    pub fn max_age(&self) -> Option<u64> {
63        self.max_age
64    }
65
66    /// Get the time the response was rendered
67    pub fn timestamp(&self) -> DateTime<Utc> {
68        self.timestamp
69    }
70
71    /// Write the freshness to the response headers.
72    pub fn write(&self, headers: &mut http::HeaderMap<http::HeaderValue>) {
73        let age = self.age();
74        headers.insert(http::header::AGE, age.into());
75        if let Some(max_age) = self.max_age() {
76            headers.insert(
77                http::header::CACHE_CONTROL,
78                http::HeaderValue::from_str(&format!("max-age={}", max_age)).unwrap(),
79            );
80        }
81    }
82}