Package org.casbin.jcasbin.main
Class CachedEnforcer
- 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.CachedEnforcer
-
public class CachedEnforcer extends Enforcer
-
-
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 CachedEnforcer()
Default constructor for CachedEnforcer.CachedEnforcer(java.lang.String modelPath)
Initializes an enforcer with a model file.CachedEnforcer(java.lang.String modelPath, java.lang.String policyFile)
Initializes an enforcer with a model file and a policy file.CachedEnforcer(java.lang.String modelPath, java.lang.String policyFile, boolean enableLog)
Initializes an enforcer with a model file, a policy file, and a logging flag.CachedEnforcer(java.lang.String modelPath, Adapter adapter)
Initializes an enforcer with a model file and a database adapter.CachedEnforcer(Model m)
Initializes an enforcer with a model.CachedEnforcer(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 void
clearPolicy()
Clears all policies from the enforcer.boolean
enforce(java.lang.Object... rvals)
Enforces a policy based on the given request values.Cache
getCache()
Retrieves the current cache used by this CachedEnforcer.private boolean
getCachedResult(java.lang.String key)
Retrieves a cached result based on the 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 cache key from the given parameters.void
invalidateCache()
Invalidates all cached decisions.void
loadPolicy()
Loads policies into the enforcer.boolean
removePolicies(java.lang.String[][] rules)
Removes multiple policies from the enforcer.boolean
removePolicies(java.util.List<java.util.List<java.lang.String>> rules)
Removes multiple policies from the enforcer.boolean
removePolicy(java.lang.String... params)
Removes a policy from the enforcer.void
setCache(Cache cache)
Sets a custom cache implementation.private void
setCachedResult(java.lang.String key, boolean result, java.time.Duration expireTime)
Stores a result in the cache with an expiration time.void
setExpireTime(java.time.Duration expireTime)
Sets the expiration time for cached items.-
Methods inherited from class org.casbin.jcasbin.main.Enforcer
addPermissionForUser, addPermissionForUser, addRoleForUser, addRoleForUserInDomain, batchEnforce, batchEnforceWithMatcher, deletePermission, deletePermission, deletePermissionForUser, deletePermissionForUser, deletePermissionsForUser, deleteRole, deleteRoleForUser, deleteRoleForUserInDomain, deleteRolesForUser, deleteUser, getImplicitPermissionsForUser, getImplicitPermissionsForUserInDomain, getImplicitRolesForUser, getImplicitUsersForRole, getNamedImplicitPermissionsForUser, getNamedPermissionsForUser, getPermissionsForUser, getPermissionsForUserInDomain, getRolesForUser, getRolesForUserInDomain, getUsersForRole, getUsersForRoleInDomain, hasPermissionForUser, hasPermissionForUser, hasRoleForUser, updatePermissionForUser
-
Methods inherited from class org.casbin.jcasbin.main.ManagementEnforcer
addFunction, addGroupingPolicies, addGroupingPolicies, addGroupingPolicy, addGroupingPolicy, addNamedGroupingPolicies, addNamedGroupingPolicies, addNamedGroupingPolicy, addNamedGroupingPolicy, addNamedPolicies, addNamedPoliciesEx, addNamedPolicy, addNamedPolicy, addPolicies, addPolicies, addPolicy, addPolicy, getAllActions, getAllNamedActions, getAllNamedObjects, getAllNamedRoles, getAllNamedSubjects, getAllObjects, getAllRoles, getAllSubjects, getFilteredGroupingPolicy, getFilteredNamedGroupingPolicy, getFilteredNamedPolicy, getFilteredPolicy, getGroupingPolicy, getNamedGroupingPolicy, getNamedPolicy, getPermittedActions, getPolicy, hasGroupingPolicy, hasGroupingPolicy, hasNamedGroupingPolicy, hasNamedGroupingPolicy, hasNamedPolicy, hasNamedPolicy, hasPolicy, hasPolicy, removeFilteredGroupingPolicy, removeFilteredNamedGroupingPolicy, removeFilteredNamedPolicy, removeFilteredPolicy, removeGroupingPolicies, removeGroupingPolicies, removeGroupingPolicy, removeGroupingPolicy, removeNamedGroupingPolicies, removeNamedGroupingPolicies, removeNamedGroupingPolicy, removeNamedGroupingPolicy, removeNamedPolicies, removeNamedPolicy, removeNamedPolicy, removePolicy, updateGroupingPolicy, updateNamedGroupingPolicy, updateNamedPolicy, updatePolicy
-
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, buildRoleLinks, enableAcceptJsonRequest, enableAutoBuildRoleLinks, enableAutoSave, enableEnforce, enableLog, enforceEx, enforceExWithMatcher, enforceWithMatcher, getAdapter, getAviatorEval, getModel, getNamedRoleManager, getRmMap, getRoleManager, initialize, isAutoNotifyDispatcher, isAutoNotifyWatcher, isFiltered, loadFilteredPolicy, loadModel, mustUseDispatcher, newModel, newModel, newModel, resetExpressionEvaluator, savePolicy, setAdapter, setAutoNotifyDispatcher, setAutoNotifyWatcher, setAviatorEvaluator, setDispatcher, setEffector, setModel, setNamedDomainLinkConditionFuncParams, setNamedLinkConditionFuncParams, setNamedRoleManager, setRoleManager, setRoleManager, setWatcher, 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
-
CachedEnforcer
public CachedEnforcer()
Default constructor for CachedEnforcer. Initializes a new CachedEnforcer with a default cache.
-
CachedEnforcer
public CachedEnforcer(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.
-
CachedEnforcer
public CachedEnforcer(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.
-
CachedEnforcer
public CachedEnforcer(Model m, Adapter adapter)
Initializes an enforcer with a model and a database adapter.- Parameters:
m
- The model.adapter
- The adapter for the database.
-
CachedEnforcer
public CachedEnforcer(Model m)
Initializes an enforcer with a model.- Parameters:
m
- The model.
-
CachedEnforcer
public CachedEnforcer(java.lang.String modelPath)
Initializes an enforcer with a model file.- Parameters:
modelPath
- The path of the model file.
-
CachedEnforcer
public CachedEnforcer(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
-
getCache
public Cache getCache()
Retrieves the current cache used by this CachedEnforcer.- Returns:
- The cache instance.
-
enforce
public boolean enforce(java.lang.Object... rvals)
Enforces a policy based on the given request values.- Overrides:
enforce
in classCoreEnforcer
- Parameters:
rvals
- The request values, usually in the format of (sub, obj, act).- Returns:
- The result of the enforcement (true or false).
-
loadPolicy
public void loadPolicy()
Loads policies into the enforcer. If caching is enabled, clears the cache before loading policies.- Overrides:
loadPolicy
in classCoreEnforcer
-
removePolicy
public boolean removePolicy(java.lang.String... params)
Removes a policy from the enforcer.- Overrides:
removePolicy
in classManagementEnforcer
- Parameters:
params
- The parameters of the policy to be removed.- Returns:
- True if the policy was removed, false otherwise.
-
removePolicies
public boolean removePolicies(java.util.List<java.util.List<java.lang.String>> rules)
Removes multiple policies from the enforcer.- Overrides:
removePolicies
in classManagementEnforcer
- Parameters:
rules
- The list of policies to be removed.- Returns:
- True if the policies were removed, false otherwise.
-
removePolicies
public boolean removePolicies(java.lang.String[][] rules)
Removes multiple policies from the enforcer.- Overrides:
removePolicies
in classManagementEnforcer
- Parameters:
rules
- The list of policies to be removed.- Returns:
- True if the policies were removed, false otherwise.
-
getCachedResult
private boolean getCachedResult(java.lang.String key)
Retrieves a cached result based on the key.- Parameters:
key
- The cache key.- Returns:
- The cached result, or null if not found.
-
setExpireTime
public void setExpireTime(java.time.Duration expireTime)
Sets the expiration time for cached items.- Parameters:
expireTime
- The duration after which cached items will expire.
-
setCache
public void setCache(Cache cache)
Sets a custom cache implementation.- Parameters:
cache
- The cache instance to use.
-
setCachedResult
private void setCachedResult(java.lang.String key, boolean result, java.time.Duration expireTime)
Stores a result in the cache with an expiration time.- Parameters:
key
- The cache key.result
- The result to cache.expireTime
- The duration for which the result should be cached.
-
getKey
private java.lang.String getKey(java.lang.Object... params)
Generates a cache key from the given parameters.- Parameters:
params
- The parameters for generating the key.- Returns:
- The generated cache key, or null if invalid parameters are provided.
-
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.
-
invalidateCache
public void invalidateCache()
Invalidates all cached decisions.
-
clearPolicy
public void clearPolicy()
Clears all policies from the enforcer. If caching is enabled, clears the cache before clearing policies.- Overrides:
clearPolicy
in classCoreEnforcer
-
-