redis_watcher/
lib.rs

1// Copyright 2025 The Casbin Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Redis Watcher for Casbin-RS
16//!
17//! This library provides a Redis-based watcher implementation for Casbin-RS,
18//! allowing policy synchronization across multiple instances through Redis pub/sub.
19//!
20//! # Examples
21//!
22//! ## Standalone Redis
23//!
24//! ```rust,no_run
25//! use redis_watcher::{RedisWatcher, WatcherOptions};
26//! use casbin::prelude::*;
27//!
28//! fn main() -> redis_watcher::Result<()> {
29//!     let options = WatcherOptions::default()
30//!         .with_channel("/casbin-policy-updates".to_string())
31//!         .with_ignore_self(true);
32//!     
33//!     let mut watcher = RedisWatcher::new("redis://127.0.0.1:6379", options)?;
34//!     
35//!     // Set callback to reload policies when notified
36//!     watcher.set_update_callback(Box::new(|msg: String| {
37//!         println!("Received policy update: {}", msg);
38//!         // Reload your enforcer here
39//!     }));
40//!     
41//!     // Use watcher with enforcer
42//!     // let mut enforcer = Enforcer::new("model.conf", "policy.csv").await.unwrap();
43//!     // enforcer.set_watcher(Box::new(watcher));
44//!     
45//!     Ok(())
46//! }
47//! ```
48//!
49//! ## Redis Cluster
50//!
51//! ```rust,no_run
52//! use redis_watcher::{RedisWatcher, WatcherOptions};
53//! use casbin::prelude::*;
54//!
55//! fn main() -> redis_watcher::Result<()> {
56//!     let options = WatcherOptions::default()
57//!         .with_channel("/casbin-policy-updates".to_string())
58//!         .with_ignore_self(true);
59//!     
60//!     // Connect to Redis Cluster with multiple nodes
61//!     let cluster_urls = "redis://127.0.0.1:7000,redis://127.0.0.1:7001,redis://127.0.0.1:7002";
62//!     let mut watcher = RedisWatcher::new_cluster(cluster_urls, options)?;
63//!     
64//!     // Set callback to reload policies when notified
65//!     watcher.set_update_callback(Box::new(|msg: String| {
66//!         println!("Received policy update from cluster: {}", msg);
67//!         // Reload your enforcer here
68//!     }));
69//!     
70//!     Ok(())
71//! }
72//! ```
73
74mod options;
75mod watcher;
76
77#[cfg(test)]
78mod watcher_test;
79
80pub use options::WatcherOptions;
81pub use watcher::RedisWatcher;
82
83/// Re-export for convenience
84pub use watcher::{Message, Result, UpdateType, WatcherError};