Line data Source code
1 : use crate::cache::common::Cache;
2 : use crate::cache::{Cached, ControlPlaneResult, CplaneExpiry};
3 : use crate::config::CacheOptions;
4 : use crate::control_plane::NodeInfo;
5 : use crate::types::EndpointCacheKey;
6 :
7 : pub(crate) struct NodeInfoCache(moka::sync::Cache<EndpointCacheKey, ControlPlaneResult<NodeInfo>>);
8 : pub(crate) type CachedNodeInfo = Cached<&'static NodeInfoCache, NodeInfo>;
9 :
10 : impl Cache for NodeInfoCache {
11 : type Key = EndpointCacheKey;
12 : type Value = ControlPlaneResult<NodeInfo>;
13 :
14 6 : fn invalidate(&self, info: &EndpointCacheKey) {
15 6 : self.0.invalidate(info);
16 6 : }
17 : }
18 :
19 : impl NodeInfoCache {
20 13 : pub fn new(config: CacheOptions) -> Self {
21 13 : let builder = moka::sync::Cache::builder()
22 13 : .name("node_info_cache")
23 13 : .expire_after(CplaneExpiry::default());
24 13 : let builder = config.moka(builder);
25 13 : Self(builder.build())
26 13 : }
27 :
28 17 : pub fn insert(&self, key: EndpointCacheKey, value: ControlPlaneResult<NodeInfo>) {
29 17 : self.0.insert(key, value);
30 17 : }
31 :
32 0 : pub fn get(&'static self, key: &EndpointCacheKey) -> Option<ControlPlaneResult<NodeInfo>> {
33 0 : self.0.get(key)
34 0 : }
35 :
36 0 : pub fn get_entry(
37 0 : &'static self,
38 0 : key: &EndpointCacheKey,
39 0 : ) -> Option<ControlPlaneResult<CachedNodeInfo>> {
40 0 : self.get(key).map(|res| {
41 0 : res.map(|value| Cached {
42 0 : token: Some((self, key.clone())),
43 0 : value,
44 0 : })
45 0 : })
46 0 : }
47 : }
|