cat_gateway/db/index/queries/caches/rbac/
chains.rs

1//! RBAC Persistent Chains Cache.
2
3use catalyst_types::catalyst_id::CatalystId;
4use moka::policy::EvictionPolicy;
5use rbac_registration::registration::cardano::RegistrationChain;
6
7use crate::{service::utilities::cache::Cache, settings::Settings};
8
9/// RBAC Chains Cache.
10pub(crate) struct ChainsCache {
11    /// Interior cache type.
12    inner: Cache<CatalystId, RegistrationChain>,
13}
14
15impl ChainsCache {
16    /// Name for cache
17    const CACHE_NAME: &str = "RBAC Persistent Chains Cache";
18
19    /// New Chains Cache instance.
20    ///
21    /// # Arguments
22    /// * `is_persistent` - If the `CassandraSession` is persistent.
23    ///
24    /// Disables the cache for Volatile sessions (`is_persistent` is `false`).
25    pub(crate) fn new(is_persistent: bool) -> Self {
26        let max_capacity = if is_persistent {
27            Settings::rbac_cfg().persistent_chains_cache_size
28        } else {
29            0
30        };
31        Self {
32            inner: Cache::new(Self::CACHE_NAME, EvictionPolicy::lru(), max_capacity),
33        }
34    }
35
36    /// Get an entry from the cache
37    pub(crate) fn get(
38        &self,
39        key: &CatalystId,
40    ) -> Option<RegistrationChain> {
41        self.inner.get(key)
42    }
43
44    /// Get an entry from the cache
45    pub(crate) fn insert(
46        &self,
47        key: CatalystId,
48        value: RegistrationChain,
49    ) {
50        self.inner.insert(key, value);
51    }
52
53    /// Weighted-size of the cache.
54    pub(crate) fn weighted_size(&self) -> u64 {
55        self.inner.weighted_size()
56    }
57
58    /// Number of entries in the cache.
59    pub(crate) fn entry_count(&self) -> u64 {
60        self.inner.entry_count()
61    }
62
63    /// Returns `true` if the cache is enabled.
64    pub(crate) fn is_enabled(&self) -> bool {
65        self.inner.is_enabled()
66    }
67}