spawn-access-control 0.1.12

A Rust library for access control management with WebAssembly support, including role-based access control (RBAC), permissions, and audit logging.
Documentation
use redis::{Client, Commands, RedisError};
use crate::cache::OptimizedCache;
use std::time::Duration;

pub struct DistributedCache {
    local_cache: OptimizedCache,
    redis_client: Client,
    ttl: Duration,
}

impl DistributedCache {
    pub fn new(redis_url: &str, local_capacity: u64, ttl: Duration) -> Result<Self, RedisError> {
        Ok(Self {
            local_cache: OptimizedCache::new(local_capacity),
            redis_client: Client::open(redis_url)?,
            ttl,
        })
    }

    pub async fn get(&self, key: &str) -> Result<Option<bool>, RedisError> {
        // Önce local cache'e bak
        if let Some(value) = self.local_cache.get(key) {
            return Ok(Some(value));
        }

        // Redis'ten kontrol et
        let mut conn = self.redis_client.get_connection()?;
        let result: Option<bool> = conn.get(key)?;
        
        // Local cache'e ekle
        if let Some(value) = result {
            self.local_cache.insert(key.to_string(), value);
        }

        Ok(result)
    }

    pub async fn set(&self, key: &str, value: bool) -> Result<(), RedisError> {
        let mut conn = self.redis_client.get_connection()?;
        conn.set_ex(key, value, self.ttl.as_secs() as usize)?;
        self.local_cache.insert(key.to_string(), value);
        Ok(())
    }
}