hydracache_core/
options.rs1use std::time::Duration;
2
3use crate::TagSet;
4
5#[derive(Debug, Clone, Default, PartialEq, Eq)]
24pub struct CacheOptions {
25 ttl: Option<Duration>,
26 tags: Vec<String>,
27}
28
29impl CacheOptions {
30 pub fn new() -> Self {
32 Self::default()
33 }
34
35 pub fn ttl(mut self, ttl: Duration) -> Self {
37 self.ttl = Some(ttl);
38 self
39 }
40
41 pub fn tag(mut self, tag: impl Into<String>) -> Self {
43 self.tags.push(tag.into());
44 self
45 }
46
47 pub fn tags<I, S>(mut self, tags: I) -> Self
49 where
50 I: IntoIterator<Item = S>,
51 S: Into<String>,
52 {
53 self.tags = tags.into_iter().map(Into::into).collect();
54 self
55 }
56
57 pub fn tag_set(mut self, tags: TagSet) -> Self {
59 self.tags = tags.into_vec();
60 self
61 }
62
63 pub fn ttl_value(&self) -> Option<Duration> {
65 self.ttl
66 }
67
68 pub fn tags_value(&self) -> &[String] {
70 &self.tags
71 }
72}
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77 use crate::TagSet;
78
79 #[test]
80 fn cache_options_tag_set_replaces_existing_tags() {
81 let options = CacheOptions::new()
82 .tag("old")
83 .tag_set(TagSet::new().tag("new").entity("user", 42));
84
85 assert_eq!(
86 options.tags_value(),
87 &["new".to_owned(), "user:42".to_owned()]
88 );
89 }
90}