Skip to main content

rs_tenant/
cache.rs

1use crate::permission::Permission;
2use crate::types::{PrincipalId, RoleId, TenantId};
3use async_trait::async_trait;
4
5/// Cache interface for effective permissions.
6#[async_trait]
7pub trait Cache: Send + Sync {
8    /// Gets cached permissions for a (tenant, principal) pair.
9    async fn get_permissions(
10        &self,
11        tenant: &TenantId,
12        principal: &PrincipalId,
13    ) -> Option<Vec<Permission>>;
14
15    /// Sets cached permissions for a (tenant, principal) pair.
16    async fn set_permissions(
17        &self,
18        tenant: &TenantId,
19        principal: &PrincipalId,
20        perms: Vec<Permission>,
21    );
22
23    /// Invalidates cache for a principal.
24    async fn invalidate_principal(&self, tenant: &TenantId, principal: &PrincipalId);
25
26    /// Invalidates cache for a role.
27    async fn invalidate_role(&self, tenant: &TenantId, role: &RoleId);
28
29    /// Invalidates cache for a tenant.
30    async fn invalidate_tenant(&self, tenant: &TenantId);
31}
32
33/// No-op cache implementation.
34#[derive(Debug, Default, Clone, Copy)]
35pub struct NoCache;
36
37#[async_trait]
38impl Cache for NoCache {
39    async fn get_permissions(
40        &self,
41        _tenant: &TenantId,
42        _principal: &PrincipalId,
43    ) -> Option<Vec<Permission>> {
44        None
45    }
46
47    async fn set_permissions(
48        &self,
49        _tenant: &TenantId,
50        _principal: &PrincipalId,
51        _perms: Vec<Permission>,
52    ) {
53    }
54
55    async fn invalidate_principal(&self, _tenant: &TenantId, _principal: &PrincipalId) {}
56
57    async fn invalidate_role(&self, _tenant: &TenantId, _role: &RoleId) {}
58
59    async fn invalidate_tenant(&self, _tenant: &TenantId) {}
60}