Crate redis_watcher

Crate redis_watcher 

Source
Expand description

Redis Watcher for Casbin-RS

This library provides a Redis-based watcher implementation for Casbin-RS, allowing policy synchronization across multiple instances through Redis pub/sub.

§Examples

§Standalone Redis

use redis_watcher::{RedisWatcher, WatcherOptions};
use casbin::prelude::*;

fn main() -> redis_watcher::Result<()> {
    let options = WatcherOptions::default()
        .with_channel("/casbin-policy-updates".to_string())
        .with_ignore_self(true);
     
    let mut watcher = RedisWatcher::new("redis://127.0.0.1:6379", options)?;
     
    // Set callback to reload policies when notified
    watcher.set_update_callback(Box::new(|msg: String| {
        println!("Received policy update: {}", msg);
        // Reload your enforcer here
    }));
     
    // Use watcher with enforcer
    // let mut enforcer = Enforcer::new("model.conf", "policy.csv").await.unwrap();
    // enforcer.set_watcher(Box::new(watcher));
     
    Ok(())
}

§Redis Cluster

use redis_watcher::{RedisWatcher, WatcherOptions};
use casbin::prelude::*;

fn main() -> redis_watcher::Result<()> {
    let options = WatcherOptions::default()
        .with_channel("/casbin-policy-updates".to_string())
        .with_ignore_self(true);
     
    // Connect to Redis Cluster with multiple nodes
    let cluster_urls = "redis://127.0.0.1:7000,redis://127.0.0.1:7001,redis://127.0.0.1:7002";
    let mut watcher = RedisWatcher::new_cluster(cluster_urls, options)?;
     
    // Set callback to reload policies when notified
    watcher.set_update_callback(Box::new(|msg: String| {
        println!("Received policy update from cluster: {}", msg);
        // Reload your enforcer here
    }));
     
    Ok(())
}

Structs§

Message
Re-export for convenience Message structure for Redis pub/sub communication
RedisWatcher
WatcherOptions
Configuration options for the Redis watcher This mirrors the Go version’s WatcherOptions structure

Enums§

UpdateType
Re-export for convenience Message types for communication between watcher instances
WatcherError
Re-export for convenience

Type Aliases§

Result
Re-export for convenience