Class CachedEnforcer


public class CachedEnforcer extends Enforcer
  • Constructor Details

    • CachedEnforcer

      public CachedEnforcer()
      Default constructor for CachedEnforcer. Initializes a new CachedEnforcer with a default cache.
    • CachedEnforcer

      public CachedEnforcer(String modelPath, 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(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(String modelPath)
      Initializes an enforcer with a model file.
      Parameters:
      modelPath - The path of the model file.
    • CachedEnforcer

      public CachedEnforcer(String modelPath, 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 Details

    • getCache

      public Cache getCache()
      Retrieves the current cache used by this CachedEnforcer.
      Returns:
      The cache instance.
    • enforce

      public boolean enforce(Object... rvals)
      Enforces a policy based on the given request values.
      Overrides:
      enforce in class CoreEnforcer
      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 class CoreEnforcer
    • removePolicy

      public boolean removePolicy(String... params)
      Removes a policy from the enforcer.
      Overrides:
      removePolicy in class ManagementEnforcer
      Parameters:
      params - The parameters of the policy to be removed.
      Returns:
      True if the policy was removed, false otherwise.
    • removePolicies

      public boolean removePolicies(List<List<String>> rules)
      Removes multiple policies from the enforcer.
      Overrides:
      removePolicies in class ManagementEnforcer
      Parameters:
      rules - The list of policies to be removed.
      Returns:
      True if the policies were removed, false otherwise.
    • removePolicies

      public boolean removePolicies(String[][] rules)
      Removes multiple policies from the enforcer.
      Overrides:
      removePolicies in class ManagementEnforcer
      Parameters:
      rules - The list of policies to be removed.
      Returns:
      True if the policies were removed, false otherwise.
    • setExpireTime

      public void setExpireTime(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.
    • getCacheKey

      public String getCacheKey(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 class CoreEnforcer
    • buildIncrementalRoleLinks

      public void buildIncrementalRoleLinks(Model.PolicyOperations op, String ptype, List<List<String>> rules)
      buildIncrementalRoleLinks provides incremental build the role inheritance relations.
      Parameters:
      op - Policy operations.
      ptype - policy type.
      rules - the rules.