Skip to main content

key

Function key 

Source
pub fn key(parts: &[&(dyn Display + Sync)]) -> String
Expand description

Builds a cache key from its parts joined by /, like Rails’ cache_key_with_version.

Put in it everything the cached value depends on: the table, the id and updated_at of each record (so an update makes a new key and the old value expires with its TTL, no delete needed), the locale when the text is translated, and a version (v1) to bump when the template or type changes. Nested fragments compose keys (Russian doll caching): the outer key includes the newest updated_at of the records inside.

Keys longer than 256 bytes become sha256/<64 hex characters>, so any key fits KV’s 512-byte limit. Pure: no KV operation. Parts are Sync, so a key built inline in an awaited call keeps handler futures Send.

§Examples

use ocre::cache::key;

let (id, updated_at) = (12, "2026-09-29 14:05:00");
assert_eq!(key(&[&"posts", &id, &updated_at, &"v1"]), "posts/12/2026-09-29 14:05:00/v1");

let long = "x".repeat(300);
assert!(key(&[&long]).starts_with("sha256/"));
assert_eq!(key(&[&long]).len(), 7 + 64);