1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use crate::{validation::Error, Ref, Value};
use std::collections::HashMap;
use std::sync::RwLock;
/// Represents a monitored cache of configured options.
pub struct Cache<T>(RwLock<HashMap<String, Ref<T>>>);
impl<T> Default for Cache<T> {
#[inline]
fn default() -> Self {
Self(Default::default())
}
}
impl<T: Value> Cache<T> {
/// Initializes a new options [Cache].
#[inline]
pub fn new() -> Self {
Self::default()
}
/// Gets or adds options with the specified name.
///
/// # Arguments
///
/// * `name` - The optional name of the options
/// * `create` - The function used to create options when added
pub fn get_or_add(&self, name: &str, create: &dyn Fn(&str) -> Result<T, Error>) -> Result<Ref<T>, Error> {
let cache = self.0.read().unwrap();
if let Some(options) = cache.get(name) {
return Ok(options.clone());
}
drop(cache);
let options = Ref::new(create(name)?);
let mut cache = self.0.write().unwrap();
if let Some(options) = cache.get(name) {
Ok(options.clone())
} else {
cache.insert(name.to_owned(), options.clone());
Ok(options)
}
}
/// Attempts to add options with the specified name.
///
/// # Arguments
///
/// * `name` - The optional name of the options
/// * `options` - The options to add
///
/// # Remarks
///
/// Returns `true` if the options were added; otherwise, `false`. Options are not added if they already exist.
#[must_use]
pub fn try_add(&self, name: &str, options: T) -> bool {
let mut cache = self.0.write().unwrap();
if cache.contains_key(name) {
false
} else {
cache.insert(name.into(), Ref::new(options));
true
}
}
/// Removes options with the specified name, if any.
///
/// # Arguments
///
/// * `name` - The optional name of the options
///
/// # Remarks
///
/// Returns `true` if any options were removed; otherwise, `false`.
pub fn remove(&self, name: &str) -> bool {
self.0.write().unwrap().remove(name).is_some()
}
/// Clears all options from the cache.
pub fn clear(&self) {
self.0.write().unwrap().clear()
}
}