use std::time::Duration;
use crate::TagSet;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CacheOptions {
ttl: Option<Duration>,
tags: Vec<String>,
}
impl CacheOptions {
pub fn new() -> Self {
Self::default()
}
pub fn ttl(mut self, ttl: Duration) -> Self {
self.ttl = Some(ttl);
self
}
pub fn tag(mut self, tag: impl Into<String>) -> Self {
self.tags.push(tag.into());
self
}
pub fn tags<I, S>(mut self, tags: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.tags = tags.into_iter().map(Into::into).collect();
self
}
pub fn tag_set(mut self, tags: TagSet) -> Self {
self.tags = tags.into_vec();
self
}
pub fn ttl_value(&self) -> Option<Duration> {
self.ttl
}
pub fn tags_value(&self) -> &[String] {
&self.tags
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::TagSet;
#[test]
fn cache_options_tag_set_replaces_existing_tags() {
let options = CacheOptions::new()
.tag("old")
.tag_set(TagSet::new().tag("new").entity("user", 42));
assert_eq!(
options.tags_value(),
&["new".to_owned(), "user:42".to_owned()]
);
}
}