From 769694fac2e8250f7ef121778f7eb6c3be1c48f0 Mon Sep 17 00:00:00 2001 From: sukidayou <77531556+sukidayou@users.noreply.github.com> Date: Sun, 1 Dec 2024 11:15:00 +0800 Subject: [PATCH] feat: add missing code to SyncedCachedEnforcer and fix cache not used issue (#444) --- .../jcasbin/main/SyncedCachedEnforcer.java | 58 ++++++++++++++++++- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/casbin/jcasbin/main/SyncedCachedEnforcer.java b/src/main/java/org/casbin/jcasbin/main/SyncedCachedEnforcer.java index 6de7b6d6..a31b4433 100644 --- a/src/main/java/org/casbin/jcasbin/main/SyncedCachedEnforcer.java +++ b/src/main/java/org/casbin/jcasbin/main/SyncedCachedEnforcer.java @@ -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); } @@ -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(); @@ -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; @@ -173,6 +176,7 @@ public boolean addPolicy(String... params) { * @param rules Policy rules. * @return Whether the addition was successful. */ + @Override public boolean addPolicies(List> rules) { if (!checkManyAndRemoveCache(rules)) { return false; @@ -180,12 +184,27 @@ public boolean addPolicies(List> rules) { 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; @@ -199,6 +218,7 @@ public boolean removePolicy(String... params) { * @param rules Policy rules. * @return Whether the removal was successful. */ + @Override public boolean removePolicies(List>rules) { if (!checkManyAndRemoveCache(rules)) { return false; @@ -206,6 +226,20 @@ public boolean removePolicies(List>rules) { 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. * @@ -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); } @@ -323,7 +357,25 @@ private boolean checkOneAndRemoveCache(String... params) { private boolean checkManyAndRemoveCache(List> rules) { if (!rules.isEmpty() && enableCache.get()) { for (List 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); }