1use std::collections::HashMap;
2use std::sync::Arc;
3use std::time::Duration;
4
5use awa_model::admin;
6use awa_model::job::JobState;
7use awa_model::AwaError;
8use moka::future::Cache;
9
10#[derive(Clone)]
20pub struct DashboardCache {
21 pub stats: Cache<(), HashMap<JobState, i64>>,
22 pub queues: Cache<(), Vec<admin::QueueOverview>>,
23 pub runtime: Cache<(), admin::RuntimeOverview>,
24 pub queue_runtime: Cache<(), Vec<admin::QueueRuntimeSummary>>,
25 pub storage: Cache<(), awa_model::storage::StorageStatusReport>,
26}
27
28fn build_cache<V: Clone + Send + Sync + 'static>(ttl: Duration) -> Cache<(), V> {
29 Cache::builder().time_to_live(ttl).max_capacity(1).build()
30}
31
32impl DashboardCache {
33 pub fn new(ttl: Duration) -> Self {
34 Self {
35 stats: build_cache(ttl),
36 queues: build_cache(ttl),
37 runtime: build_cache(ttl),
38 queue_runtime: build_cache(ttl),
39 storage: build_cache(ttl),
40 }
41 }
42}
43
44#[derive(Debug, Clone)]
49pub struct CacheError(pub Arc<AwaError>);
50
51impl From<AwaError> for CacheError {
52 fn from(err: AwaError) -> Self {
53 Self(Arc::new(err))
54 }
55}