Class SyncedCachedEnforcer


public class SyncedCachedEnforcer extends SyncedEnforcer
  • Field Details

  • Constructor Details

    • SyncedCachedEnforcer

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

      public SyncedCachedEnforcer(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.
    • SyncedCachedEnforcer

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

      public SyncedCachedEnforcer(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

    • enableCache

      public void enableCache(boolean enable)
      Enables or disables caching.
      Parameters:
      enable - Whether to enable caching.
    • enforce

      public boolean enforce(Object... rvals)
      Performs an enforcement check based on given parameters, using the cache.
      Overrides:
      enforce in class SyncedEnforcer
      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 class SyncedEnforcer
    • addPolicy

      public boolean addPolicy(String... params)
      Adds a single policy while checking and removing the cache.
      Overrides:
      addPolicy in class SyncedEnforcer
      Parameters:
      params - Policy parameters.
      Returns:
      Whether the addition was successful.
    • addPolicies

      public boolean addPolicies(List<List<String>> rules)
      Adds multiple policies while checking and removing the cache.
      Overrides:
      addPolicies in class SyncedEnforcer
      Parameters:
      rules - Policy rules.
      Returns:
      Whether the addition was successful.
    • addPolicies

      public boolean addPolicies(String[][] rules)
      Adds multiple policies while checking and removing the cache.
      Overrides:
      addPolicies in class SyncedEnforcer
      Parameters:
      rules - Policy rules.
      Returns:
      Whether the addition was successful.
    • removePolicy

      public boolean removePolicy(String... params)
      Removes a single policy while checking and removing the cache.
      Overrides:
      removePolicy in class SyncedEnforcer
      Parameters:
      params - Policy parameters.
      Returns:
      Whether the removal was successful.
    • removePolicies

      public boolean removePolicies(List<List<String>> rules)
      Removes multiple policies while checking and removing the cache.
      Overrides:
      removePolicies in class SyncedEnforcer
      Parameters:
      rules - Policy rules.
      Returns:
      Whether the removal was successful.
    • removePolicies

      public boolean removePolicies(String[][] rules)
      Removes multiple policies while checking and removing the cache.
      Overrides:
      removePolicies in class SyncedEnforcer
      Parameters:
      rules - Policy rules.
      Returns:
      Whether the removal was successful.
    • getCachedResult

      private Boolean getCachedResult(String key)
      Retrieves a cached result based on the given key.
      Parameters:
      key - The cache key.
      Returns:
      The cached result.
    • setExpireTime

      public void setExpireTime(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(String key, boolean result, Object... extra)
      Sets the cached result.
      Parameters:
      key - The cache key.
      result - The enforcement check result.
      extra - Additional parameters.
    • 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.
    • getKey

      private String getKey(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(String... params)
      Checks and removes cache for a single policy.
      Parameters:
      params - Policy parameters.
      Returns:
      Whether the check was successful.
    • checkManyAndRemoveCache

      private boolean checkManyAndRemoveCache(List<List<String>> rules)
      Checks and removes cache for multiple policies.
      Parameters:
      rules - Policy rules.
      Returns:
      Whether the check was successful.
    • checkManyAndRemoveCache

      private boolean checkManyAndRemoveCache(String[][] rules)
      Checks and removes cache for multiple policies.
      Parameters:
      rules - Policy rules.
      Returns:
      Whether the check was successful.