https://github.com/casbin/redis-watcher
Redis WatcherEx for Casbin
https://github.com/casbin/redis-watcher
abac access-control acl auth authorization authz casbin go golang rbac redis watcher watcherex
Last synced: 3 months ago
JSON representation
Redis WatcherEx for Casbin
- Host: GitHub
- URL: https://github.com/casbin/redis-watcher
- Owner: casbin
- License: apache-2.0
- Created: 2021-04-14T14:18:14.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2023-07-20T01:09:37.000Z (almost 2 years ago)
- Last Synced: 2025-04-19T17:14:38.204Z (3 months ago)
- Topics: abac, access-control, acl, auth, authorization, authz, casbin, go, golang, rbac, redis, watcher, watcherex
- Language: Go
- Homepage: https://github.com/casbin/casbin
- Size: 41 KB
- Stars: 40
- Watchers: 1
- Forks: 38
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Redis Watcher
---> For Go 1.17+, use v2.4.0+ ,
> For Go 1.16 and below, stay on v2.3.0.[](https://github.com/casbin/redis-watcher/actions/workflows/ci.yml)
[](https://goreportcard.com/report/github.com/casbin/redis-watcher)
[](https://coveralls.io/github/casbin/redis-watcher?branch=master)
[](https://pkg.go.dev/github.com/casbin/redis-watcher/v2)
[](https://github.com/casbin/redis-watcher/releases/latest)Redis Watcher is a [Redis](http://redis.io) watcher for [Casbin](https://github.com/casbin/casbin).
## Installation
go get github.com/casbin/redis-watcher/v2
## Simple Example
```go
package mainimport (
"log""github.com/casbin/casbin/v2"
rediswatcher "github.com/casbin/redis-watcher/v2"
"github.com/redis/go-redis/v9"
)func updateCallback(msg string) {
log.Println(msg)
}func main() {
// Initialize the watcher.
// Use the Redis host as parameter.
w, _ := rediswatcher.NewWatcher("localhost:6379", rediswatcher.WatcherOptions{
Options: redis.Options{
Network: "tcp",
Password: "",
},
Channel: "/casbin",
// Only exists in test, generally be true
IgnoreSelf: false,
})// Or initialize the watcher in redis cluster.
// w, _ := rediswatcher.NewWatcherWithCluster("localhost:6379,localhost:6379,localhost:6379", rediswatcher.WatcherOptions{
// ClusterOptions: redis.ClusterOptions{
// Password: "",
// },
// Channel: "/casbin",
// IgnoreSelf: false,
// })// Initialize the enforcer.
e, _ := casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")// Set the watcher for the enforcer.
_ = e.SetWatcher(w)// Set callback to local example
_ = w.SetUpdateCallback(updateCallback)
// Or use the default callback
// _ = w.SetUpdateCallback(rediswatcher.DefaultUpdateCallback(e))// Update the policy to test the effect.
// You should see "[casbin rules updated]" in the log.
_ = e.SavePolicy()
// Only exists in test
fmt.Scanln()
}```
## Getting Help
- [Casbin](https://github.com/casbin/casbin)
- [go-redis](https://github.com/go-redis/redis)## License
This project is under Apache 2.0 License. See the [LICENSE](LICENSE) file for the full license text.