Skip to content

Commit d1b6524

Browse files
authored
feat: add TxWatcher (#99)
* support spring tx * Revert "support spring tx" This reverts commit 94dd068. * feat: add TxWatcher (#98) * fix: add the new usage to README.md
1 parent a5221b0 commit d1b6524

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ casbin:
7777
#CasbinWatcher notification mode, defaults to use Redis for notification synchronization, temporarily only supports Redis
7878
#After opening Watcher, you need to manually add spring-boot-starter-data-redis dependency.
7979
watcherType: redis
80+
#Watcher to support spring tx, the default is not enable.
81+
#If the mechanism is enabled, When updating policy in a spring transaction, watcher will trigger the update after commit
82+
watcherTxSupport: false
8083
exception:
8184
... See Schedule A for exception settings.
8285
```

src/main/java/org/casbin/spring/boot/autoconfigure/CasbinRedisWatcherAutoConfiguration.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
1313
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
1414
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
15+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
1516
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
1617
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
1718
import org.springframework.boot.context.properties.EnableConfigurationProperties;
1819
import org.springframework.context.annotation.Bean;
1920
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.context.annotation.Primary;
2022
import org.springframework.data.redis.core.RedisTemplate;
2123

2224
/**
@@ -71,4 +73,15 @@ public Watcher lettuceRedisWatcher(RedisProperties redisProperties, CasbinProper
7173
throw new CasbinWatcherLettuceTypeUnsupportedException("Unsupported watcher type!");
7274
}
7375
}
74-
}
76+
77+
@Bean
78+
@Primary
79+
@ConditionalOnBean(Watcher.class)
80+
@ConditionalOnProperty(value = "casbin.watcher-tx-support", havingValue = "true")
81+
public Watcher txWatcher(Watcher watcher, Enforcer enforcer) {
82+
TxWatcher txWatcher = new TxWatcher(watcher);
83+
enforcer.setWatcher(txWatcher);
84+
logger.info("TxWatcher proxy watcher: {}", watcher.getClass().getName());
85+
return txWatcher;
86+
}
87+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.casbin.spring.boot.autoconfigure;
2+
3+
import org.casbin.jcasbin.persist.Watcher;
4+
import org.springframework.transaction.support.TransactionSynchronization;
5+
6+
import java.util.function.Consumer;
7+
8+
import static org.springframework.transaction.support.TransactionSynchronizationManager.isActualTransactionActive;
9+
import static org.springframework.transaction.support.TransactionSynchronizationManager.registerSynchronization;
10+
11+
public class TxWatcher implements Watcher {
12+
private final Watcher watcher;
13+
14+
public TxWatcher(Watcher watcher) {
15+
this.watcher = watcher;
16+
}
17+
18+
@Override
19+
public void setUpdateCallback(Runnable runnable) {
20+
this.watcher.setUpdateCallback(runnable);
21+
}
22+
23+
@Override
24+
public void setUpdateCallback(Consumer<String> consumer) {
25+
this.watcher.setUpdateCallback(consumer);
26+
}
27+
28+
@Override
29+
public void update() {
30+
if (isActualTransactionActive()) {
31+
registerSynchronization(new TransactionSynchronization() {
32+
@Override
33+
public void afterCommit() {
34+
watcher.update();
35+
}
36+
});
37+
} else {
38+
watcher.update();
39+
}
40+
}
41+
}

src/main/java/org/casbin/spring/boot/autoconfigure/properties/CasbinProperties.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ public class CasbinProperties {
7272
*/
7373
private CasbinWatcherLettuceRedisType watcherLettuceRedisType = CasbinWatcherLettuceRedisType.NONE;
7474

75+
/**
76+
* Use {@link org.casbin.spring.boot.autoconfigure.TxWatcher} proxy, update after the transaction is committed
77+
*/
78+
private boolean watcherTxSupport = false;
79+
7580
public String getModelContext() {
7681
String text = FileUtils.getFileAsText(model);
7782
if (text == null) {
@@ -193,4 +198,12 @@ public CasbinWatcherLettuceRedisType getWatcherLettuceRedisType() {
193198
public void setWatcherLettuceRedisType(CasbinWatcherLettuceRedisType watcherLettuceRedisType) {
194199
this.watcherLettuceRedisType = watcherLettuceRedisType;
195200
}
196-
}
201+
202+
public boolean isWatcherTxSupport() {
203+
return watcherTxSupport;
204+
}
205+
206+
public void setWatcherTxSupport(boolean watcherTxSupport) {
207+
this.watcherTxSupport = watcherTxSupport;
208+
}
209+
}

0 commit comments

Comments
 (0)