Redis Watcher
test tag
Redis Watcher is a Redis watcher for Casbin-RS.
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
= { = "2.13", = ["watcher"] }
= { = "1.0", = ["rt-multi-thread", "macros", "time"] }
= { = "0.32", = ["tokio-comp", "cluster-async", "aio"] }
Note: The watcher feature is required for Casbin to enable watcher functionality. For Redis cluster support, include the cluster-async feature.
Simple Example
use ;
use ;
Key Features:
- Automatic Subscription: The watcher starts listening for updates automatically when you set the callback
- Thread-Safe: Built with Rust's safety guarantees and proper synchronization
- Synchronous API: Simple blocking API that handles async operations internally
- Casbin Integration: Implements the
Watchertrait for seamless integration with Casbin enforcers
Cluster Example
use ;
use ;
Cluster Features:
- High Availability: Connects to multiple Redis cluster nodes
- Automatic Failover: Redis cluster handles node failures automatically
- Scalability: Distributes load across cluster nodes
- Pub/Sub Support: Uses dedicated connection for pub/sub operations in cluster mode
Configuration
WatcherOptions
The WatcherOptions struct provides configuration for the Redis watcher:
use WatcherOptions;
let options = default
.with_channel // Redis channel name
.with_ignore_self // Ignore self-generated updates
.with_local_id; // Unique identifier for this instance
Options Explained:
channel: Redis pub/sub channel name for policy updates (default:"/casbin")ignore_self: Whentrue, the watcher ignores messages it published itself, preventing circular updates (default:false)local_id: Unique identifier for this watcher instance, automatically generated using UUID v4 if not specified
Best Practices:
- Set
ignore_selftotruein production to avoid processing your own updates - Use a descriptive
local_idfor easier debugging in multi-instance deployments - Choose a channel name that doesn't conflict with other Redis applications
Update Types
The watcher supports various policy update types through the UpdateType enum, which corresponds to different Casbin operations:
Message Structure:
Each update is published as a JSON message with the following structure:
Integration with Casbin:
The watcher automatically converts Casbin's EventData to these message types when you call watcher.update(event_data). This ensures consistent synchronization across all instances.
Getting Help
Documentation
- API Documentation: docs.rs/redis-watcher
- Casbin-RS Documentation: Casbin-RS GitHub
- Redis Client: redis-rs Documentation
- Async Runtime: Tokio Documentation
License
This project is under Apache 2.0 License. See the LICENSE file for the full license text.