Redis Watcher
Redis Watcher is a Redis watcher for Casbin-RS.
Installation
Add this to your Cargo.toml
:
[]
= "1.0.0"
= { = "2.13.0", = ["watcher"] }
= { = "1.0", = ["rt-multi-thread", "macros", "time"] }
= { = "0.32.6", = ["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 ;
Cluster Example
use ;
use ;
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_self
totrue
in production to avoid processing your own updates - Use a descriptive
local_id
for 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.