EXPIRE: set or update TTL on an existing key. Returns true if the key was found and TTL was set.
(
&mut self,
tenant_id: u64,
collection: &str,
key: &[u8],
ttl_ms: u64,
now_ms: u64,
)
| 181 | /// EXPIRE: set or update TTL on an existing key. |
| 182 | /// Returns true if the key was found and TTL was set. |
| 183 | pub fn expire( |
| 184 | &mut self, |
| 185 | tenant_id: u64, |
| 186 | collection: &str, |
| 187 | key: &[u8], |
| 188 | ttl_ms: u64, |
| 189 | now_ms: u64, |
| 190 | ) -> bool { |
| 191 | let tkey = table_key(tenant_id, collection); |
| 192 | let table = match self.tables.get_mut(&tkey) { |
| 193 | Some(t) => t, |
| 194 | None => return false, |
| 195 | }; |
| 196 | |
| 197 | // Cancel old expiry. |
| 198 | if let Some(meta) = table.get_entry_meta(key) |
| 199 | && meta.has_ttl |
| 200 | { |
| 201 | let composite = expiry_key(tenant_id, collection, key); |
| 202 | self.expiry.cancel(&composite, meta.expire_at_ms); |
| 203 | } |
| 204 | |
| 205 | let expire_at = now_ms + ttl_ms; |
| 206 | if table.set_expire(key, expire_at) { |
| 207 | let composite = expiry_key(tenant_id, collection, key); |
| 208 | self.expiry.insert(composite, expire_at); |
| 209 | true |
| 210 | } else { |
| 211 | false |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /// PERSIST: remove TTL from a key. Returns true if the key was found. |
| 216 | pub fn persist(&mut self, tenant_id: u64, collection: &str, key: &[u8]) -> bool { |
no test coverage detected