https://github.com/jcasbin/redis-watcher
Redis watcher for jCasbin
https://github.com/jcasbin/redis-watcher
abac acl auth authorization authz casbin java jcasbin rbac redis watcher
Last synced: 6 months ago
JSON representation
Redis watcher for jCasbin
- Host: GitHub
- URL: https://github.com/jcasbin/redis-watcher
- Owner: jcasbin
- License: apache-2.0
- Created: 2021-04-12T10:34:18.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2024-09-27T16:25:32.000Z (7 months ago)
- Last Synced: 2024-11-09T20:08:22.514Z (6 months ago)
- Topics: abac, acl, auth, authorization, authz, casbin, java, jcasbin, rbac, redis, watcher
- Language: Java
- Homepage: https://github.com/casbin/jcasbin
- Size: 35.2 KB
- Stars: 4
- Watchers: 3
- Forks: 12
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Redis Watcher
---[](https://github.com/jcasbin/redis-watcher/actions/workflows/maven-ci.yml)

[](https://javadoc.io/doc/org.casbin/jcasbin-redis-watcher)
[](https://codecov.io/gh/jcasbin/redis-watcher)
[](https://codebeat.co/projects/github-com-jcasbin-redis-watcher-master)
[](https://mvnrepository.com/artifact/org.casbin/jcasbin-redis-watcher/latest)
[](https://github.com/jcasbin/redis-watcher/releases/latest)
[](https://discord.gg/S5UjpzGZjN)Redis Watcher is a [Redis](http://redis.io) watcher for [jCasbin](https://github.com/casbin/jcasbin).
## Installation
**For Maven**
```
org.casbin
jcasbin-redis-watcher
1.0-SNAPSHOT
```## Publish and Subscribe
Please learn redis [publish and subscribe](https://redisbook.readthedocs.io/en/latest/feature/pubsub.html) first.
Creating a ``redis-watcher`` will create a new thread for subscribing to the ``topic (Channel)`` of redis. Multiple instances create multiple watchers and corresponding threads subscribe to the same topic. When one of the instances executes the action of the update policy (such as ``e.addPolicy``, ``e.removePolicy`` ...), it will send a message to the topic, and then other instances that have subscribed to the topic will receive the notification and execute the ``updateCallback`` method. The default ``updateCallback`` is to call ``e.LoadPolicy``, which is the reload policy. (When you call ``e.setWatcher(redisWatcher)``, it will set default updateCallback)
## Simple Example
if you have two casbin instances A and B
**A:** **Producer**
```java
String redisTopic="jcasbin-topic";
RedisWatcher redisWatcher = new RedisWatcher("127.0.0.1",6379, redisTopic);
// Support for connecting to redis with timeout and password
// RedisWatcher redisWatcher = new RedisWatcher("127.0.0.1",6379, redisTopic, 2000, "foobared");Enforcer enforcer = new SyncedEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");
enforcer.setWatcher(redisWatcher);// The following code is not necessary and generally does not need to be written unless you understand what you want to do
/*
Runnable updateCallback = ()->{
// Custom behavior
};redisWatcher.setUpdateCallback(updateCallback);
*/// Modify policy, it will notify B
enforcer.addPolicy(...);
```**B:** **Consumer**
````Java
String redisTopic="jcasbin-topic";
RedisWatcher redisWatcher = new RedisWatcher("127.0.0.1",6379, redisTopic);Enforcer enforcer = new SyncedEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");
enforcer.setWatcher(redisWatcher);
// B set watcher and subscribe redisTopic, then it will receive the notification of A, and then call LoadPolicy to reload policy
````## Getting Help
- [jCasbin](https://github.com/casbin/jCasbin)
- [jedis](https://github.com/redis/jedis)## License
This project is under Apache 2.0 License. See the [LICENSE](https://github.com/jcasbin/redis-watcher/blob/master/LICENSE) file for the full license text.