Skip to content

Commit

Permalink
feat: add missing code to SyncedCachedEnforcer and fix cache not used…
Browse files Browse the repository at this point in the history
… issue (#444)
  • Loading branch information
sukidayou authored Dec 1, 2024
1 parent 8dd4940 commit 769694f
Showing 1 changed file with 55 additions and 3 deletions.
58 changes: 55 additions & 3 deletions src/main/java/org/casbin/jcasbin/main/SyncedCachedEnforcer.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ public void enableCache(boolean enable) {
* @param rvals Parameters for the enforcement check.
* @return The result of the enforcement check.
*/
@Override
public boolean enforce(Object... rvals) {
if (enableCache.get()) {
if (!enableCache.get()) {
return super.enforce(rvals);
}

Expand All @@ -143,6 +144,7 @@ public boolean enforce(Object... rvals) {
/**
* Loads the policy, clearing the cache if enabled.
*/
@Override
public void loadPolicy() {
if(enableCache == null || !enableCache.get()){
super.loadPolicy();
Expand All @@ -160,6 +162,7 @@ public void loadPolicy() {
* @param params Policy parameters.
* @return Whether the addition was successful.
*/
@Override
public boolean addPolicy(String... params) {
if (!checkOneAndRemoveCache(params)) {
return false;
Expand All @@ -173,19 +176,35 @@ public boolean addPolicy(String... params) {
* @param rules Policy rules.
* @return Whether the addition was successful.
*/
@Override
public boolean addPolicies(List<List<String>> rules) {
if (!checkManyAndRemoveCache(rules)) {
return false;
}
return super.addPolicies(rules);
}

/**
* Adds multiple policies while checking and removing the cache.
*
* @param rules Policy rules.
* @return Whether the addition was successful.
*/
@Override
public boolean addPolicies(String[][] rules) {
if (!checkManyAndRemoveCache(rules)) {
return false;
}
return super.addPolicies(rules);
}

/**
* Removes a single policy while checking and removing the cache.
*
* @param params Policy parameters.
* @return Whether the removal was successful.
*/
@Override
public boolean removePolicy(String... params) {
if (!checkOneAndRemoveCache(params)) {
return false;
Expand All @@ -199,13 +218,28 @@ public boolean removePolicy(String... params) {
* @param rules Policy rules.
* @return Whether the removal was successful.
*/
@Override
public boolean removePolicies(List<List<String>>rules) {
if (!checkManyAndRemoveCache(rules)) {
return false;
}
return super.removePolicies(rules);
}

/**
* Removes multiple policies while checking and removing the cache.
*
* @param rules Policy rules.
* @return Whether the removal was successful.
*/
@Override
public boolean removePolicies(String[][] rules) {
if (!checkManyAndRemoveCache(rules)) {
return false;
}
return super.removePolicies(rules);
}

/**
* Retrieves a cached result based on the given key.
*
Expand Down Expand Up @@ -306,7 +340,7 @@ public void invalidateCache() {
*/
private boolean checkOneAndRemoveCache(String... params) {
if (enableCache.get()) {
String key = getKey((Object) params);
String key = getKey((Object []) params);
if (key != null) {
cache.delete(key);
}
Expand All @@ -323,7 +357,25 @@ private boolean checkOneAndRemoveCache(String... params) {
private boolean checkManyAndRemoveCache(List<List<String>> rules) {
if (!rules.isEmpty() && enableCache.get()) {
for (List<String> rule : rules) {
String key = getKey(rule);
String key = getKey(rule.toArray());
if (key != null) {
cache.delete(key);
}
}
}
return true;
}

/**
* Checks and removes cache for multiple policies.
*
* @param rules Policy rules.
* @return Whether the check was successful.
*/
private boolean checkManyAndRemoveCache(String[][] rules) {
if (rules != null && enableCache.get()) {
for (String[] rule : rules) {
String key = getKey((Object[]) rule);
if (key != null) {
cache.delete(key);
}
Expand Down

0 comments on commit 769694f

Please sign in to comment.