ballista_cache/
lib.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use crate::backend::policy::CachePolicy;
19use crate::backend::CacheBackend;
20use crate::listener::{
21    cache_policy::CachePolicyWithListener, loading_cache::LoadingCacheWithListener,
22};
23use crate::loading_cache::{driver::CacheDriver, loader::CacheLoader};
24use std::fmt::Debug;
25use std::hash::Hash;
26use std::sync::Arc;
27
28pub mod backend;
29pub mod listener;
30pub mod loading_cache;
31pub mod metrics;
32
33pub type DefaultLoadingCache<K, V, L> = LoadingCacheWithListener<CacheDriver<K, V, L>>;
34pub type LoadingCacheMetrics<K, V> = metrics::loading_cache::Metrics<K, V>;
35
36pub fn create_loading_cache_with_metrics<K, V, L>(
37    policy: impl CachePolicy<K = K, V = V>,
38    loader: Arc<L>,
39) -> (DefaultLoadingCache<K, V, L>, Arc<LoadingCacheMetrics<K, V>>)
40where
41    K: Clone + Eq + Hash + Debug + Ord + Send + 'static,
42    V: Clone + Debug + Send + 'static,
43    L: CacheLoader<K = K, V = V>,
44{
45    let metrics = Arc::new(metrics::loading_cache::Metrics::new());
46
47    let policy_with_metrics = CachePolicyWithListener::new(policy, vec![metrics.clone()]);
48    let cache_backend = CacheBackend::new(policy_with_metrics);
49    let loading_cache = CacheDriver::new(cache_backend, loader);
50    let loading_cache_with_metrics =
51        LoadingCacheWithListener::new(loading_cache, vec![metrics.clone()]);
52
53    (loading_cache_with_metrics, metrics)
54}