Skip to content

Commit e5148c6

Browse files
authored
feat: fix CachedEnforcer bug that false result was not cached (#457)
1 parent 97a3bdd commit e5148c6

File tree

5 files changed

+16
-16
lines changed

5 files changed

+16
-16
lines changed

src/main/java/org/casbin/jcasbin/main/CachedEnforcer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ public boolean enforce(Object... rvals) {
133133
return super.enforce(rvals);
134134
}
135135

136-
boolean cachedResult = getCachedResult(key);
137-
if (cachedResult) {
136+
Boolean cachedResult = getCachedResult(key);
137+
if (cachedResult != null) {
138138
return cachedResult;
139139
}
140140

@@ -217,7 +217,7 @@ public boolean removePolicies(String[][] rules) {
217217
* @param key The cache key.
218218
* @return The cached result, or null if not found.
219219
*/
220-
private boolean getCachedResult(String key) {
220+
private Boolean getCachedResult(String key) {
221221
READ_WRITE_LOCK.readLock().lock();
222222
try {
223223
return cache.get(key);

src/main/java/org/casbin/jcasbin/main/SyncedCachedEnforcer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public boolean enforce(Object... rvals) {
132132
}
133133

134134
Boolean cachedResult = getCachedResult(key);
135-
if (cachedResult) {
135+
if (cachedResult != null) {
136136
return cachedResult;
137137
}
138138

src/main/java/org/casbin/jcasbin/persist/cache/Cache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public interface Cache {
3636
* @param key the key to retrieve
3737
* @return an Optional containing the boolean value if present, otherwise Optional.empty()
3838
*/
39-
boolean get(String key);
39+
Boolean get(String key);
4040

4141
/**
4242
* Delete removes the specific key from the cache.

src/main/java/org/casbin/jcasbin/persist/cache/DefaultCache.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ public boolean set(String key, boolean value, Object... extra) {
7979
* @throws CasbinCacheException If the key does not exist in the cache.
8080
* @return The value corresponding to the key.
8181
*/
82-
public boolean get(String key) {
82+
public Boolean get(String key) {
8383
CacheItem item = cache.get(key);
8484
if (item == null) {
85-
return false;
85+
return null;
8686
}
8787
if (item.isExpired()) {
8888
cache.remove(key);
89-
return false;
89+
return null;
9090
}
9191
return item.getValue();
9292
}

src/test/java/org/casbin/jcasbin/main/CachedEnforcerUnitTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class CachedEnforcerUnitTest {
2929
private Cache cache;
3030

3131
private void testEnforceCache(String sub, String obj, String act, boolean expectedRes) throws Exception {
32-
boolean actualRes = cachedEnforcer.enforce(sub, obj, act);
32+
Boolean actualRes = cachedEnforcer.enforce(sub, obj, act);
3333
assertEquals(String.format("%s, %s, %s: %s, supposed to be %s", sub, obj, act, actualRes, expectedRes), expectedRes, actualRes);
3434
}
3535

@@ -87,17 +87,17 @@ public void testCache() throws Exception {
8787
public void testInvalidateCache() throws Exception {
8888
cachedEnforcer = new CachedEnforcer("examples/basic_model.conf", "examples/basic_policy.csv", false);
8989

90-
boolean cacheKey = cachedEnforcer.getCache().get(cachedEnforcer.getCacheKey("alice", "data1", "read"));
91-
assertFalse(String.format("alice, data1, read: %s, supposed to be %s", cacheKey, false), cacheKey);
90+
Boolean cacheKey = cachedEnforcer.getCache().get(cachedEnforcer.getCacheKey("alice", "data1", "read"));
91+
assertNull(String.format("alice, data1, read: %s, supposed to be %s", cacheKey, null), cacheKey);
9292

93-
boolean actualRes = cachedEnforcer.enforce("alice", "data1", "read");
93+
Boolean actualRes = cachedEnforcer.enforce("alice", "data1", "read");
9494
cacheKey = cachedEnforcer.getCache().get(cachedEnforcer.getCacheKey("alice", "data1", "read"));
9595
assertTrue(String.format("alice, data1, read: %s, supposed to be %s", actualRes, true), actualRes);
9696
assertTrue(String.format("alice, data1, read: %s, supposed to be %s", cacheKey, true), cacheKey);
9797

9898
cachedEnforcer.invalidateCache();
9999
cacheKey = cachedEnforcer.getCache().get(cachedEnforcer.getCacheKey("alice", "data1", "read"));
100-
assertFalse(String.format("alice, data1, read: %s, supposed to be %s", cacheKey, false), cacheKey);
100+
assertNull(String.format("alice, data1, read: %s, supposed to be %s", cacheKey, null), cacheKey);
101101

102102
}
103103

@@ -132,14 +132,14 @@ public void testCacheExpiration() throws Exception {
132132
// Test cache expiration
133133
cache.set(getKey("alice", "data1", "read"),true,Duration.ofMillis(10));
134134
cachedEnforcer.setCache(cache);
135-
boolean cacheKey = cachedEnforcer.getCache().get(cachedEnforcer.getCacheKey("alice", "data1", "read"));
135+
Boolean cacheKey = cachedEnforcer.getCache().get(cachedEnforcer.getCacheKey("alice", "data1", "read"));
136136
assertTrue(String.format("alice, data1, read: %s, supposed to be %s", cacheKey, true), cacheKey);
137137

138138
// Wait for the cache to expire
139139
Thread.sleep(15);
140140

141141
cacheKey = cachedEnforcer.getCache().get(cachedEnforcer.getCacheKey("alice", "data1", "read"));
142-
assertFalse(String.format("alice, data1, read: %s, supposed to be %s", cacheKey, false), cacheKey);
142+
assertNull(String.format("alice, data1, read: %s, supposed to be %s", cacheKey, null), cacheKey);
143143

144144
// Replace cache during test run
145145
cache.clear();
@@ -152,7 +152,7 @@ public void testCacheExpiration() throws Exception {
152152
cache.set(getKey("jack", "data1", "write"),true,Duration.ofMillis(1000));
153153
cachedEnforcer.setCache(cache);
154154
cacheKey = cachedEnforcer.getCache().get(cachedEnforcer.getCacheKey("bob", "data1", "read"));
155-
assertFalse(String.format("bob, data1, read: %s, supposed to be %s", cacheKey, false), cacheKey);
155+
assertNull(String.format("bob, data1, read: %s, supposed to be %s", cacheKey, null), cacheKey);
156156

157157
cacheKey = cachedEnforcer.getCache().get(cachedEnforcer.getCacheKey("jack", "data1", "write"));
158158
assertTrue(String.format("jack, data1, write: %s, supposed to be %s", cacheKey, true), cacheKey);

0 commit comments

Comments
 (0)