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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use std::collections::HashMap;
use std::fmt::Debug;
use std::time::Duration;
use async_trait::async_trait;
use log::debug;
use moka::sync::CacheBuilder;
use moka::sync::SegmentedCache;
use crate::raw::adapters::kv;
use crate::raw::*;
use crate::*;
#[derive(Default, Debug)]
pub struct MokaBuilder {
name: Option<String>,
max_capacity: Option<u64>,
time_to_live: Option<Duration>,
time_to_idle: Option<Duration>,
num_segments: Option<usize>,
thread_pool_enabled: Option<bool>,
}
impl MokaBuilder {
pub fn name(&mut self, v: &str) -> &mut Self {
if !v.is_empty() {
self.name = Some(v.to_owned());
}
self
}
pub fn max_capacity(&mut self, v: u64) -> &mut Self {
if v != 0 {
self.max_capacity = Some(v);
}
self
}
pub fn time_to_live(&mut self, v: Duration) -> &mut Self {
if !v.is_zero() {
self.time_to_live = Some(v);
}
self
}
pub fn time_to_idle(&mut self, v: Duration) -> &mut Self {
if !v.is_zero() {
self.time_to_idle = Some(v);
}
self
}
pub fn segments(&mut self, v: usize) -> &mut Self {
assert!(v != 0);
self.num_segments = Some(v);
self
}
pub fn thread_pool_enabled(&mut self, v: bool) -> &mut Self {
self.thread_pool_enabled = Some(v);
self
}
}
impl Builder for MokaBuilder {
const SCHEME: Scheme = Scheme::Moka;
type Accessor = MokaBackend;
fn from_map(map: HashMap<String, String>) -> Self {
let mut builder = MokaBuilder::default();
map.get("name").map(|v| builder.name(v));
map.get("max_capacity")
.map(|v| v.parse::<u64>().map(|v| builder.max_capacity(v)));
map.get("time_to_live").map(|v| {
v.parse::<u64>()
.map(|v| builder.time_to_live(Duration::from_secs(v)))
});
map.get("time_to_idle").map(|v| {
v.parse::<u64>()
.map(|v| builder.time_to_idle(Duration::from_secs(v)))
});
map.get("num_segments")
.map(|v| v.parse::<usize>().map(|v| builder.segments(v)));
map.get("thread_pool_enabled")
.map(|v| v.parse::<bool>().map(|v| builder.thread_pool_enabled(v)));
builder
}
fn build(&mut self) -> Result<Self::Accessor> {
debug!("backend build started: {:?}", &self);
let mut builder: CacheBuilder<String, Vec<u8>, _> =
SegmentedCache::builder(self.num_segments.unwrap_or(1))
.thread_pool_enabled(self.thread_pool_enabled.unwrap_or(false));
builder = builder.weigher(|k, v| (k.len() + v.len()) as u32);
if let Some(v) = &self.name {
builder = builder.name(v);
}
if let Some(v) = self.max_capacity {
builder = builder.max_capacity(v)
}
if let Some(v) = self.time_to_live {
builder = builder.time_to_live(v)
}
if let Some(v) = self.time_to_idle {
builder = builder.time_to_idle(v)
}
debug!("backend build finished: {:?}", &self);
Ok(MokaBackend::new(Adapter {
inner: builder.build(),
}))
}
}
pub type MokaBackend = kv::Backend<Adapter>;
#[derive(Clone)]
pub struct Adapter {
inner: SegmentedCache<String, Vec<u8>>,
}
impl Debug for Adapter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Adapter")
.field("size", &self.inner.weighted_size())
.field("count", &self.inner.entry_count())
.finish()
}
}
#[async_trait]
impl kv::Adapter for Adapter {
fn metadata(&self) -> kv::Metadata {
kv::Metadata::new(
Scheme::Moka,
self.inner.name().unwrap_or("moka"),
AccessorCapability::Read | AccessorCapability::Write,
)
}
async fn get(&self, path: &str) -> Result<Option<Vec<u8>>> {
self.blocking_get(path)
}
fn blocking_get(&self, path: &str) -> Result<Option<Vec<u8>>> {
match self.inner.get(path) {
None => Ok(None),
Some(bs) => Ok(Some(bs)),
}
}
async fn set(&self, path: &str, value: &[u8]) -> Result<()> {
self.blocking_set(path, value)
}
fn blocking_set(&self, path: &str, value: &[u8]) -> Result<()> {
self.inner.insert(path.to_string(), value.to_vec());
Ok(())
}
async fn delete(&self, path: &str) -> Result<()> {
self.blocking_delete(path)
}
fn blocking_delete(&self, path: &str) -> Result<()> {
self.inner.invalidate(path);
Ok(())
}
}