7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1 | // Copyright 2025 The Matrix.org Foundation C.I.C. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 2 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 4 | // you may not use this file except in compliance with the License. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 5 | // You may obtain a copy of the License at |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 6 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 8 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 9 | // Unless required by applicable law or agreed to in writing, software |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 12 | // See the License for the specific language governing permissions and |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 13 | // limitations under the License. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 14 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 15 | //! Types to implement TTL caches which can be used to persist data for a fixed |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 16 | //! duration. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 17 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 18 | use ruma::time::SystemTime; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 19 | use serde::{Deserialize, Serialize}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 20 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 21 | /// A value that expires after some time. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 22 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 23 | /// This value is (de)serializable so it can be persisted in a store. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 24 | #[derive(Debug, Clone, Serialize, Deserialize)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 25 | pub struct TtlValue<T> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 26 | /// The data of the item. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 27 | #[serde(flatten)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 28 | data: T, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 29 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 30 | /// Last time we fetched this data from the server, in milliseconds since |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 31 | /// UNIX epoch. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 32 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 33 | /// When this field is missing during deserialization, it defaults to `0.0`, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 34 | /// which means that the data is always expired. This allows to be |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 35 | /// compatible with data that was persisted before deciding to add an |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 36 | /// expiration time. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 37 | #[serde(default = "default_timestamp")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 38 | last_fetch_ts: Option<f64>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 39 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 40 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 41 | impl<T> TtlValue<T> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 42 | /// The number of milliseconds after which the data is considered stale. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 43 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 44 | /// This matches 1 day. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 45 | pub const STALE_THRESHOLD: f64 = (1000 * 60 * 60 * 24) as _; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 46 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 47 | /// Construct a new `TtlValue` with the given data. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 48 | pub fn new(data: T) -> Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 49 | Self { data, last_fetch_ts: Some(now_timestamp_ms()) } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 50 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 51 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 52 | /// Construct a new `TtlValue` with the given data that never expires. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 53 | pub fn without_expiry(data: T) -> Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 54 | Self { data, last_fetch_ts: None } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 55 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 56 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 57 | /// Converts from `&TtlValue<T>` to `TtlValue<&T>`. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 58 | pub fn as_ref(&self) -> TtlValue<&T> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 59 | TtlValue { data: &self.data, last_fetch_ts: self.last_fetch_ts } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 60 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 61 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 62 | /// Transform the data of this `TtlValue` with the given function. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 63 | pub fn map<U, F>(self, f: F) -> TtlValue<U> |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 64 | where |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 65 | F: FnOnce(T) -> U, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 66 | { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 67 | TtlValue { data: f(self.data), last_fetch_ts: self.last_fetch_ts } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 68 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 69 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 70 | /// Whether this value has expired. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 71 | pub fn has_expired(&self) -> bool { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 72 | self.last_fetch_ts.is_some_and(|ts| now_timestamp_ms() - ts >= Self::STALE_THRESHOLD) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 73 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 74 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 75 | /// Mark this value has expired. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 76 | pub fn expire(&mut self) { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 77 | // We assume that the system time is always correct and we are far from the UNIX |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 78 | // epoch so a timestamp of 0 should always be expired. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 79 | self.last_fetch_ts = Some(0.0) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 80 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 81 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 82 | /// Get a reference to the data of this value. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 83 | pub fn data(&self) -> &T { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 84 | &self.data |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 85 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 86 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 87 | /// Get the data of this value. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 88 | pub fn into_data(self) -> T { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 89 | self.data |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 90 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 91 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 92 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 93 | /// Get the current timestamp as the number of milliseconds since Unix Epoch. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 94 | fn now_timestamp_ms() -> f64 { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 95 | SystemTime::now() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 96 | .duration_since(SystemTime::UNIX_EPOCH) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 97 | .expect("System clock was before 1970.") |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 98 | .as_secs_f64() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 99 | * 1000.0 |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 100 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 101 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 102 | /// The default timestamp if it is missing during deserialization. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 103 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 104 | /// We expect that a value that was serialized always has an expiry time, so the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 105 | /// default is `Some(0.0)`. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 106 | fn default_timestamp() -> Option<f64> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 107 | Some(0.0) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 108 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 109 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 110 | #[cfg(test)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 111 | mod tests { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 112 | use serde::{Deserialize, Serialize}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 113 | use serde_json::json; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 114 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 115 | use super::{TtlValue, now_timestamp_ms}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 116 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 117 | #[test] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 118 | fn test_ttl_value_expiry() { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 119 | // Definitely stale. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 120 | let ttl_value = TtlValue { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 121 | data: (), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 122 | last_fetch_ts: Some(now_timestamp_ms() - TtlValue::<()>::STALE_THRESHOLD - 1.0), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 123 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 124 | assert!(ttl_value.has_expired()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 125 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 126 | // Definitely not stale. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 127 | let ttl_value = TtlValue::new(()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 128 | assert!(!ttl_value.has_expired()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 129 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 130 | // Cannot be stale. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 131 | let ttl_value = TtlValue::without_expiry(()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 132 | assert!(!ttl_value.has_expired()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 133 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 134 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 135 | #[test] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 136 | fn test_ttl_value_serialize_roundtrip() { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 137 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 138 | struct Data { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 139 | foo: String, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 140 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 141 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 142 | let data = Data { foo: "bar".to_owned() }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 143 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 144 | // With timestamp. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 145 | let ttl_value = TtlValue { data: data.clone(), last_fetch_ts: Some(1000.0) }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 146 | let json = json!({ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 147 | "foo": "bar", |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 148 | "last_fetch_ts": 1000.0, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 149 | }); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 150 | assert_eq!(serde_json::to_value(&ttl_value).unwrap(), json); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 151 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 152 | let deserialized = serde_json::from_value::<TtlValue<Data>>(json).unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 153 | assert_eq!(deserialized.data, data); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 154 | assert!(deserialized.last_fetch_ts.unwrap() - ttl_value.last_fetch_ts.unwrap() < 0.0001); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 155 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 156 | // Without timestamp the value is always expired in theory. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 157 | let json = json!({ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 158 | "foo": "bar", |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 159 | }); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 160 | let deserialized = serde_json::from_value::<TtlValue<Data>>(json).unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 161 | assert_eq!(deserialized.data, data); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 162 | assert!(deserialized.last_fetch_ts.unwrap() - 0.0 < 0.0001); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 163 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 164 | } |