Package org.casbin.jcasbin.main
Class SyncedCachedEnforcer
- java.lang.Object
-
- org.casbin.jcasbin.main.CoreEnforcer
-
- org.casbin.jcasbin.main.InternalEnforcer
-
- org.casbin.jcasbin.main.ManagementEnforcer
-
- org.casbin.jcasbin.main.Enforcer
-
- org.casbin.jcasbin.main.SyncedEnforcer
-
- org.casbin.jcasbin.main.SyncedCachedEnforcer
-
public class SyncedCachedEnforcer extends SyncedEnforcer
-
-
Field Summary
Fields Modifier and Type Field Description private Cache
cache
private java.util.concurrent.atomic.AtomicBoolean
enableCache
private java.time.Duration
expireTime
private static java.util.concurrent.locks.ReadWriteLock
READ_WRITE_LOCK
-
Fields inherited from class org.casbin.jcasbin.main.CoreEnforcer
acceptJsonRequest, adapter, autoBuildRoleLinks, autoNotifyDispatcher, autoNotifyWatcher, autoSave, condRmMap, dispatcher, fm, model, modelPath, rmMap, watcher
-
-
Constructor Summary
Constructors Constructor Description SyncedCachedEnforcer()
Default constructor.SyncedCachedEnforcer(java.lang.String modelPath)
Initializes an enforcer with a model file.SyncedCachedEnforcer(java.lang.String modelPath, java.lang.String policyFile)
Initializes an enforcer with a model file and a policy file.SyncedCachedEnforcer(java.lang.String modelPath, java.lang.String policyFile, boolean enableLog)
Initializes an enforcer with a model file, a policy file, and a logging flag.SyncedCachedEnforcer(java.lang.String modelPath, Adapter adapter)
Initializes an enforcer with a model file and a database adapter.SyncedCachedEnforcer(Model m)
Initializes an enforcer with a model.SyncedCachedEnforcer(Model m, Adapter adapter)
Initializes an enforcer with a model and a database adapter.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
addPolicies(java.lang.String[][] rules)
Adds multiple policies while checking and removing the cache.boolean
addPolicies(java.util.List<java.util.List<java.lang.String>> rules)
Adds multiple policies while checking and removing the cache.boolean
addPolicy(java.lang.String... params)
Adds a single policy while checking and removing the cache.private boolean
checkManyAndRemoveCache(java.lang.String[][] rules)
Checks and removes cache for multiple policies.private boolean
checkManyAndRemoveCache(java.util.List<java.util.List<java.lang.String>> rules)
Checks and removes cache for multiple policies.private boolean
checkOneAndRemoveCache(java.lang.String... params)
Checks and removes cache for a single policy.void
enableCache(boolean enable)
Enables or disables caching.boolean
enforce(java.lang.Object... rvals)
Performs an enforcement check based on given parameters, using the cache.private java.lang.Boolean
getCachedResult(java.lang.String key)
Retrieves a cached result based on the given key.java.lang.String
getCacheKey(java.lang.Object... params)
Retrieves a cache key from the given parameters.private java.lang.String
getKey(java.lang.Object... params)
Generates a key based on the given parameters.void
invalidateCache()
Invalidates the cache by clearing it.void
loadPolicy()
Loads the policy, clearing the cache if enabled.boolean
removePolicies(java.lang.String[][] rules)
Removes multiple policies while checking and removing the cache.boolean
removePolicies(java.util.List<java.util.List<java.lang.String>> rules)
Removes multiple policies while checking and removing the cache.boolean
removePolicy(java.lang.String... params)
Removes a single policy while checking and removing the cache.void
setCache(Cache cache)
Sets a custom cache.private void
setCachedResult(java.lang.String key, boolean result, java.lang.Object... extra)
Sets the cached result.void
setExpireTime(java.time.Duration expireTime)
Sets the cache expiration time.-
Methods inherited from class org.casbin.jcasbin.main.SyncedEnforcer
addGroupingPolicies, addGroupingPolicies, addGroupingPolicy, addGroupingPolicy, addNamedGroupingPolicies, addNamedGroupingPolicies, addNamedGroupingPolicy, addNamedGroupingPolicy, addNamedPolicies, addNamedPoliciesEx, addNamedPolicy, addNamedPolicy, addPermissionForUser, addPermissionForUser, addPolicy, addRoleForUser, addRoleForUserInDomain, batchEnforce, batchEnforceWithMatcher, buildRoleLinks, clearPolicy, deletePermission, deletePermission, deletePermissionForUser, deletePermissionForUser, deletePermissionsForUser, deleteRole, deleteRoleForUser, deleteRoleForUserInDomain, deleteRolesForUser, deleteUser, enforceEx, enforceExWithMatcher, enforceWithMatcher, getAllActions, getAllNamedActions, getAllNamedObjects, getAllNamedRoles, getAllObjects, getAllRoles, getAllSubjects, getFilteredGroupingPolicy, getFilteredNamedGroupingPolicy, getFilteredNamedPolicy, getFilteredPolicy, getGroupingPolicy, getImplicitPermissionsForUser, getImplicitRolesForUser, getNamedGroupingPolicy, getNamedImplicitPermissionsForUser, getNamedPermissionsForUser, getNamedPolicy, getPermissionsForUser, getPermissionsForUserInDomain, getPolicy, getRolesForUser, getRolesForUserInDomain, getUsersForRole, getUsersForRoleInDomain, hasGroupingPolicy, hasGroupingPolicy, hasNamedGroupingPolicy, hasNamedGroupingPolicy, hasNamedPolicy, hasNamedPolicy, hasPermissionForUser, hasPermissionForUser, hasPolicy, hasPolicy, hasRoleForUser, isAutoLoadingRunning, loadFilteredPolicy, removeFilteredGroupingPolicy, removeFilteredNamedGroupingPolicy, removeFilteredNamedPolicy, removeFilteredPolicy, removeGroupingPolicies, removeGroupingPolicies, removeGroupingPolicy, removeGroupingPolicy, removeNamedGroupingPolicies, removeNamedGroupingPolicies, removeNamedGroupingPolicy, removeNamedGroupingPolicy, removeNamedPolicies, removeNamedPolicy, removeNamedPolicy, removePolicy, savePolicy, setWatcher, startAutoLoadPolicy, stopAutoLoadPolicy, updateGroupingPolicy, updateNamedGroupingPolicy, updateNamedPolicy, updatePolicy
-
Methods inherited from class org.casbin.jcasbin.main.Enforcer
getImplicitPermissionsForUserInDomain, getImplicitUsersForRole, updatePermissionForUser
-
Methods inherited from class org.casbin.jcasbin.main.ManagementEnforcer
addFunction, getAllNamedSubjects, getPermittedActions
-
Methods inherited from class org.casbin.jcasbin.main.InternalEnforcer
addPolicies, addPolicy, buildIncrementalRoleLinks, getDomainIndex, removeFilteredPolicy, removePolicies, removePolicy, updatePolicy
-
Methods inherited from class org.casbin.jcasbin.main.CoreEnforcer
addNamedDomainLinkConditionFunc, addNamedDomainMatchingFunc, addNamedLinkConditionFunc, addNamedMatchingFunc, buildConditionalRoleLinks, enableAcceptJsonRequest, enableAutoBuildRoleLinks, enableAutoSave, enableEnforce, enableLog, getAdapter, getAviatorEval, getModel, getNamedRoleManager, getRmMap, getRoleManager, initialize, isAutoNotifyDispatcher, isAutoNotifyWatcher, isFiltered, loadModel, mustUseDispatcher, newModel, newModel, newModel, resetExpressionEvaluator, setAdapter, setAutoNotifyDispatcher, setAutoNotifyWatcher, setAviatorEvaluator, setDispatcher, setEffector, setModel, setNamedDomainLinkConditionFuncParams, setNamedLinkConditionFuncParams, setNamedRoleManager, setRoleManager, setRoleManager, validateEnforce
-
-
-
-
Field Detail
-
expireTime
private java.time.Duration expireTime
-
cache
private Cache cache
-
enableCache
private final java.util.concurrent.atomic.AtomicBoolean enableCache
-
READ_WRITE_LOCK
private static final java.util.concurrent.locks.ReadWriteLock READ_WRITE_LOCK
-
-
Constructor Detail
-
SyncedCachedEnforcer
public SyncedCachedEnforcer()
Default constructor. Initializes a new SyncedCachedEnforcer with a default cache.
-
SyncedCachedEnforcer
public SyncedCachedEnforcer(java.lang.String modelPath, java.lang.String policyFile)
Initializes an enforcer with a model file and a policy file.- Parameters:
modelPath
- The path of the model file.policyFile
- The path of the policy file.
-
SyncedCachedEnforcer
public SyncedCachedEnforcer(java.lang.String modelPath, Adapter adapter)
Initializes an enforcer with a model file and a database adapter.- Parameters:
modelPath
- The path of the model file.adapter
- The adapter for the database.
-
SyncedCachedEnforcer
public SyncedCachedEnforcer(Model m, Adapter adapter)
Initializes an enforcer with a model and a database adapter.- Parameters:
m
- The model.adapter
- The adapter for the database.
-
SyncedCachedEnforcer
public SyncedCachedEnforcer(Model m)
Initializes an enforcer with a model.- Parameters:
m
- The model.
-
SyncedCachedEnforcer
public SyncedCachedEnforcer(java.lang.String modelPath)
Initializes an enforcer with a model file.- Parameters:
modelPath
- The path of the model file.
-
SyncedCachedEnforcer
public SyncedCachedEnforcer(java.lang.String modelPath, java.lang.String policyFile, boolean enableLog)
Initializes an enforcer with a model file, a policy file, and a logging flag.- Parameters:
modelPath
- The path of the model file.policyFile
- The path of the policy file.enableLog
- Whether to enable logging for Casbin.
-
-
Method Detail
-
enableCache
public void enableCache(boolean enable)
Enables or disables caching.- Parameters:
enable
- Whether to enable caching.
-
enforce
public boolean enforce(java.lang.Object... rvals)
Performs an enforcement check based on given parameters, using the cache.- Overrides:
enforce
in classSyncedEnforcer
- Parameters:
rvals
- Parameters for the enforcement check.- Returns:
- The result of the enforcement check.
-
loadPolicy
public void loadPolicy()
Loads the policy, clearing the cache if enabled.- Overrides:
loadPolicy
in classSyncedEnforcer
-
addPolicy
public boolean addPolicy(java.lang.String... params)
Adds a single policy while checking and removing the cache.- Overrides:
addPolicy
in classSyncedEnforcer
- Parameters:
params
- Policy parameters.- Returns:
- Whether the addition was successful.
-
addPolicies
public boolean addPolicies(java.util.List<java.util.List<java.lang.String>> rules)
Adds multiple policies while checking and removing the cache.- Overrides:
addPolicies
in classSyncedEnforcer
- Parameters:
rules
- Policy rules.- Returns:
- Whether the addition was successful.
-
addPolicies
public boolean addPolicies(java.lang.String[][] rules)
Adds multiple policies while checking and removing the cache.- Overrides:
addPolicies
in classSyncedEnforcer
- Parameters:
rules
- Policy rules.- Returns:
- Whether the addition was successful.
-
removePolicy
public boolean removePolicy(java.lang.String... params)
Removes a single policy while checking and removing the cache.- Overrides:
removePolicy
in classSyncedEnforcer
- Parameters:
params
- Policy parameters.- Returns:
- Whether the removal was successful.
-
removePolicies
public boolean removePolicies(java.util.List<java.util.List<java.lang.String>> rules)
Removes multiple policies while checking and removing the cache.- Overrides:
removePolicies
in classSyncedEnforcer
- Parameters:
rules
- Policy rules.- Returns:
- Whether the removal was successful.
-
removePolicies
public boolean removePolicies(java.lang.String[][] rules)
Removes multiple policies while checking and removing the cache.- Overrides:
removePolicies
in classSyncedEnforcer
- Parameters:
rules
- Policy rules.- Returns:
- Whether the removal was successful.
-
getCachedResult
private java.lang.Boolean getCachedResult(java.lang.String key)
Retrieves a cached result based on the given key.- Parameters:
key
- The cache key.- Returns:
- The cached result.
-
setExpireTime
public void setExpireTime(java.time.Duration expireTime)
Sets the cache expiration time.- Parameters:
expireTime
- The expiration time.
-
setCache
public void setCache(Cache cache)
Sets a custom cache.- Parameters:
cache
- The custom cache.
-
setCachedResult
private void setCachedResult(java.lang.String key, boolean result, java.lang.Object... extra)
Sets the cached result.- Parameters:
key
- The cache key.result
- The enforcement check result.extra
- Additional parameters.
-
getCacheKey
public java.lang.String getCacheKey(java.lang.Object... params)
Retrieves a cache key from the given parameters.- Parameters:
params
- The parameters for generating the key.- Returns:
- The generated cache key as a string.
-
getKey
private java.lang.String getKey(java.lang.Object... params)
Generates a key based on the given parameters.- Parameters:
params
- Parameters.- Returns:
- The generated key.
-
invalidateCache
public void invalidateCache()
Invalidates the cache by clearing it.
-
checkOneAndRemoveCache
private boolean checkOneAndRemoveCache(java.lang.String... params)
Checks and removes cache for a single policy.- Parameters:
params
- Policy parameters.- Returns:
- Whether the check was successful.
-
checkManyAndRemoveCache
private boolean checkManyAndRemoveCache(java.util.List<java.util.List<java.lang.String>> rules)
Checks and removes cache for multiple policies.- Parameters:
rules
- Policy rules.- Returns:
- Whether the check was successful.
-
checkManyAndRemoveCache
private boolean checkManyAndRemoveCache(java.lang.String[][] rules)
Checks and removes cache for multiple policies.- Parameters:
rules
- Policy rules.- Returns:
- Whether the check was successful.
-
-