Doslam opened a new pull request, #426: URL: https://github.com/apache/casbin-pycasbin/pull/426
This PR adds CachedEnforcer, SyncedCachedEnforcer, AsyncCachedEnforcer and g() function cache for pycasbin. Three new enforcer classes are now available — CachedEnforcer (single-thread), SyncedCachedEnforcer (thread-safe) and AsyncCachedEnforcer (async-safe) — all providing decision caching on enforce() results. Existing Enforcer also gains enable_g_function_cache() to control role-lookup caching. This aligns pycasbin with go-casbin's CachedEnforcer. Additionally, SyncedCachedEnforcer and AsyncCachedEnforcer are provided for concurrent workflows not covered by go-casbin. Two independent caches: - **Decision cache** — caches enforce() results. Repeated same sub/obj/act return instantly. Auto-invalidated on any policy mutation (_add_policy/_remove/_update/load/clear). - **g() function cache** — caches has_link() on RoleManager via lru_cache. Avoids redundant role-graph traversal inside each enforce(). Opt-in via enable_g_function_cache(True). Cleared on add_link/delete_link/clear. ### Cache invalidation | Operation | Decision cache | g() cache | |---|---|---| | add/remove/update p policy | Cleared | Unchanged | | add/remove grouping policy | Cleared | Cleared | | load_policy / clear_policy | Cleared | Cleared | ### Files **New:** - casbin/cache/__init__.py — Cache ABC - casbin/cache/default_cache.py — DefaultCache (dict + TTL) - casbin/cached_enforcer.py — CachedEnforcer - casbin/synced_cached_enforcer.py — SyncedCachedEnforcer - casbin/async_cached_enforcer.py — AsyncCachedEnforcer - tests/test_cached_enforcer.py — 22 tests **Modified:** - casbin/core_enforcer.py — add enable_g_function_cache() - casbin/rbac/default_role_manager/role_manager.py — lru_cache + clear_g_cache + DomainManager propagation - casbin/__init__.py — exports ### Compatibility Zero breaking changes. g() cache disabled by default. All additive. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
