Skip to content

Commit c81ed92

Browse files
authored
feat: add password and timeout configuration for connecting to redis (#20)
* feat: add password and timeout configuration for connecting to redis Signed-off-by: “seriouszyx” <[email protected]> * docs: add timeout and password configuration example Signed-off-by: “seriouszyx” <[email protected]>
1 parent 52ee85d commit c81ed92

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

.github/workflows/maven-ci.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,22 @@ jobs:
99
redis:
1010
image: redis
1111
ports:
12-
- 6379:6379
12+
- 6378:6378
13+
1314
steps:
1415
- name: Checkout
1516
uses: actions/checkout@v2
1617
with:
1718
fetch-depth: '0'
1819

20+
- name: Set up Redis with password
21+
uses: getong/redis-action@v1
22+
with:
23+
redis version: 'latest'
24+
host port: 6379
25+
container port: 6379
26+
redis password: 'foobared'
27+
1928
- name: Set up JDK 1.8
2029
uses: actions/setup-java@v1
2130
with:

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ Redis Watcher
33

44
[![GitHub Actions](https://github.com/jcasbin/redis-watcher/actions/workflows/maven-ci.yml/badge.svg)](https://github.com/jcasbin/redis-watcher/actions/workflows/maven-ci.yml)
55
![License](https://img.shields.io/github/license/jcasbin/redis-watcher)
6-
[![Javadoc](https://javadoc.io/badge2/org.casbin/redis-watcher/javadoc.svg)](https://javadoc.io/doc/org.casbin/redis-watcher)
6+
[![Javadoc](https://javadoc.io/badge2/org.casbin/jcasbin-redis-watcher/javadoc.svg)](https://javadoc.io/doc/org.casbin/jcasbin-redis-watcher)
77
[![codecov](https://codecov.io/gh/jcasbin/redis-watcher/branch/master/graph/badge.svg?token=ENt9xr4nFg)](https://codecov.io/gh/jcasbin/redis-watcher)
88
[![codebeat badge](https://codebeat.co/badges/8b3da1c4-3a61-4123-a3d4-002b2598a297)](https://codebeat.co/projects/github-com-jcasbin-redis-watcher-master)
9-
[![Maven Central](https://img.shields.io/maven-central/v/org.casbin/redis-watcher.svg)](https://mvnrepository.com/artifact/org.casbin/redis-watcher/latest)
9+
[![Maven Central](https://img.shields.io/maven-central/v/org.casbin/jcasbin-redis-watcher.svg)](https://mvnrepository.com/artifact/org.casbin/jcasbin-redis-watcher/latest)
1010
[![Release](https://img.shields.io/github/release/jcasbin/redis-watcher.svg)](https://github.com/jcasbin/redis-watcher/releases/latest)
1111
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/casbin/lobby)
1212

@@ -40,6 +40,8 @@ if you have two casbin instances A and B
4040
```java
4141
String redisTopic="jcasbin-topic";
4242
RedisWatcher redisWatcher = new RedisWatcher("127.0.0.1",6379, redisTopic);
43+
// Support for connecting to redis with timeout and password
44+
// RedisWatcher redisWatcher = new RedisWatcher("127.0.0.1",6379, redisTopic, 2000, "foobared");
4345

4446
Enforcer enforcer = new SyncedEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");
4547
enforcer.setWatcher(redisWatcher);

src/main/java/org/casbin/watcher/RedisWatcher.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ public class RedisWatcher implements Watcher {
1515
private final String redisChannelName;
1616
private SubThread subThread;
1717

18-
public RedisWatcher(String redisIp, int redisPort,String redisChannelName){
19-
this.jedisPool = new JedisPool(new JedisPoolConfig(), redisIp, redisPort);
18+
public RedisWatcher(String redisIp, int redisPort, String redisChannelName, int timeout, String password) {
19+
this.jedisPool = new JedisPool(new JedisPoolConfig(), redisIp, redisPort, timeout, password);
2020
this.localId = UUID.randomUUID().toString();
21-
this.redisChannelName=redisChannelName;
21+
this.redisChannelName = redisChannelName;
2222
startSub();
2323
}
2424

25+
public RedisWatcher(String redisIp, int redisPort, String redisChannelName) {
26+
this(redisIp, redisPort, redisChannelName, 2000, (String)null);
27+
}
28+
2529
@Override
2630
public void setUpdateCallback(Runnable runnable) {
2731
this.updateCallback=runnable;

src/test/java/org/casbin/test/RedisWatcherTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ public class RedisWatcherTest {
1212
private RedisWatcher redisWatcher;
1313
private final String expect="update msg";
1414

15+
/**
16+
* You should replace the initWatcher() method's content with your own Redis instance.
17+
*/
1518
@Before
1619
public void initWatcher(){
1720
String redisTopic = "jcasbin-topic";
18-
redisWatcher = new RedisWatcher("127.0.0.1",6379, redisTopic);
21+
redisWatcher = new RedisWatcher("127.0.0.1",6379, redisTopic, 2000, "foobared");
1922
Enforcer enforcer = new Enforcer();
2023
enforcer.setWatcher(redisWatcher);
2124
}
@@ -38,4 +41,11 @@ public void testConsumerCallback() throws InterruptedException {
3841
redisWatcher.update();
3942
Thread.sleep(100);
4043
}
44+
45+
@Test
46+
public void testConnectWatcherWithoutPassword() {
47+
String redisTopic = "jcasbin-topic";
48+
RedisWatcher redisWatcherWithoutPassword = new RedisWatcher("127.0.0.1", 6378, redisTopic);
49+
Assert.assertNotNull(redisWatcherWithoutPassword);
50+
}
4151
}

0 commit comments

Comments
 (0)