ballista_cache/loading_cache/loader.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 async_trait::async_trait;
19use std::fmt::Debug;
20use std::hash::Hash;
21
22/// Loader for missing [`Cache`](crate::cache::Cache) entries.
23#[async_trait]
24pub trait CacheLoader: Debug + Send + Sync + 'static {
25 /// Cache key.
26 type K: Debug + Hash + Send + 'static;
27
28 /// Cache value.
29 type V: Debug + Send + 'static;
30
31 /// Extra data needed when loading a missing entry. Specify `()` if not needed.
32 type Extra: Debug + Send + 'static;
33
34 /// Load value for given key, using the extra data if needed.
35 async fn load(&self, k: Self::K, extra: Self::Extra) -> Self::V;
36}
37
38#[async_trait]
39impl<K, V, Extra> CacheLoader for Box<dyn CacheLoader<K = K, V = V, Extra = Extra>>
40where
41 K: Debug + Hash + Send + 'static,
42 V: Debug + Send + 'static,
43 Extra: Debug + Send + 'static,
44{
45 type K = K;
46 type V = V;
47 type Extra = Extra;
48
49 async fn load(&self, k: Self::K, extra: Self::Extra) -> Self::V {
50 self.as_ref().load(k, extra).await
51 }
52}