1pub mod error;
16
17use std::hash::Hash;
18use std::sync::Arc;
19use std::time::Duration;
20
21use catalog::kvbackend::new_table_cache;
22use common_meta::cache::{
23 CacheRegistry, CacheRegistryBuilder, LayeredCacheRegistryBuilder, new_schema_cache,
24 new_table_flownode_set_cache, new_table_info_cache, new_table_name_cache,
25 new_table_route_cache, new_table_schema_cache, new_view_info_cache,
26};
27use common_meta::kv_backend::KvBackendRef;
28use moka::future::{Cache, CacheBuilder};
29use partition::cache::new_partition_info_cache;
30use snafu::OptionExt;
31
32use crate::error::Result;
33
34const DEFAULT_CACHE_MAX_CAPACITY: u64 = 65536;
35const DEFAULT_CACHE_TTL: Duration = Duration::from_secs(10 * 60);
36const DEFAULT_CACHE_TTI: Duration = Duration::from_secs(5 * 60);
37
38fn default_cache<K: Send + Sync + Hash + Eq + 'static, V: Send + Sync + Clone + 'static>()
39-> Cache<K, V> {
40 CacheBuilder::new(DEFAULT_CACHE_MAX_CAPACITY)
41 .time_to_live(DEFAULT_CACHE_TTL)
42 .time_to_idle(DEFAULT_CACHE_TTI)
43 .build()
44}
45
46pub const TABLE_INFO_CACHE_NAME: &str = "table_info_cache";
47pub const VIEW_INFO_CACHE_NAME: &str = "view_info_cache";
48pub const TABLE_NAME_CACHE_NAME: &str = "table_name_cache";
49pub const TABLE_CACHE_NAME: &str = "table_cache";
50pub const SCHEMA_CACHE_NAME: &str = "schema_cache";
51pub const TABLE_SCHEMA_NAME_CACHE_NAME: &str = "table_schema_name_cache";
52pub const TABLE_FLOWNODE_SET_CACHE_NAME: &str = "table_flownode_set_cache";
53pub const TABLE_ROUTE_CACHE_NAME: &str = "table_route_cache";
54pub const PARTITION_INFO_CACHE_NAME: &str = "partition_info_cache";
55
56pub fn build_datanode_cache_registry(kv_backend: KvBackendRef) -> CacheRegistry {
60 let cache = CacheBuilder::new(DEFAULT_CACHE_MAX_CAPACITY).build();
62 let table_id_schema_cache = Arc::new(new_table_schema_cache(
63 TABLE_SCHEMA_NAME_CACHE_NAME.to_string(),
64 cache,
65 kv_backend.clone(),
66 ));
67
68 let cache = default_cache();
70 let schema_cache = Arc::new(new_schema_cache(
71 SCHEMA_CACHE_NAME.to_string(),
72 cache,
73 kv_backend.clone(),
74 ));
75
76 CacheRegistryBuilder::default()
77 .add_cache(table_id_schema_cache)
78 .add_cache(schema_cache)
79 .build()
80}
81
82pub fn build_fundamental_cache_registry(kv_backend: KvBackendRef) -> CacheRegistry {
90 let cache = default_cache();
92 let table_info_cache = Arc::new(new_table_info_cache(
93 TABLE_INFO_CACHE_NAME.to_string(),
94 cache,
95 kv_backend.clone(),
96 ));
97
98 let cache = default_cache();
100 let table_name_cache = Arc::new(new_table_name_cache(
101 TABLE_NAME_CACHE_NAME.to_string(),
102 cache,
103 kv_backend.clone(),
104 ));
105
106 let cache = default_cache();
108 let table_route_cache = Arc::new(new_table_route_cache(
109 TABLE_ROUTE_CACHE_NAME.to_string(),
110 cache,
111 kv_backend.clone(),
112 ));
113
114 let cache = default_cache();
116 let table_flownode_set_cache = Arc::new(new_table_flownode_set_cache(
117 TABLE_FLOWNODE_SET_CACHE_NAME.to_string(),
118 cache,
119 kv_backend.clone(),
120 ));
121 let cache = default_cache();
123 let view_info_cache = Arc::new(new_view_info_cache(
124 VIEW_INFO_CACHE_NAME.to_string(),
125 cache,
126 kv_backend.clone(),
127 ));
128
129 let cache = default_cache();
131 let schema_cache = Arc::new(new_schema_cache(
132 SCHEMA_CACHE_NAME.to_string(),
133 cache,
134 kv_backend.clone(),
135 ));
136
137 let table_id_schema_cache = Arc::new(new_table_schema_cache(
138 TABLE_SCHEMA_NAME_CACHE_NAME.to_string(),
139 CacheBuilder::new(DEFAULT_CACHE_MAX_CAPACITY).build(),
140 kv_backend,
141 ));
142 CacheRegistryBuilder::default()
143 .add_cache(table_info_cache)
144 .add_cache(table_name_cache)
145 .add_cache(table_route_cache)
146 .add_cache(view_info_cache)
147 .add_cache(table_flownode_set_cache)
148 .add_cache(schema_cache)
149 .add_cache(table_id_schema_cache)
150 .build()
151}
152
153pub fn with_default_composite_cache_registry(
155 builder: LayeredCacheRegistryBuilder,
156) -> Result<LayeredCacheRegistryBuilder> {
157 let table_info_cache = builder.get().context(error::CacheRequiredSnafu {
158 name: TABLE_INFO_CACHE_NAME,
159 })?;
160 let table_name_cache = builder.get().context(error::CacheRequiredSnafu {
161 name: TABLE_NAME_CACHE_NAME,
162 })?;
163 let table_route_cache = builder.get().context(error::CacheRequiredSnafu {
164 name: TABLE_ROUTE_CACHE_NAME,
165 })?;
166
167 let cache = default_cache();
169 let table_cache = Arc::new(new_table_cache(
170 TABLE_CACHE_NAME.to_string(),
171 cache,
172 table_info_cache,
173 table_name_cache,
174 ));
175
176 let cache = default_cache();
177 let partition_info_cache = Arc::new(new_partition_info_cache(
178 PARTITION_INFO_CACHE_NAME.to_string(),
179 cache,
180 table_route_cache,
181 ));
182
183 let registry = CacheRegistryBuilder::default()
184 .add_cache(table_cache)
185 .add_cache(partition_info_cache)
186 .build();
187
188 Ok(builder.add_cache_registry(registry))
189}