Skip to main content

rs_zero/cache/
key.rs

1/// Namespaced cache key.
2#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3pub struct CacheKey {
4    namespace: String,
5    parts: Vec<String>,
6}
7
8impl CacheKey {
9    /// Creates a key with namespace.
10    pub fn new(
11        namespace: impl Into<String>,
12        parts: impl IntoIterator<Item = impl Into<String>>,
13    ) -> Self {
14        Self {
15            namespace: namespace.into(),
16            parts: parts.into_iter().map(Into::into).collect(),
17        }
18    }
19
20    /// Renders a stable colon-separated key.
21    pub fn render(&self) -> String {
22        std::iter::once(self.namespace.as_str())
23            .chain(self.parts.iter().map(String::as_str))
24            .collect::<Vec<_>>()
25            .join(":")
26    }
27}