iceberg_cache_moka/
lib.rs1use std::hash::Hash;
19use std::sync::Arc;
20
21use iceberg::cache::{ObjectCache, ObjectCacheProvide};
22use iceberg::spec::{Manifest, ManifestList};
23
24const DEFAULT_CACHE_SIZE_BYTES: u64 = 32 * 1024 * 1024; struct MokaObjectCache<K, V>(moka::sync::Cache<K, V>);
27
28impl<K, V> ObjectCache<K, V> for MokaObjectCache<K, V>
29where
30 K: Hash + Eq + Send + Sync + 'static,
31 V: Clone + Send + Sync + 'static,
32{
33 fn get(&self, key: &K) -> Option<V> {
34 self.0.get(key)
35 }
36
37 fn set(&self, key: K, value: V) {
38 self.0.insert(key, value);
39 }
40}
41
42pub struct MokaObjectCacheProvider {
44 manifest_cache: MokaObjectCache<String, Arc<Manifest>>,
45 manifest_list_cache: MokaObjectCache<String, Arc<ManifestList>>,
46}
47
48impl Default for MokaObjectCacheProvider {
49 fn default() -> Self {
50 Self::new()
51 }
52}
53
54impl MokaObjectCacheProvider {
55 pub fn new() -> Self {
57 let manifest_cache = MokaObjectCache(moka::sync::Cache::new(DEFAULT_CACHE_SIZE_BYTES));
58 let manifest_list_cache = MokaObjectCache(moka::sync::Cache::new(DEFAULT_CACHE_SIZE_BYTES));
59
60 Self {
61 manifest_cache,
62 manifest_list_cache,
63 }
64 }
65
66 pub fn with_manifest_cache(mut self, cache: moka::sync::Cache<String, Arc<Manifest>>) -> Self {
68 self.manifest_cache = MokaObjectCache(cache);
69 self
70 }
71
72 pub fn with_manifest_list_cache(
74 mut self,
75 cache: moka::sync::Cache<String, Arc<ManifestList>>,
76 ) -> Self {
77 self.manifest_list_cache = MokaObjectCache(cache);
78 self
79 }
80}
81
82impl ObjectCacheProvide for MokaObjectCacheProvider {
83 fn manifest_cache(&self) -> &dyn ObjectCache<String, Arc<Manifest>> {
84 &self.manifest_cache
85 }
86
87 fn manifest_list_cache(&self) -> &dyn ObjectCache<String, Arc<ManifestList>> {
88 &self.manifest_list_cache
89 }
90}